diff --git a/examples/fc1.php b/examples/fc1.php index 36ff8d8..cf81090 100644 --- a/examples/fc1.php +++ b/examples/fc1.php @@ -16,7 +16,7 @@ $startAddress = 12288; $quantity = 16; -$unitID = 0; +$unitID = 1; $packet = new ReadCoilsRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/examples/fc15.php b/examples/fc15.php index 2e087cd..beabbd2 100644 --- a/examples/fc15.php +++ b/examples/fc15.php @@ -20,7 +20,7 @@ 0, 0, 0, 0, 0, 0, 0, 0, // dec: 0, hex x0 1, 0, 0, 1 // dec: 9, hex: x9 ]; -$unitID = 0; +$unitID = 1; $packet = new WriteMultipleCoilsRequest($startAddress, $coils, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/examples/fc16.php b/examples/fc16.php index df60e92..ffd7ea7 100644 --- a/examples/fc16.php +++ b/examples/fc16.php @@ -22,7 +22,7 @@ Types::toInt16(-1000), //hex: FC18 as word Types::toInt32(2000), //dec: 2000 -> hex: 7d00 is as 2 word 7D00 0000 ]; -$unitID = 0; +$unitID = 1; $packet = new WriteMultipleRegistersRequest($startAddress, $registers, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/examples/fc2.php b/examples/fc2.php index 81bea0a..766c121 100644 --- a/examples/fc2.php +++ b/examples/fc2.php @@ -16,7 +16,7 @@ $startAddress = 12288; $quantity = 16; -$unitID = 0; +$unitID = 1; $packet = new ReadInputDiscretesRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/examples/fc22.php b/examples/fc22.php index 5d0c50f..85859b2 100644 --- a/examples/fc22.php +++ b/examples/fc22.php @@ -23,7 +23,7 @@ $ORMask = 0x0007; // 2 bytes, bin = 0b00000111 $ORMask &= ~(1 << $bitPosition); // clear the bit, set third bit to 0. $ORMask = 0x0003, 0b00000011 -$unitID = 0; +$unitID = 1; $packet = new MaskWriteRegisterRequest( $readStartAddress, $ANDMask, diff --git a/examples/fc23.php b/examples/fc23.php index bc93d94..f77f81d 100644 --- a/examples/fc23.php +++ b/examples/fc23.php @@ -23,7 +23,7 @@ Types::toInt16(10), //000a as word Types::toInt16(-1000), //hex: FC18 as word ]; -$unitID = 0; +$unitID = 1; $packet = new ReadWriteMultipleRegistersRequest( $readStartAddress, $readQuantity, diff --git a/examples/fc3.php b/examples/fc3.php index 64165e6..c8e7cb1 100644 --- a/examples/fc3.php +++ b/examples/fc3.php @@ -22,7 +22,7 @@ $startAddress = 256; $quantity = 6; -$unitID = 0; +$unitID = 1; $packet = new ReadHoldingRegistersRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! try { diff --git a/examples/fc4.php b/examples/fc4.php index dfcc76f..749be33 100644 --- a/examples/fc4.php +++ b/examples/fc4.php @@ -16,7 +16,7 @@ $startAddress = 256; $quantity = 6; -$unitID = 0; +$unitID = 1; $packet = new ReadInputRegistersRequest($startAddress, $quantity, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/examples/fc5.php b/examples/fc5.php index d3b1d04..456a537 100644 --- a/examples/fc5.php +++ b/examples/fc5.php @@ -16,7 +16,7 @@ $startAddress = 12288; $coil = true; -$unitID = 0; +$unitID = 1; $packet = new WriteSingleCoilRequest($startAddress, $coil, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/examples/fc6.php b/examples/fc6.php index aefc751..4c96e5d 100644 --- a/examples/fc6.php +++ b/examples/fc6.php @@ -16,7 +16,7 @@ $startAddress = 12288; $value = 55; -$unitID = 0; +$unitID = 1; $packet = new WriteSingleRegisterRequest($startAddress, $value, $unitID); // NB: This is Modbus TCP packet not Modbus RTU over TCP! echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL; diff --git a/src/Packet/ModbusApplicationHeader.php b/src/Packet/ModbusApplicationHeader.php index a346d37..d12efe2 100644 --- a/src/Packet/ModbusApplicationHeader.php +++ b/src/Packet/ModbusApplicationHeader.php @@ -117,8 +117,12 @@ private function validate(int $length, int $unitId, int|null $transactionId): vo if (!$length || !($length > 0 && $length <= Types::MAX_VALUE_UINT16)) { throw new InvalidArgumentException("length is not set or out of range (uint16): {$length}"); } - if (!($unitId >= 0 && $unitId <= 247)) { - throw new InvalidArgumentException("unitId is out of range (0-247): {$unitId}"); + if (!($unitId >= 0 && $unitId <= 255)) { + // Older MODBUS specification limited unit ID (slave id) in range of 0-247) + // See "Modicon Modbus Protocol Reference Guide PI–MBUS–300 Rev. J" page 19 + // + // but newer spec has that limitation removed. + throw new InvalidArgumentException("unitId is out of range (0-255): {$unitId}"); } if ((null !== $transactionId) && !($transactionId >= 0 && $transactionId <= Types::MAX_VALUE_UINT16)) { throw new InvalidArgumentException("transactionId is out of range (uint16): {$transactionId}"); diff --git a/tests/unit/Packet/ModbusApplicationHeaderTest.php b/tests/unit/Packet/ModbusApplicationHeaderTest.php index 6f1dc3f..b169c4f 100644 --- a/tests/unit/Packet/ModbusApplicationHeaderTest.php +++ b/tests/unit/Packet/ModbusApplicationHeaderTest.php @@ -82,15 +82,15 @@ public function testLengthOverFlow() public function testUnitIdOverFlow() { - $this->expectExceptionMessage("unitId is out of range (0-247): 248"); + $this->expectExceptionMessage("unitId is out of range (0-255): 256"); $this->expectException(InvalidArgumentException::class); - new ModbusApplicationHeader(1, 248, 1); + new ModbusApplicationHeader(1, 256, 1); } public function testUnitIdUnderFlow() { - $this->expectExceptionMessage("unitId is out of range (0-247): -1"); + $this->expectExceptionMessage("unitId is out of range (0-255): -1"); $this->expectException(InvalidArgumentException::class); new ModbusApplicationHeader(1, -1, 1); diff --git a/tests/unit/Packet/ProtocolDataUnitTest.php b/tests/unit/Packet/ProtocolDataUnitTest.php index 486ed59..4407484 100644 --- a/tests/unit/Packet/ProtocolDataUnitTest.php +++ b/tests/unit/Packet/ProtocolDataUnitTest.php @@ -22,7 +22,7 @@ public function testValidation() public function testFailWithNegativeUnitID() { - $this->expectExceptionMessage("unitId is out of range (0-247): -1"); + $this->expectExceptionMessage("unitId is out of range (0-255): -1"); $this->expectException(InvalidArgumentException::class); new ProtocolDataUnitImpl(-1); diff --git a/tests/unit/Utils/TypesTest.php b/tests/unit/Utils/TypesTest.php index ee8e619..5c3ff32 100644 --- a/tests/unit/Utils/TypesTest.php +++ b/tests/unit/Utils/TypesTest.php @@ -340,7 +340,7 @@ public function testShouldSeeIfBitIsNotSet() public function testShouldExceptionWhenBitToHighNumber() { - $this->expectErrorMessageMatches("/On .*bit PHP bit shifting more than .* bit is not possible as int size is .* bytes/"); + $this->expectExceptionMessageMatches("/On .*bit PHP bit shifting more than .* bit is not possible as int size is .* bytes/"); $this->expectException(InvalidArgumentException::class); if (PHP_INT_SIZE === 4) {