Search in sources :

Example 1 with SerialOptions

use of qz.communication.SerialOptions 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);
    }
}
Also used : StreamEvent(qz.ws.StreamEvent) SerialPortException(jssc.SerialPortException) SerialOptions(qz.communication.SerialOptions) SerialIO(qz.communication.SerialIO)

Aggregations

SerialPortException (jssc.SerialPortException)1 SerialIO (qz.communication.SerialIO)1 SerialOptions (qz.communication.SerialOptions)1 StreamEvent (qz.ws.StreamEvent)1