From cd9cb59bec502e2f027f33f9fac404da5a7c9398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norbert=20S=C3=A1ndor?= Date: Tue, 1 Sep 2020 21:04:50 +0200 Subject: [PATCH] #41: +JSerialCommTransport --- pom.xml | 6 + .../transport/JSerialCommTransport.java | 103 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/main/java/org/firmata4j/transport/JSerialCommTransport.java diff --git a/pom.xml b/pom.xml index 239b372..cddfc85 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,12 @@ jssc ${jssc.version} + + com.fazecast + jSerialComm + 2.6.2 + true + org.slf4j diff --git a/src/main/java/org/firmata4j/transport/JSerialCommTransport.java b/src/main/java/org/firmata4j/transport/JSerialCommTransport.java new file mode 100644 index 0000000..b32b288 --- /dev/null +++ b/src/main/java/org/firmata4j/transport/JSerialCommTransport.java @@ -0,0 +1,103 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2019 Oleg Kurbatov (o.v.kurbatov@gmail.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.firmata4j.transport; + +import java.io.IOException; + +import org.firmata4j.Parser; + +import com.fazecast.jSerialComm.SerialPort; +import com.fazecast.jSerialComm.SerialPortDataListener; +import com.fazecast.jSerialComm.SerialPortEvent; + +/** + * Implementation of {@link TransportInterface} based on {@link SerialPort}. + * + * @author Norbert Sándor + */ +public class JSerialCommTransport implements TransportInterface +{ + private final SerialPort serialPort; + + private Parser parser; + + public JSerialCommTransport(String portDescriptor) + { + serialPort = SerialPort.getCommPort(portDescriptor); + } + + @Override + public void start() throws IOException + { + if (!serialPort.isOpen()) + { + if (serialPort.openPort()) + { + serialPort.setComPortParameters(jssc.SerialPort.BAUDRATE_57600, jssc.SerialPort.DATABITS_8, + SerialPort.ONE_STOP_BIT, SerialPort.NO_PARITY); + serialPort.addDataListener(new SerialPortDataListener() + { + @Override + public void serialEvent(SerialPortEvent event) + { + if (event.getEventType() == SerialPort.LISTENING_EVENT_DATA_RECEIVED) + { + parser.parse(event.getReceivedData()); + } + } + + @Override + public int getListeningEvents() + { + return SerialPort.LISTENING_EVENT_DATA_RECEIVED; + } + }); + } else + { + throw new IOException("Cannot start firmata device: port=" + serialPort); + } + } + } + + @Override + public void stop() throws IOException + { + if (serialPort.isOpen() && !serialPort.closePort()) + { + throw new IOException("Cannot properly stop firmata device: port=" + serialPort); + } + } + + @Override + public void write(byte[] bytes) throws IOException + { + serialPort.writeBytes(bytes, bytes.length); + } + + @Override + public void setParser(Parser parser) + { + this.parser = parser; + } +}