use of qz.communication.SerialIO in project tray by qzind.
the class SerialUtilities method setupSerialPort.
public static void setupSerialPort(final Session session, String UID, SocketConnection connection, JSONObject params) throws JSONException {
final String portName = params.getString("port");
if (connection.getSerialPort(portName) != null) {
PrintSocketClient.sendError(session, UID, String.format("Serial port [%s] is already open.", portName));
return;
}
try {
SerialOptions props = new SerialOptions(params.optJSONObject("options"), true);
final SerialIO serial = new SerialIO(portName);
if (serial.open(props)) {
connection.addSerialPort(portName, serial);
// apply listener here, so we can send all replies to the browser
serial.applyPortListener(spe -> {
String output = serial.processSerialEvent(spe);
if (output != null) {
log.debug("Received serial output: {}", output);
StreamEvent event = new StreamEvent(StreamEvent.Stream.SERIAL, StreamEvent.Type.RECEIVE).withData("portName", portName).withData("output", output);
PrintSocketClient.sendStream(session, event);
}
});
PrintSocketClient.sendResult(session, UID, null);
} else {
PrintSocketClient.sendError(session, UID, String.format("Unable to open serial port [%s]", portName));
}
} catch (SerialPortException e) {
PrintSocketClient.sendError(session, UID, e);
}
}
Aggregations