diff --git a/examples/index.php b/examples/index.php index 49820d9..f0126e0 100644 --- a/examples/index.php +++ b/examples/index.php @@ -15,7 +15,11 @@ // sudo udevadm control --reload-rules && sudo udevadm trigger $deviceURI = '/dev/ttyUSB0'; // do not make this changeable from WEB. This could be serious security risk. $isSerialDevice = false; // change to true to enable reading serial devices. this will disable ip/port logic and uses RTU -if (getenv('MODBUS_SERIAL_ENABLED')) { // can be set from Nginx/Apache fast-cgi conf +if (getenv('MODBUS_SERIAL_ENABLED')) { + // can be set from Nginx/Apache fast-cgi conf + // for Nginx add these lines where you PHP is configured: + // fastcgi_param MODBUS_SERIAL_ENABLED true; + // fastcgi_param MODBUS_SERIAL_DEVICE /dev/ttyUSB0; $isSerialDevice = filter_var(getenv('MODBUS_SERIAL_ENABLED'), FILTER_VALIDATE_BOOLEAN); if ($isSerialDevice && getenv('MODBUS_SERIAL_DEVICE')) { $deviceURI = getenv('MODBUS_SERIAL_DEVICE'); diff --git a/src/Packet/ModbusApplicationHeader.php b/src/Packet/ModbusApplicationHeader.php index 7d4372e..a346d37 100644 --- a/src/Packet/ModbusApplicationHeader.php +++ b/src/Packet/ModbusApplicationHeader.php @@ -6,6 +6,7 @@ use ModbusTcpClient\Exception\InvalidArgumentException; use ModbusTcpClient\Exception\ModbusException; +use ModbusTcpClient\Utils\Endian; use ModbusTcpClient\Utils\Types; /** @@ -100,8 +101,8 @@ public static function parse(string $binaryString): ModbusApplicationHeader throw new ModbusException('Data length too short to be valid header!'); } - $transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1]); - $length = Types::parseUInt16($binaryString[4] . $binaryString[5]); + $transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1], Endian::BIG_ENDIAN); + $length = Types::parseUInt16($binaryString[4] . $binaryString[5], Endian::BIG_ENDIAN); $unitId = Types::parseByte($binaryString[6]); return new ModbusApplicationHeader( diff --git a/src/Packet/ModbusFunction/MaskWriteRegisterRequest.php b/src/Packet/ModbusFunction/MaskWriteRegisterRequest.php index d76e84f..540f269 100644 --- a/src/Packet/ModbusFunction/MaskWriteRegisterRequest.php +++ b/src/Packet/ModbusFunction/MaskWriteRegisterRequest.php @@ -93,7 +93,7 @@ public function getORMask(): int */ public function getANDMaskAsWord(): Word { - return new Word(Types::toInt16($this->andMask)); + return new Word(Types::toInt16($this->andMask, Endian::BIG_ENDIAN)); } /** @@ -101,7 +101,7 @@ public function getANDMaskAsWord(): Word */ public function getORMaskAsWord(): Word { - return new Word(Types::toInt16($this->orMask)); + return new Word(Types::toInt16($this->orMask, Endian::BIG_ENDIAN)); } protected function getLengthInternal(): int diff --git a/src/Packet/ModbusFunction/MaskWriteRegisterResponse.php b/src/Packet/ModbusFunction/MaskWriteRegisterResponse.php index ae2bee8..5201ef9 100644 --- a/src/Packet/ModbusFunction/MaskWriteRegisterResponse.php +++ b/src/Packet/ModbusFunction/MaskWriteRegisterResponse.php @@ -80,7 +80,7 @@ public function getORMask(): int */ public function getANDMaskAsWord(): Word { - return new Word(Types::toInt16($this->andMask)); + return new Word(Types::toInt16($this->andMask, Endian::BIG_ENDIAN)); } /** @@ -88,6 +88,6 @@ public function getANDMaskAsWord(): Word */ public function getORMaskAsWord(): Word { - return new Word(Types::toInt16($this->orMask)); + return new Word(Types::toInt16($this->orMask, Endian::BIG_ENDIAN)); } } diff --git a/src/Packet/ProtocolDataUnitRequest.php b/src/Packet/ProtocolDataUnitRequest.php index 3d72d9c..76b83dc 100644 --- a/src/Packet/ProtocolDataUnitRequest.php +++ b/src/Packet/ProtocolDataUnitRequest.php @@ -33,7 +33,7 @@ public function __toString(): string return b'' . $this->getHeader()->__toString() . Types::toByte($this->getFunctionCode()) - . Types::toUint16($this->getStartAddress()); + . Types::toUint16($this->getStartAddress(), Endian::BIG_ENDIAN); } public function getStartAddress(): int diff --git a/src/Packet/ResponseFactory.php b/src/Packet/ResponseFactory.php index e9de28d..d4a7887 100644 --- a/src/Packet/ResponseFactory.php +++ b/src/Packet/ResponseFactory.php @@ -16,6 +16,7 @@ use ModbusTcpClient\Packet\ModbusFunction\WriteMultipleRegistersResponse; use ModbusTcpClient\Packet\ModbusFunction\WriteSingleCoilResponse; use ModbusTcpClient\Packet\ModbusFunction\WriteSingleRegisterResponse; +use ModbusTcpClient\Utils\Endian; use ModbusTcpClient\Utils\Types; class ResponseFactory @@ -40,7 +41,7 @@ public static function parseResponse(string|null $binaryString): ModbusResponse| return new ErrorResponse(ModbusApplicationHeader::parse($binaryString), $functionCode, $exceptionCode); } - $transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1]); + $transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1], Endian::BIG_ENDIAN); $unitId = Types::parseByte($binaryString[6]); $rawData = substr($binaryString, 8); diff --git a/src/Packet/StartAddressResponse.php b/src/Packet/StartAddressResponse.php index 4e2ff5a..ff7efa2 100644 --- a/src/Packet/StartAddressResponse.php +++ b/src/Packet/StartAddressResponse.php @@ -4,6 +4,7 @@ namespace ModbusTcpClient\Packet; +use ModbusTcpClient\Utils\Endian; use ModbusTcpClient\Utils\Types; abstract class StartAddressResponse extends ProtocolDataUnit implements ModbusResponse @@ -16,7 +17,7 @@ abstract class StartAddressResponse extends ProtocolDataUnit implements ModbusRe public function __construct(string $rawData, int $unitId = 0, int $transactionId = null) { parent::__construct($unitId, $transactionId); - $this->startAddress = Types::parseUInt16(substr($rawData, 0, 2)); + $this->startAddress = Types::parseUInt16(substr($rawData, 0, 2), Endian::BIG_ENDIAN); } public function __toString(): string diff --git a/tests/unit/Packet/ModbusFunction/MaskWriteRegisterRequestTest.php b/tests/unit/Packet/ModbusFunction/MaskWriteRegisterRequestTest.php index 907146e..e7fe51b 100644 --- a/tests/unit/Packet/ModbusFunction/MaskWriteRegisterRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/MaskWriteRegisterRequestTest.php @@ -7,11 +7,22 @@ use ModbusTcpClient\Packet\ModbusFunction\MaskWriteRegisterRequest; use ModbusTcpClient\Packet\ModbusPacket; use ModbusTcpClient\Packet\Word; +use ModbusTcpClient\Utils\Endian; use ModbusTcpClient\Utils\Types; use PHPUnit\Framework\TestCase; class MaskWriteRegisterRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { // Field: Size in packet diff --git a/tests/unit/Packet/ModbusFunction/MaskWriteRegisterResponseTest.php b/tests/unit/Packet/ModbusFunction/MaskWriteRegisterResponseTest.php index 688ffff..eff852d 100644 --- a/tests/unit/Packet/ModbusFunction/MaskWriteRegisterResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/MaskWriteRegisterResponseTest.php @@ -5,10 +5,21 @@ use ModbusTcpClient\Packet\ModbusFunction\MaskWriteRegisterResponse; use ModbusTcpClient\Packet\ModbusPacket; use ModbusTcpClient\Packet\Word; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class MaskWriteRegisterResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { // Field: Size in packet diff --git a/tests/unit/Packet/ModbusFunction/ReadCoilsRequestTest.php b/tests/unit/Packet/ModbusFunction/ReadCoilsRequestTest.php index e06f76d..128b3ac 100644 --- a/tests/unit/Packet/ModbusFunction/ReadCoilsRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadCoilsRequestTest.php @@ -6,10 +6,21 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\ReadCoilsRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadCoilsRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadCoilsResponseTest.php b/tests/unit/Packet/ModbusFunction/ReadCoilsResponseTest.php index 25cd788..6d4ddd0 100644 --- a/tests/unit/Packet/ModbusFunction/ReadCoilsResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadCoilsResponseTest.php @@ -7,10 +7,21 @@ use ModbusTcpClient\Exception\ParseException; use ModbusTcpClient\Packet\ModbusFunction\ReadCoilsResponse; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadCoilsResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( @@ -18,6 +29,7 @@ public function testPacketToString() (new ReadCoilsResponse("\x02\xCD\x6B", 3, 33152))->__toString() ); } + public function testToHex() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersRequestTest.php b/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersRequestTest.php index 6e85e98..e40edd5 100644 --- a/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersRequestTest.php @@ -6,6 +6,7 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\ReadHoldingRegistersRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; /* @@ -25,6 +26,16 @@ class ReadHoldingRegistersRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersResponseTest.php b/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersResponseTest.php index 6e40af2..c0d387e 100644 --- a/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadHoldingRegistersResponseTest.php @@ -7,10 +7,21 @@ use ModbusTcpClient\Exception\ParseException; use ModbusTcpClient\Packet\ModbusFunction\ReadHoldingRegistersResponse; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadHoldingRegistersResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( @@ -341,7 +352,7 @@ public function testGetAsciiString() $packet = (new ReadHoldingRegistersResponse("\x08\x01\x00\xF8\x53\x65\x72\x00\x6E", 3, 33152))->withStartAddress(50); $this->assertCount(4, $packet->getWords()); - $this->assertEquals('Søren', $packet->getAsciiStringAt(51, 5)); + $this->assertEquals('Søren', $packet->getAsciiStringAt(51, 5, Endian::BIG_ENDIAN_LOW_WORD_FIRST)); } public function testGetAsciiStringInvalidAddressLow() diff --git a/tests/unit/Packet/ModbusFunction/ReadInputDiscretesRequestTest.php b/tests/unit/Packet/ModbusFunction/ReadInputDiscretesRequestTest.php index 1485e9c..dce3099 100644 --- a/tests/unit/Packet/ModbusFunction/ReadInputDiscretesRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadInputDiscretesRequestTest.php @@ -5,10 +5,21 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\ReadInputDiscretesRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadInputDiscretesRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadInputDiscretesResponseTest.php b/tests/unit/Packet/ModbusFunction/ReadInputDiscretesResponseTest.php index 1588993..ce1fe7b 100644 --- a/tests/unit/Packet/ModbusFunction/ReadInputDiscretesResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadInputDiscretesResponseTest.php @@ -5,10 +5,21 @@ use ModbusTcpClient\Exception\ParseException; use ModbusTcpClient\Packet\ModbusFunction\ReadInputDiscretesResponse; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadInputDiscretesResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadInputRegistersRequestTest.php b/tests/unit/Packet/ModbusFunction/ReadInputRegistersRequestTest.php index 9f4291d..5b5ae24 100644 --- a/tests/unit/Packet/ModbusFunction/ReadInputRegistersRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadInputRegistersRequestTest.php @@ -6,10 +6,21 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\ReadInputRegistersRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadInputRegistersRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadInputRegistersResponseTest.php b/tests/unit/Packet/ModbusFunction/ReadInputRegistersResponseTest.php index c68f98e..d29e95f 100644 --- a/tests/unit/Packet/ModbusFunction/ReadInputRegistersResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadInputRegistersResponseTest.php @@ -5,10 +5,21 @@ use ModbusTcpClient\Exception\ParseException; use ModbusTcpClient\Packet\ModbusFunction\ReadInputRegistersResponse; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadInputRegistersResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersRequestTest.php b/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersRequestTest.php index 50c8edc..e47ff5f 100644 --- a/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersRequestTest.php @@ -6,11 +6,22 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\ReadWriteMultipleRegistersRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use ModbusTcpClient\Utils\Types; use PHPUnit\Framework\TestCase; class ReadWriteMultipleRegistersRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { // Field: Size in packet @@ -32,7 +43,7 @@ public function testOnPacketToString() 0x0410, 1, 0x0112, - [Types::toByte(200), Types::toInt16(130)], + [Types::toByte(200), Types::toInt16(130, Endian::BIG_ENDIAN_LOW_WORD_FIRST)], 0x11, 0x0138 ))->__toString() @@ -176,7 +187,7 @@ public function testParse() 0x0410, 1, 0x0112, - [Types::toByte(200), Types::toInt16(130)], + [Types::toByte(200), Types::toInt16(130, Endian::BIG_ENDIAN_LOW_WORD_FIRST)], 0x11, 0x0138 ))->__toString()); diff --git a/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersResponseTest.php b/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersResponseTest.php index 89d75c7..bf42637 100644 --- a/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/ReadWriteMultipleRegistersResponseTest.php @@ -7,10 +7,21 @@ use ModbusTcpClient\Exception\ParseException; use ModbusTcpClient\Packet\ModbusFunction\ReadWriteMultipleRegistersResponse; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class ReadWriteMultipleRegistersResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( @@ -341,7 +352,7 @@ public function testGetAsciiString() $packet = (new ReadWriteMultipleRegistersResponse("\x08\x01\x00\xF8\x53\x65\x72\x00\x6E", 3, 33152))->withStartAddress(50); $this->assertCount(4, $packet->getWords()); - $this->assertEquals('Søren', $packet->getAsciiStringAt(51, 5)); + $this->assertEquals('Søren', $packet->getAsciiStringAt(51, 5, Endian::BIG_ENDIAN_LOW_WORD_FIRST)); } public function testGetAsciiStringInvalidAddressLow() diff --git a/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsRequestTest.php b/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsRequestTest.php index cbdc5c4..8162cca 100644 --- a/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsRequestTest.php @@ -6,10 +6,21 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\WriteMultipleCoilsRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class WriteMultipleCoilsRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsResponseTest.php b/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsResponseTest.php index 683e88d..02f1508 100644 --- a/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteMultipleCoilsResponseTest.php @@ -3,12 +3,23 @@ namespace Tests\Packet\ModbusFunction; -use ModbusTcpClient\Packet\ModbusPacket; use ModbusTcpClient\Packet\ModbusFunction\WriteMultipleCoilsResponse; +use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class WriteMultipleCoilsResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersRequestTest.php b/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersRequestTest.php index f95905f..1a24596 100644 --- a/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersRequestTest.php @@ -6,11 +6,22 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\WriteMultipleRegistersRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use ModbusTcpClient\Utils\Types; use PHPUnit\Framework\TestCase; class WriteMultipleRegistersRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { // Field: Size in packet @@ -27,7 +38,11 @@ public function testPacketToString() // registers: 00c8 0082 8701 (3 registers = 6 bytes) $this->assertEquals( "\x01\x38\x00\x00\x00\x0d\x11\x10\x04\x10\x00\x03\x06\x00\xC8\x00\x82\x87\x01", - (new WriteMultipleRegistersRequest(0x0410, [Types::toByte(200), Types::toInt16(130), Types::toUint16(34561)], 0x11, 0x0138))->__toString() + (new WriteMultipleRegistersRequest(0x0410, [ + Types::toByte(200), + Types::toInt16(130, Endian::BIG_ENDIAN_LOW_WORD_FIRST), + Types::toUint16(34561, Endian::BIG_ENDIAN_LOW_WORD_FIRST) + ], 0x11, 0x0138))->__toString() ); } @@ -57,7 +72,11 @@ public function testPacketProperties() public function testParse() { $packet = WriteMultipleRegistersRequest::parse("\x01\x38\x00\x00\x00\x0d\x11\x10\x04\x10\x00\x03\x06\x00\xC8\x00\x82\x87\x01"); - $this->assertEquals($packet, (new WriteMultipleRegistersRequest(0x0410, [Types::toByte(200), Types::toInt16(130), Types::toUint16(34561)], 0x11, 0x0138))->__toString()); + $this->assertEquals($packet, (new WriteMultipleRegistersRequest(0x0410, [ + Types::toByte(200), + Types::toInt16(130, Endian::BIG_ENDIAN_LOW_WORD_FIRST), + Types::toUint16(34561, Endian::BIG_ENDIAN_LOW_WORD_FIRST) + ], 0x11, 0x0138))->__toString()); $this->assertEquals(0x0410, $packet->getStartAddress()); $this->assertEquals(["\x00\xC8", "\x00\x82", "\x87\x01"], $packet->getRegisters()); diff --git a/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersResponseTest.php b/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersResponseTest.php index 4447379..d220dab 100644 --- a/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteMultipleRegistersResponseTest.php @@ -3,12 +3,23 @@ namespace Tests\Packet\ModbusFunction; -use ModbusTcpClient\Packet\ModbusPacket; use ModbusTcpClient\Packet\ModbusFunction\WriteMultipleRegistersResponse; +use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class WriteMultipleRegistersResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/WriteSingleCoilRequestTest.php b/tests/unit/Packet/ModbusFunction/WriteSingleCoilRequestTest.php index b4606ae..e4f84ae 100644 --- a/tests/unit/Packet/ModbusFunction/WriteSingleCoilRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteSingleCoilRequestTest.php @@ -5,10 +5,21 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\WriteSingleCoilRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class WriteSingleCoilRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testOnPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/WriteSingleCoilResponseTest.php b/tests/unit/Packet/ModbusFunction/WriteSingleCoilResponseTest.php index bf5ec11..c28611e 100644 --- a/tests/unit/Packet/ModbusFunction/WriteSingleCoilResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteSingleCoilResponseTest.php @@ -1,13 +1,25 @@ assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/WriteSingleRegisterRequestTest.php b/tests/unit/Packet/ModbusFunction/WriteSingleRegisterRequestTest.php index 31eb838..02d9f6e 100644 --- a/tests/unit/Packet/ModbusFunction/WriteSingleRegisterRequestTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteSingleRegisterRequestTest.php @@ -6,10 +6,21 @@ use ModbusTcpClient\Packet\ErrorResponse; use ModbusTcpClient\Packet\ModbusFunction\WriteSingleRegisterRequest; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class WriteSingleRegisterRequestTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( diff --git a/tests/unit/Packet/ModbusFunction/WriteSingleRegisterResponseTest.php b/tests/unit/Packet/ModbusFunction/WriteSingleRegisterResponseTest.php index a23c794..617e018 100644 --- a/tests/unit/Packet/ModbusFunction/WriteSingleRegisterResponseTest.php +++ b/tests/unit/Packet/ModbusFunction/WriteSingleRegisterResponseTest.php @@ -5,10 +5,21 @@ use ModbusTcpClient\Packet\ModbusFunction\WriteSingleRegisterResponse; use ModbusTcpClient\Packet\ModbusPacket; +use ModbusTcpClient\Utils\Endian; use PHPUnit\Framework\TestCase; class WriteSingleRegisterResponseTest extends TestCase { + protected function setUp(): void + { + Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output + } + + protected function tearDown(): void + { + Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST; + } + public function testPacketToString() { $this->assertEquals( @@ -22,7 +33,7 @@ public function testOnPacketProperties() $packet = new WriteSingleRegisterResponse("\x00\x02\xFF\x00", 3, 33152); $this->assertEquals(ModbusPacket::WRITE_SINGLE_REGISTER, $packet->getFunctionCode()); - $this->assertEquals(0xFF00, $packet->getWord()->getUInt16()); + $this->assertEquals(0xFF00, $packet->getWord()->getUInt16(Endian::BIG_ENDIAN_LOW_WORD_FIRST)); $header = $packet->getHeader(); $this->assertEquals(33152, $header->getTransactionId());