What happened?
When defining a ModbusTag with array size > 1, the ModbusOptimizer.processReadResponses(...) ModbusTagCoil logic returns only the first bit as the result. The array logic works correctly for registers, but the customized coil logic is missing the array handling:
|
// Calculate the byte that contains the response for this Coil |
|
byte[] responseData = response.getResponseData(); |
|
int bitPosition = coilTag.getAddress() - response.startingAddress; |
|
int bytePosition = bitPosition / 8; |
|
int bitPositionInByte = bitPosition % 8; |
|
boolean isBitSet = (responseData[bytePosition] & (1 << bitPositionInByte)) != 0; |
|
values.put(tagName, new DefaultPlcResponseItem<>(PlcResponseCode.OK, new PlcBOOL(isBitSet))); |
What it should do is check for the for the modbusTag.getNumberOfElements() and generate the PlcList when needed. My current development version fixes this as follows:
if (modbusTag.getNumberOfElements() > 1) {
PlcList bitValues = new PlcList();
for (int i = 0; i < modbusTag.getNumberOfElements(); i++) {
int bitPosition = modbusTag.getAddress() - response.startingAddress + i;
boolean isBitSet = getIsBitSet(bitPosition, responseData);
bitValues.add(new PlcBOOL(isBitSet));
}
values.put(tagName, new DefaultPlcResponseItem<>(PlcResponseCode.OK, bitValues));
} else {
int bitPosition = modbusTag.getAddress() - response.startingAddress;
boolean isBitSet = getIsBitSet(bitPosition, responseData);
values.put(tagName, new DefaultPlcResponseItem<>(PlcResponseCode.OK, new PlcBOOL(isBitSet)));
}
private static boolean getIsBitSet(int bitPosition, byte[] responseData) {
int bytePosition = bitPosition / 8;
int bitPositionInByte = bitPosition % 8;
boolean isBitSet = (responseData[bytePosition] & (1 << bitPositionInByte)) != 0;
return isBitSet;
}
Version
v0.13.0-SNAPSHOT
Programming Languages
Protocols
What happened?
When defining a
ModbusTagwith array size > 1, theModbusOptimizer.processReadResponses(...)ModbusTagCoillogic returns only the first bit as the result. The array logic works correctly for registers, but the customized coil logic is missing the array handling:plc4x/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/base/optimizer/ModbusOptimizer.java
Lines 210 to 216 in a7ab2cf
What it should do is check for the for the
modbusTag.getNumberOfElements()and generate thePlcListwhen needed. My current development version fixes this as follows:Version
v0.13.0-SNAPSHOT
Programming Languages
Protocols