Skip to content

Commit

Permalink
- Introduced a new optional "slaveId" parameter to the modbus driver
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz authored and acs committed Feb 24, 2020
1 parent a0cd00e commit 190e20c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public abstract class BaseModbusPlcConnection extends NettyPlcConnection impleme

private static final Logger logger = LoggerFactory.getLogger(BaseModbusPlcConnection.class);

// This slaveId defaults to 0 which is a broadcast.
private short slaveId = 0;

BaseModbusPlcConnection(ChannelFactory channelFactory, String params) {
super(channelFactory);

Expand All @@ -46,6 +49,9 @@ public abstract class BaseModbusPlcConnection extends NettyPlcConnection impleme
if (paramElements.length == 2) {
String paramValue = paramElements[1];
switch (paramName) {
case "slaveId": {
slaveId = Short.parseShort(paramValue);
}
default:
logger.debug("Unknown parameter {} with value {}", paramName, paramValue);
}
Expand Down Expand Up @@ -104,4 +110,8 @@ public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
.thenApply(PlcWriteResponse.class::cast);
}

public short getSlaveId() {
return slaveId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected ChannelHandler getChannelHandler(CompletableFuture<Void> sessionSetupC
@Override
protected void initChannel(Channel channel) {
channel.pipeline().addLast(new ModbusTcpCodec(new ModbusRequestEncoder(), new ModbusResponseDecoder()));
channel.pipeline().addLast(new Plc4XModbusProtocol());
channel.pipeline().addLast(new Plc4XModbusProtocol(getSlaveId()));
channel.pipeline().addLast(new SingleItemToSingleRequestProtocol(ModbusTcpPlcConnection.this, ModbusTcpPlcConnection.this, null, timer, null, false));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ private void assertMatching(Pattern pattern, String match) {
public void getConnection() throws Exception {
ModbusTcpPlcConnection modbusConnection = (ModbusTcpPlcConnection)
new PlcDriverManager().getConnection("modbus:tcp://localhost:" + tcpHexDumper.getPort());
assertThat(modbusConnection.getSlaveId(), is((short) 0));
modbusConnection.close();
}

@Test
public void getConnectionWithSlaveId() throws Exception {
ModbusTcpPlcConnection modbusConnection = (ModbusTcpPlcConnection)
new PlcDriverManager().getConnection("modbus:tcp://localhost:" + tcpHexDumper.getPort() + "?slaveId=42");
assertThat(modbusConnection.getSlaveId(), is((short) 42));
modbusConnection.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,

private final ConcurrentMap<Short, PlcRequestContainer<InternalPlcRequest, InternalPlcResponse>> requestsMap = new ConcurrentHashMap<>();

private final short slaveId;

public Plc4XModbusProtocol(short slaveId) {
this.slaveId = slaveId;
}

@Override
protected void encode(ChannelHandlerContext ctx, PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws Exception {
LOGGER.trace("(<--OUT): {}, {}, {}", ctx, msg, out);
Expand Down Expand Up @@ -89,8 +95,6 @@ private void encodeWriteRequest(PlcRequestContainer<InternalPlcRequest, Internal
LOGGER.warn("Supplied number of values [{}] don't match t the addressed quantity of [{}]", field.getQuantity(), quantity);
}

short unitId = 0;

/*
* It seems that in Modbus, there are only two types of resources, that can be accessed:
* - Register: 2 byte value
Expand Down Expand Up @@ -163,7 +167,7 @@ private void encodeWriteRequest(PlcRequestContainer<InternalPlcRequest, Internal
}
short transactionId = (short) this.transactionId.getAndIncrement();
requestsMap.put(transactionId, msg);
out.add(new ModbusTcpPayload(transactionId, unitId, modbusRequest));
out.add(new ModbusTcpPayload(transactionId, slaveId, modbusRequest));
}

private void encodeReadRequest(PlcRequestContainer<InternalPlcRequest, InternalPlcResponse> msg, List<Object> out) throws PlcException {
Expand All @@ -178,8 +182,6 @@ private void encodeReadRequest(PlcRequestContainer<InternalPlcRequest, InternalP

ModbusField field = (ModbusField) request.getField(fieldName);
int quantity = field.getQuantity();
// TODO: the unit the should be used for multiple Requests
short unitId = 0;

ModbusPdu modbusRequest;
if (field instanceof CoilModbusField) {
Expand All @@ -202,7 +204,7 @@ private void encodeReadRequest(PlcRequestContainer<InternalPlcRequest, InternalP
}
short transactionId = (short) this.transactionId.getAndIncrement();
requestsMap.put(transactionId, msg);
out.add(new ModbusTcpPayload(transactionId, unitId, modbusRequest));
out.add(new ModbusTcpPayload(transactionId, slaveId, modbusRequest));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -523,4 +525,4 @@ private DefaultModbusByteArrayFieldItem produceRegisterValueList(ByteBuf byteBuf
}
return new DefaultModbusByteArrayFieldItem(data.toArray(new Byte[0][0]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static byte[] cutRegister(byte[] right) {

@Before
public void setUp() {
SUT = new Plc4XModbusProtocol();
SUT = new Plc4XModbusProtocol((short) 1);
}

@Test
Expand Down

0 comments on commit 190e20c

Please sign in to comment.