Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Replace nrjavaserial with jSerialComm

Rainer Hitz56a9cd9d5eaa
diff --git a/agent/build.gradle b/agent/build.gradle
index ce483f99e..490b9d7cc 100644
--- a/agent/build.gradle
+++ b/agent/build.gradle
@@ -5,12 +5,10 @@ dependencies {
compile resolveProject(":container")
- compile("io.netty:netty-transport-rxtx:$nettyVersion") {
- exclude group: "org.rxtx"
- }
compile "io.netty:netty-codec:$nettyVersion"
compile "io.netty:netty-codec-http:$nettyVersion"
- compile "com.neuronrobotics:nrjavaserial:$nrjavaserialVersion"
+
+ compile "com.fazecast:jSerialComm:$jSerialCommVersion"
compile("com.github.calimero:calimero-core:$calimeroVersion") {
// Libraries should not depend on a logging implementation and/or alpha versions!
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/DefaultJSerialCommChannelConfig.java b/agent/src/main/java/org/openremote/agent/protocol/serial/DefaultJSerialCommChannelConfig.java
new file mode 100644
index 000000000..298ad5910
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/DefaultJSerialCommChannelConfig.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2021, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol.serial;
+
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.DefaultChannelConfig;
+import io.netty.channel.MessageSizeEstimator;
+import io.netty.channel.RecvByteBufAllocator;
+
+import java.util.Map;
+
+import static org.openremote.agent.protocol.serial.JSerialCommChannelOption.*;
+
+/**
+ * A configuration class for jSerialComm device connections.
+ */
+final class DefaultJSerialCommChannelConfig extends DefaultChannelConfig implements JSerialCommChannelConfig {
+
+ private volatile int baudrate = 115200;
+ private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
+ private volatile int databits = 8;
+ private volatile Paritybit paritybit = Paritybit.NONE;
+ private volatile int waitTime;
+ private volatile int readTimeout = 200;
+
+ DefaultJSerialCommChannelConfig(JSerialCommChannel channel) {
+ super(channel);
+ }
+
+ @Override
+ public Map<ChannelOption<?>, Object> getOptions() {
+ return getOptions(super.getOptions(), BAUD_RATE, STOP_BITS, DATA_BITS, PARITY_BIT, WAIT_TIME);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T getOption(ChannelOption<T> option) {
+ if (option == BAUD_RATE) {
+ return (T) Integer.valueOf(getBaudrate());
+ }
+ if (option == STOP_BITS) {
+ return (T) getStopbits();
+ }
+ if (option == DATA_BITS) {
+ return (T) Integer.valueOf(getDatabits());
+ }
+ if (option == PARITY_BIT) {
+ return (T) getParitybit();
+ }
+ if (option == WAIT_TIME) {
+ return (T) Integer.valueOf(getWaitTimeMillis());
+ }
+ if (option == READ_TIMEOUT) {
+ return (T) Integer.valueOf(getReadTimeout());
+ }
+ return super.getOption(option);
+ }
+
+ @Override
+ public <T> boolean setOption(ChannelOption<T> option, T value) {
+ validate(option, value);
+
+ if (option == BAUD_RATE) {
+ setBaudrate((Integer) value);
+ } else if (option == STOP_BITS) {
+ setStopbits((Stopbits) value);
+ } else if (option == DATA_BITS) {
+ setDatabits((Integer) value);
+ } else if (option == PARITY_BIT) {
+ setParitybit((Paritybit) value);
+ } else if (option == WAIT_TIME) {
+ setWaitTimeMillis((Integer) value);
+ } else if (option == READ_TIMEOUT) {
+ setReadTimeout((Integer) value);
+ } else {
+ return super.setOption(option, value);
+ }
+ return true;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setBaudrate(final int baudrate) {
+ this.baudrate = baudrate;
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setStopbits(final Stopbits stopbits) {
+ this.stopbits = stopbits;
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setDatabits(final int databits) {
+ this.databits = databits;
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setParitybit(final Paritybit paritybit) {
+ this.paritybit = paritybit;
+ return this;
+ }
+
+ @Override
+ public int getBaudrate() {
+ return baudrate;
+ }
+
+ @Override
+ public Stopbits getStopbits() {
+ return stopbits;
+ }
+
+ @Override
+ public int getDatabits() {
+ return databits;
+ }
+
+ @Override
+ public Paritybit getParitybit() {
+ return paritybit;
+ }
+
+
+ @Override
+ public int getWaitTimeMillis() {
+ return waitTime;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setWaitTimeMillis(final int waitTimeMillis) {
+ if (waitTimeMillis < 0) {
+ throw new IllegalArgumentException("Wait time must be >= 0");
+ }
+ waitTime = waitTimeMillis;
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setReadTimeout(int readTimeout) {
+ if (readTimeout < 0) {
+ throw new IllegalArgumentException("readTime must be >= 0");
+ }
+ this.readTimeout = readTimeout;
+ return this;
+ }
+
+ @Override
+ public int getReadTimeout() {
+ return readTimeout;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
+ super.setConnectTimeoutMillis(connectTimeoutMillis);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setWriteSpinCount(int writeSpinCount) {
+ super.setWriteSpinCount(writeSpinCount);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setAllocator(ByteBufAllocator allocator) {
+ super.setAllocator(allocator);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
+ super.setRecvByteBufAllocator(allocator);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setAutoRead(boolean autoRead) {
+ super.setAutoRead(autoRead);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setAutoClose(boolean autoClose) {
+ super.setAutoClose(autoClose);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
+ super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
+ super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
+ return this;
+ }
+
+ @Override
+ public JSerialCommChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
+ super.setMessageSizeEstimator(estimator);
+ return this;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannel.java b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannel.java
new file mode 100644
index 000000000..fee853e70
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannel.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2021, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol.serial;
+
+import com.fazecast.jSerialComm.SerialPort;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelPromise;
+
+import java.io.IOException;
+import java.net.SocketAddress;
+import java.util.concurrent.TimeUnit;
+
+import static com.fazecast.jSerialComm.SerialPort.TIMEOUT_READ_SEMI_BLOCKING;
+import static org.openremote.agent.protocol.serial.JSerialCommChannelOption.*;
+
+/**
+ * A channel to a serial device using the jSerialComm library.
+ */
+@SuppressWarnings("deprecation")
+public class JSerialCommChannel extends io.netty.channel.oio.OioByteStreamChannel {
+
+ private static final JSerialCommDeviceAddress LOCAL_ADDRESS = new JSerialCommDeviceAddress("localhost");
+
+ private final JSerialCommChannelConfig config;
+
+ private boolean open = true;
+ private JSerialCommDeviceAddress deviceAddress;
+ private SerialPort serialPort;
+
+ public JSerialCommChannel() {
+ super(null);
+
+ config = new DefaultJSerialCommChannelConfig(this);
+ }
+
+ @Override
+ public JSerialCommChannelConfig config() {
+ return config;
+ }
+
+ @Override
+ public boolean isOpen() {
+ return open;
+ }
+
+ @Override
+ protected AbstractUnsafe newUnsafe() {
+ return new JSerialCommChannel.JSCUnsafe();
+ }
+
+ @Override
+ protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
+ JSerialCommDeviceAddress remote = (JSerialCommDeviceAddress) remoteAddress;
+ SerialPort commPort = SerialPort.getCommPort(remote.value());
+ if (!commPort.openPort()) {
+ throw new IOException("Could not open port: " + remote.value());
+ }
+ deviceAddress = remote;
+ serialPort = commPort;
+ }
+
+ protected void doInit() throws Exception {
+ serialPort.setComPortParameters(
+ config().getOption(BAUD_RATE),
+ config().getOption(DATA_BITS),
+ config().getOption(STOP_BITS).value(),
+ config().getOption(PARITY_BIT).value()
+ );
+ serialPort.setFlowControl(SerialPort.FLOW_CONTROL_DISABLED);
+ serialPort.setComPortTimeouts(TIMEOUT_READ_SEMI_BLOCKING, config().getOption(READ_TIMEOUT), 0);
+ activate(serialPort.getInputStreamWithSuppressedTimeoutExceptions(), serialPort.getOutputStream());
+ }
+
+ @Override
+ public JSerialCommDeviceAddress localAddress() {
+ return (JSerialCommDeviceAddress) super.localAddress();
+ }
+
+ @Override
+ public JSerialCommDeviceAddress remoteAddress() {
+ return (JSerialCommDeviceAddress) super.remoteAddress();
+ }
+
+ @Override
+ protected JSerialCommDeviceAddress localAddress0() {
+ return LOCAL_ADDRESS;
+ }
+
+ @Override
+ protected JSerialCommDeviceAddress remoteAddress0() {
+ return deviceAddress;
+ }
+
+ @Override
+ protected void doBind(SocketAddress localAddress) throws Exception {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ protected void doDisconnect() throws Exception {
+ doClose();
+ }
+
+ @Override
+ protected void doClose() throws Exception {
+ open = false;
+ try {
+ super.doClose();
+ } finally {
+ if (serialPort != null) {
+ serialPort.closePort();
+ serialPort = null;
+ }
+ }
+ }
+
+ @Override
+ protected boolean isInputShutdown() {
+ return !open;
+ }
+
+ @Override
+ protected ChannelFuture shutdownInput() {
+ return newFailedFuture(new UnsupportedOperationException("shutdownInput"));
+ }
+
+
+ private final class JSCUnsafe extends AbstractUnsafe {
+ @Override
+ public void connect(
+ final SocketAddress remoteAddress,
+ final SocketAddress localAddress, final ChannelPromise promise) {
+ if (!promise.setUncancellable() || !isOpen()) {
+ return;
+ }
+
+ try {
+ final boolean wasActive = isActive();
+ doConnect(remoteAddress, localAddress);
+
+ int waitTime = config().getOption(WAIT_TIME);
+ if (waitTime > 0) {
+ eventLoop().schedule(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ doInit();
+ safeSetSuccess(promise);
+ if (!wasActive && isActive()) {
+ pipeline().fireChannelActive();
+ }
+ } catch (Throwable t) {
+ safeSetFailure(promise, t);
+ closeIfClosed();
+ }
+ }
+ }, waitTime, TimeUnit.MILLISECONDS);
+ } else {
+ doInit();
+ safeSetSuccess(promise);
+ if (!wasActive && isActive()) {
+ pipeline().fireChannelActive();
+ }
+ }
+ } catch (Throwable t) {
+ safeSetFailure(promise, t);
+ closeIfClosed();
+ }
+ }
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannelConfig.java b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannelConfig.java
new file mode 100644
index 000000000..d23e74322
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannelConfig.java
@@ -0,0 +1,212 @@
+/*
+ * Copyright 2021, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol.serial;
+
+import com.fazecast.jSerialComm.SerialPort;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.ChannelConfig;
+import io.netty.channel.MessageSizeEstimator;
+import io.netty.channel.RecvByteBufAllocator;
+
+/**
+ * Configuration interface for jSerialComm device connections.
+ */
+public interface JSerialCommChannelConfig extends ChannelConfig {
+ enum Stopbits {
+ /**
+ * 1 stop bit will be sent at the end of every character
+ */
+ STOPBITS_1(SerialPort.ONE_STOP_BIT),
+ /**
+ * 2 stop bits will be sent at the end of every character
+ */
+ STOPBITS_2(SerialPort.TWO_STOP_BITS),
+ /**
+ * 1.5 stop bits will be sent at the end of every character
+ */
+ STOPBITS_1_5(SerialPort.ONE_POINT_FIVE_STOP_BITS);
+
+ private final int value;
+
+ Stopbits(int value) {
+ this.value = value;
+ }
+
+ public int value() {
+ return value;
+ }
+
+ public static Stopbits valueOf(int value) {
+ for (Stopbits stopbit : Stopbits.values()) {
+ if (stopbit.value == value) {
+ return stopbit;
+ }
+ }
+ throw new IllegalArgumentException("unknown " + Stopbits.class.getSimpleName() + " value: " + value);
+ }
+ }
+
+ enum Paritybit {
+ /**
+ * No parity bit will be sent with each data character at all
+ */
+ NONE(SerialPort.NO_PARITY),
+ /**
+ * An odd parity bit will be sent with each data character, ie. will be set
+ * to 1 if the data character contains an even number of bits set to 1.
+ */
+ ODD(SerialPort.ODD_PARITY),
+ /**
+ * An even parity bit will be sent with each data character, ie. will be set
+ * to 1 if the data character contains an odd number of bits set to 1.
+ */
+ EVEN(SerialPort.EVEN_PARITY),
+ /**
+ * A mark parity bit (ie. always 1) will be sent with each data character
+ */
+ MARK(SerialPort.MARK_PARITY),
+ /**
+ * A space parity bit (ie. always 0) will be sent with each data character
+ */
+ SPACE(SerialPort.SPACE_PARITY);
+
+ private final int value;
+
+ Paritybit(int value) {
+ this.value = value;
+ }
+
+ public int value() {
+ return value;
+ }
+
+ public static Paritybit valueOf(int value) {
+ for (Paritybit paritybit : Paritybit.values()) {
+ if (paritybit.value == value) {
+ return paritybit;
+ }
+ }
+ throw new IllegalArgumentException("unknown " + Paritybit.class.getSimpleName() + " value: " + value);
+ }
+ }
+
+ /**
+ * Sets the baud rate (ie. bits per second) for communication with the serial device.
+ * The baud rate will include bits for framing (in the form of stop bits and parity),
+ * such that the effective data rate will be lower than this value.
+ *
+ * @param baudrate The baud rate (in bits per second)
+ */
+ JSerialCommChannelConfig setBaudrate(int baudrate);
+
+ /**
+ * Sets the number of stop bits to include at the end of every character to aid the
+ * serial device in synchronising with the data.
+ *
+ * @param stopbits The number of stop bits to use
+ */
+ JSerialCommChannelConfig setStopbits(Stopbits stopbits);
+
+ /**
+ * Sets the number of data bits to use to make up each character sent to the serial
+ * device.
+ *
+ * @param databits The number of data bits to use
+ */
+ JSerialCommChannelConfig setDatabits(int databits);
+
+ /**
+ * Sets the type of parity bit to be used when communicating with the serial device.
+ *
+ * @param paritybit The type of parity bit to be used
+ */
+ JSerialCommChannelConfig setParitybit(Paritybit paritybit);
+
+ /**
+ * @return The configured baud rate, defaulting to 115200 if unset
+ */
+ int getBaudrate();
+
+ /**
+ * @return The configured stop bits, defaulting to {@link Stopbits#STOPBITS_1} if unset
+ */
+ Stopbits getStopbits();
+
+ /**
+ * @return The configured data bits, defaulting to 8 if unset
+ */
+ int getDatabits();
+
+ /**
+ * @return The configured parity bit, defaulting to {@link Paritybit#NONE} if unset
+ */
+ Paritybit getParitybit();
+
+ /**
+ * @return The number of milliseconds to wait between opening the serial port and
+ * initialising.
+ */
+ int getWaitTimeMillis();
+
+ /**
+ * Sets the time to wait after opening the serial port and before sending it any
+ * configuration information or data. A value of 0 indicates that no waiting should
+ * occur.
+ *
+ * @param waitTimeMillis The number of milliseconds to wait, defaulting to 0 (no
+ * wait) if unset
+ * @throws IllegalArgumentException if the supplied value is &lt; 0
+ */
+ JSerialCommChannelConfig setWaitTimeMillis(int waitTimeMillis);
+
+ /**
+ * Sets the maximal time (in ms) to block while try to read from the serial port. Default is 1000ms
+ */
+ JSerialCommChannelConfig setReadTimeout(int readTimeout);
+
+ /**
+ * Return the maximal time (in ms) to block and wait for something to be ready to read.
+ */
+ int getReadTimeout();
+
+ @Override
+ JSerialCommChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis);
+
+ @Override
+ JSerialCommChannelConfig setWriteSpinCount(int writeSpinCount);
+
+ @Override
+ JSerialCommChannelConfig setAllocator(ByteBufAllocator allocator);
+
+ @Override
+ JSerialCommChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator);
+
+ @Override
+ JSerialCommChannelConfig setAutoRead(boolean autoRead);
+
+ @Override
+ JSerialCommChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark);
+
+ @Override
+ JSerialCommChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark);
+
+ @Override
+ JSerialCommChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannelOption.java b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannelOption.java
new file mode 100644
index 000000000..0a6e7d0e9
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommChannelOption.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2021, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol.serial;
+
+import io.netty.channel.ChannelOption;
+import org.openremote.agent.protocol.serial.JSerialCommChannelConfig.Paritybit;
+import org.openremote.agent.protocol.serial.JSerialCommChannelConfig.Stopbits;
+
+/**
+ * Option for configuring a serial port connection.
+ */
+public final class JSerialCommChannelOption<T> extends ChannelOption<T> {
+
+ public static final ChannelOption<Integer> BAUD_RATE = valueOf("BAUD_RATE");
+ public static final ChannelOption<Stopbits> STOP_BITS = valueOf("STOP_BITS");
+ public static final ChannelOption<Integer> DATA_BITS = valueOf("DATA_BITS");
+ public static final ChannelOption<Paritybit> PARITY_BIT = valueOf("PARITY_BIT");
+ public static final ChannelOption<Integer> WAIT_TIME = valueOf("WAIT_TIME");
+ public static final ChannelOption<Integer> READ_TIMEOUT = valueOf("READ_TIMEOUT");
+
+ @SuppressWarnings({ "unused", "deprecation" })
+ private JSerialCommChannelOption() {
+ super(null);
+ }
+}
\ No newline at end of file
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/NrJavaSerialAddress.java b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommDeviceAddress.java
similarity index 63%
rename from agent/src/main/java/org/openremote/agent/protocol/serial/NrJavaSerialAddress.java
rename to agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommDeviceAddress.java
index 00700b670..c7402b7a7 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/serial/NrJavaSerialAddress.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/JSerialCommDeviceAddress.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017, OpenRemote Inc.
+ * Copyright 2021, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
@@ -19,17 +19,22 @@
*/
package org.openremote.agent.protocol.serial;
-@SuppressWarnings("deprecation")
-public class NrJavaSerialAddress extends io.netty.channel.rxtx.RxtxDeviceAddress {
+import java.net.SocketAddress;
- protected int baudRate;
+/**
+ * A SocketAddress subclass to wrap the serial port address of a jSerialComm device (e.g. COM1, /dev/ttyUSB0).
+ */
+public class JSerialCommDeviceAddress extends SocketAddress {
+
+ private static final long serialVersionUID = -2907820090993709523L;
+
+ private final String value;
- public NrJavaSerialAddress(String port, int baudRate) {
- super(port);
- this.baudRate = baudRate;
+ public JSerialCommDeviceAddress(String value) {
+ this.value = value;
}
- public int getBaudRate() {
- return baudRate;
+ public String value() {
+ return value;
}
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/NrJavaSerialChannel.java b/agent/src/main/java/org/openremote/agent/protocol/serial/NrJavaSerialChannel.java
deleted file mode 100644
index ac3bab634..000000000
--- a/agent/src/main/java/org/openremote/agent/protocol/serial/NrJavaSerialChannel.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Copyright 2017, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.agent.protocol.serial;
-
-import gnu.io.NRSerialPort;
-import gnu.io.SerialPort;
-import io.netty.channel.ConnectTimeoutException;
-
-import java.net.SocketAddress;
-
-@SuppressWarnings("deprecation")
-public class NrJavaSerialChannel extends io.netty.channel.rxtx.RxtxChannel {
-
- private boolean open = true;
- protected SerialPort serialPort;
- protected NrJavaSerialAddress deviceAddress;
-
- @Override
- public boolean isOpen() {
- return open;
- }
-
- @Override
- protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
- NrJavaSerialAddress remote = (NrJavaSerialAddress) remoteAddress;
- final NRSerialPort serial = new NRSerialPort(remote.value(), remote.getBaudRate());
- serial.connect();
- serialPort = serial.getSerialPortInstance();
- if (serialPort == null) {
- // No exception is thrown on connect failure so throw one
- throw new ConnectTimeoutException("Failed to establish connection to COM port: " + remote.value());
- }
- serialPort.enableReceiveTimeout(config().getOption(io.netty.channel.rxtx.RxtxChannelOption.READ_TIMEOUT));
- deviceAddress = remote;
- }
-
- @Override
- protected void doInit() throws Exception {
- serialPort.setSerialPortParams(
- config().getOption(io.netty.channel.rxtx.RxtxChannelOption.BAUD_RATE),
- config().getOption(io.netty.channel.rxtx.RxtxChannelOption.DATA_BITS).value(),
- config().getOption(io.netty.channel.rxtx.RxtxChannelOption.STOP_BITS).value(),
- config().getOption(io.netty.channel.rxtx.RxtxChannelOption.PARITY_BIT).value()
- );
- serialPort.setDTR(config().getOption(io.netty.channel.rxtx.RxtxChannelOption.DTR));
- serialPort.setRTS(config().getOption(io.netty.channel.rxtx.RxtxChannelOption.RTS));
-
- activate(serialPort.getInputStream(), serialPort.getOutputStream());
- }
-
- @Override
- protected void doClose() throws Exception {
- open = false;
- try {
- super.doClose();
- } finally {
- if (serialPort != null) {
- serialPort.removeEventListener();
- serialPort.close();
- serialPort = null;
- }
- }
- }
-}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialIOClient.java
index 9a100ea48..5b1eaabc5 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialIOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialIOClient.java
@@ -25,6 +25,11 @@ import org.openremote.agent.protocol.io.AbstractNettyIOClient;
import org.openremote.agent.protocol.io.IOClient;
import org.openremote.model.util.TextUtil;
+
+import static org.openremote.agent.protocol.serial.JSerialCommChannelConfig.Paritybit.NONE;
+import static org.openremote.agent.protocol.serial.JSerialCommChannelConfig.Stopbits.STOPBITS_1;
+import static org.openremote.agent.protocol.serial.JSerialCommChannelOption.*;
+
/**
* This is a {@link IOClient} implementation for serial ports.
* <p>
@@ -34,7 +39,7 @@ import org.openremote.model.util.TextUtil;
* to this client via {@link AbstractNettyIOClient#onMessageReceived} (see {@link ByteToMessageDecoder and
* {@link MessageToMessageDecoder}).
*/
-public class SerialIOClient<T> extends AbstractNettyIOClient<T, NrJavaSerialAddress> {
+public class SerialIOClient<T> extends AbstractNettyIOClient<T, JSerialCommDeviceAddress> {
protected String port;
protected int baudRate;
@@ -48,12 +53,12 @@ public class SerialIOClient<T> extends AbstractNettyIOClient<T, NrJavaSerialAddr
@Override
protected Class<? extends Channel> getChannelClass() {
- return NrJavaSerialChannel.class;
+ return JSerialCommChannel.class;
}
@Override
protected ChannelFuture startChannel() {
- return bootstrap.connect(new NrJavaSerialAddress(port, baudRate));
+ return bootstrap.connect(new JSerialCommDeviceAddress(port));
}
@Override
@@ -68,4 +73,13 @@ public class SerialIOClient<T> extends AbstractNettyIOClient<T, NrJavaSerialAddr
// with io.netty.channel.rxtx.RxtxChannel and causes IllegalStateException.
return new io.netty.channel.oio.OioEventLoopGroup(1);
}
+
+ @Override
+ protected void configureChannel() {
+ super.configureChannel();
+ bootstrap.option(BAUD_RATE, baudRate);
+ bootstrap.option(DATA_BITS, 8);
+ bootstrap.option(STOP_BITS, STOPBITS_1);
+ bootstrap.option(PARITY_BIT, NONE);
+ }
}
diff --git a/gradle.properties b/gradle.properties
index cf90a72ca..92da95972 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -32,7 +32,7 @@ easyRulesVersion = 4.1.0
quartzVersion= 2.3.2
calimeroVersion = 2.4
uuidGeneratorVersion = 3.1.3
-nrjavaserialVersion = 3.15.0
+jSerialCommVersion = 2.7.0
saxonHEVersion = 9.8.0-3
jmxtermVersion = 1.0.0
nettyVersion = 4.1.56.Final