[transport.modbus] Added support for RTU encoding over TCP (#1965)

* [modbus] add support for rtu encoding over tcp

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green 2020-12-26 17:10:06 +00:00 committed by GitHub
parent 49a17a5f5e
commit acb0cd5e9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 13 deletions

View File

@ -35,7 +35,7 @@
<dependency>
<groupId>net.wimpi</groupId>
<artifactId>jamod</artifactId>
<version>1.2.4.OH</version>
<version>1.3.0.OH</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -23,8 +23,15 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
@NonNullByDefault
public class ModbusTCPSlaveEndpoint extends ModbusIPSlaveEndpoint {
public ModbusTCPSlaveEndpoint(String address, int port) {
private boolean rtuEncoded;
public ModbusTCPSlaveEndpoint(String address, int port, boolean rtuEncoded) {
super(address, port);
this.rtuEncoded = rtuEncoded;
}
public boolean getRtuEncoded() {
return rtuEncoded;
}
@Override

View File

@ -162,7 +162,8 @@ public class ModbusSlaveConnectionFactoryImpl
if (config != null) {
connectTimeoutMillis = config.getConnectTimeoutMillis();
}
TCPMasterConnection connection = new TCPMasterConnection(address, key.getPort(), connectTimeoutMillis);
TCPMasterConnection connection = new TCPMasterConnection(address, key.getPort(), connectTimeoutMillis,
key.getRtuEncoded());
logger.trace("Created connection {} for endpoint {}", connection, key);
return connection;
}

View File

@ -271,7 +271,7 @@ public class IntegrationTestSupport extends JavaTest {
public ModbusSlaveEndpoint getEndpoint() {
assert tcpModbusPort > 0;
return new ModbusTCPSlaveEndpoint("127.0.0.1", tcpModbusPort);
return new ModbusTCPSlaveEndpoint("127.0.0.1", tcpModbusPort, false);
}
/**

View File

@ -29,8 +29,8 @@ public class ModbusSlaveEndpointTestCase {
@Test
public void testEqualsSameTcp() {
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
assertEquals(e1, e2);
}
@ -74,23 +74,23 @@ public class ModbusSlaveEndpointTestCase {
@Test
public void testEqualsDifferentTCPPort() {
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 501);
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 501, false);
assertNotEquals(e1, e2);
assertNotEquals(e1.hashCode(), e2.hashCode());
}
@Test
public void testEqualsDifferentTCPHost() {
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.2", 501);
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.2", 501, false);
assertNotEquals(e1, e2);
assertNotEquals(e1.hashCode(), e2.hashCode());
}
@Test
public void testEqualsDifferentProtocol() {
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
ModbusUDPSlaveEndpoint e2 = new ModbusUDPSlaveEndpoint("127.0.0.1", 500);
assertNotEquals(e1, e2);
assertNotEquals(e1.hashCode(), e2.hashCode());
@ -98,11 +98,23 @@ public class ModbusSlaveEndpointTestCase {
@Test
public void testEqualsDifferentProtocol2() {
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500);
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
ModbusSerialSlaveEndpoint e2 = new ModbusSerialSlaveEndpoint("port2", 9600, SerialPort.FLOWCONTROL_NONE,
SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,
Modbus.DEFAULT_SERIAL_ENCODING, true, 500);
assertNotEquals(e1, e2);
assertNotEquals(e1.hashCode(), e2.hashCode());
}
/*
* TCP slaves pointing to same host & port are considered equal even rtu encodinng differs.
* Thus ensures correct connection pooling and connection sharing
*/
@Test
public void testEqualsSameTcpDifferentEncoding() {
ModbusTCPSlaveEndpoint e1 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, false);
ModbusTCPSlaveEndpoint e2 = new ModbusTCPSlaveEndpoint("127.0.0.1", 500, true);
assertEquals(e1, e2);
assertEquals(e1.hashCode(), e2.hashCode());
}
}

View File

@ -181,7 +181,7 @@ public class SmokeTest extends IntegrationTestSupport {
public void testSlaveConnectionError() throws Exception {
// In the test we have non-responding slave (see http://stackoverflow.com/a/904609), and we use short connection
// timeout
ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("10.255.255.1", 9999);
ModbusSlaveEndpoint endpoint = new ModbusTCPSlaveEndpoint("10.255.255.1", 9999, false);
EndpointPoolConfiguration configuration = new EndpointPoolConfiguration();
configuration.setConnectTimeoutMillis(100);