diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java index 02328229d55a2..679aa68d61da2 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java @@ -631,7 +631,7 @@ private void validateReadIndex() throws ModbusConfigurationException { // Determine bit positions polled, both start and end inclusive int pollStartBitIndex = readRequest.getReference() * dataElementBits; - int pollEndBitIndex = pollStartBitIndex + readRequest.getDataLength() * dataElementBits; + int pollEndBitIndex = pollStartBitIndex + readRequest.getDataLength() * dataElementBits - 1; // Determine bit positions read, both start and end inclusive int readStartBitIndex = readIndex.get() * dataElementBits + readSubIndex.orElse(0) * valueTypeBitCount; diff --git a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java index db2ff85c78349..8c5352ef298df 100644 --- a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java +++ b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java @@ -293,8 +293,16 @@ public void testInitCoilsOutOfIndex() { ThingStatus.OFFLINE); } + @Test + public void testInitCoilsOutOfIndex2() { + // Reading coils 4, 5, 6. Coil 7 is out of bounds + testOutOfBoundsGeneric(4, 3, "7", ModbusReadFunctionCode.READ_COILS, ModbusConstants.ValueType.BIT, + ThingStatus.OFFLINE); + } + @Test public void testInitCoilsOK() { + // Reading coils 4, 5, 6. Coil 6 is OK testOutOfBoundsGeneric(4, 3, "6", ModbusReadFunctionCode.READ_COILS, ModbusConstants.ValueType.BIT, ThingStatus.ONLINE); } @@ -349,12 +357,20 @@ public void testInitRegistersWithInt8OK2() { @Test public void testInitRegistersWithInt16OK() { + // Poller reading registers 4, 5, 6. Register 6 is OK testOutOfBoundsGeneric(4, 3, "6", ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, ModbusConstants.ValueType.INT16, ThingStatus.ONLINE); } @Test public void testInitRegistersWithInt16OutOfBounds() { + // Poller reading registers 4, 5, 6. Register 7 is out-of-bounds + testOutOfBoundsGeneric(4, 3, "7", ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, + ModbusConstants.ValueType.INT16, ThingStatus.OFFLINE); + } + + @Test + public void testInitRegistersWithInt16OutOfBounds2() { testOutOfBoundsGeneric(4, 3, "8", ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, ModbusConstants.ValueType.INT16, ThingStatus.OFFLINE); }