● USB Serial로 연결된 멜로디 출력장치에 명령문을 전송하여 작동시키는 코드입니다.
package com.genie;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import com.fazecast.jSerialComm.*;
public class Caffeine {
public static void main(String[] args) {
String cmdStr;
Scanner scanner = new Scanner(System.in);
PipedInputStream pi = null;
PipedOutputStream po = null;
try {
pi = new PipedInputStream();
po = new PipedOutputStream(pi);
} catch (IOException ex) {
ex.printStackTrace();
}
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
Runnable devComm = new DevComm(pi);
Thread devThread = new Thread(devComm);
devThread.setName("DevComm");
devThread.start();
Timer timer = new Timer();
AlarmTask alarm = new AlarmTask(po);
timer.scheduleAtFixedRate(alarm, 0, 1000);
do {
System.out.println("Usage [ q : quit ]");
System.out.print("Input Command > ");
cmdStr = scanner.nextLine();
} while (!cmdStr.equals("q"));
timer.cancel();
devThread.interrupt();
}
}
package com.genie;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class AlarmTask extends TimerTask {
private LocalTime time;
private Integer Hour;
private Integer Minute;
private Integer Second;
private DataOutputStream theOutput;
public AlarmTask(PipedOutputStream out) {
theOutput = new DataOutputStream(out);
}
@Override
public void run() {
time = LocalTime.now();
Hour = time.getHour();
Minute = time.getMinute();
Second = time.getSecond();
try {
//System.out.println(String.format("%02d:%02d:%02d", Hour.intValue(), Minute.intValue(), Second.intValue()));
if (Minute == 0 && Second == 0)
{
//System.out.println("Send to the Pipe");
theOutput.writeUTF("{\"command\":\"LetPlayOn\"}");
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
package com.genie;
import java.lang.*;
import java.util.*;
import java.io.*;
import com.fazecast.jSerialComm.*;
public class DevComm implements Runnable {
final String devName = "/dev/ttyUSB1";
private SerialPort comPort;
private DataInputStream theInput;
public DevComm(PipedInputStream in) {
theInput = new DataInputStream(in);
}
@Override
public void run() {
comPort = SerialPort.getCommPort(devName);
comPort.setParity(SerialPort.NO_PARITY);
comPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
comPort.setNumDataBits(8);
comPort.setBaudRate(9600);
if (comPort.openPort())
{
comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0);
try {
while (true)
{
byte[] readBuffer = new byte[1024];
int numRead = comPort.readBytes(readBuffer, readBuffer.length);
String pipeBuffer = theInput.readUTF();
if (!pipeBuffer.isEmpty())
{
//System.out.println(String.format("[%d] %s", pipeBuffer.length(), pipeBuffer));
byte[] writeBuffer = pipeBuffer.getBytes();
comPort.writeBytes(writeBuffer, writeBuffer.length);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}