Skip to content

Commit dc10ec7

Browse files
committed
fix setting endian before creating request will create invalid request
1 parent 8ccb744 commit dc10ec7

27 files changed

+257
-20
lines changed

examples/index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
// sudo udevadm control --reload-rules && sudo udevadm trigger
1616
$deviceURI = '/dev/ttyUSB0'; // do not make this changeable from WEB. This could be serious security risk.
1717
$isSerialDevice = false; // change to true to enable reading serial devices. this will disable ip/port logic and uses RTU
18-
if (getenv('MODBUS_SERIAL_ENABLED')) { // can be set from Nginx/Apache fast-cgi conf
18+
if (getenv('MODBUS_SERIAL_ENABLED')) {
19+
// can be set from Nginx/Apache fast-cgi conf
20+
// for Nginx add these lines where you PHP is configured:
21+
// fastcgi_param MODBUS_SERIAL_ENABLED true;
22+
// fastcgi_param MODBUS_SERIAL_DEVICE /dev/ttyUSB0;
1923
$isSerialDevice = filter_var(getenv('MODBUS_SERIAL_ENABLED'), FILTER_VALIDATE_BOOLEAN);
2024
if ($isSerialDevice && getenv('MODBUS_SERIAL_DEVICE')) {
2125
$deviceURI = getenv('MODBUS_SERIAL_DEVICE');

src/Packet/ModbusApplicationHeader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ModbusTcpClient\Exception\InvalidArgumentException;
88
use ModbusTcpClient\Exception\ModbusException;
9+
use ModbusTcpClient\Utils\Endian;
910
use ModbusTcpClient\Utils\Types;
1011

1112
/**
@@ -100,8 +101,8 @@ public static function parse(string $binaryString): ModbusApplicationHeader
100101
throw new ModbusException('Data length too short to be valid header!');
101102
}
102103

103-
$transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1]);
104-
$length = Types::parseUInt16($binaryString[4] . $binaryString[5]);
104+
$transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1], Endian::BIG_ENDIAN);
105+
$length = Types::parseUInt16($binaryString[4] . $binaryString[5], Endian::BIG_ENDIAN);
105106
$unitId = Types::parseByte($binaryString[6]);
106107

107108
return new ModbusApplicationHeader(

src/Packet/ModbusFunction/MaskWriteRegisterRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ public function getORMask(): int
9393
*/
9494
public function getANDMaskAsWord(): Word
9595
{
96-
return new Word(Types::toInt16($this->andMask));
96+
return new Word(Types::toInt16($this->andMask, Endian::BIG_ENDIAN));
9797
}
9898

9999
/**
100100
* @return Word
101101
*/
102102
public function getORMaskAsWord(): Word
103103
{
104-
return new Word(Types::toInt16($this->orMask));
104+
return new Word(Types::toInt16($this->orMask, Endian::BIG_ENDIAN));
105105
}
106106

107107
protected function getLengthInternal(): int

src/Packet/ModbusFunction/MaskWriteRegisterResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ public function getORMask(): int
8080
*/
8181
public function getANDMaskAsWord(): Word
8282
{
83-
return new Word(Types::toInt16($this->andMask));
83+
return new Word(Types::toInt16($this->andMask, Endian::BIG_ENDIAN));
8484
}
8585

8686
/**
8787
* @return Word
8888
*/
8989
public function getORMaskAsWord(): Word
9090
{
91-
return new Word(Types::toInt16($this->orMask));
91+
return new Word(Types::toInt16($this->orMask, Endian::BIG_ENDIAN));
9292
}
9393
}

src/Packet/ProtocolDataUnitRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __toString(): string
3333
return b''
3434
. $this->getHeader()->__toString()
3535
. Types::toByte($this->getFunctionCode())
36-
. Types::toUint16($this->getStartAddress());
36+
. Types::toUint16($this->getStartAddress(), Endian::BIG_ENDIAN);
3737
}
3838

3939
public function getStartAddress(): int

src/Packet/ResponseFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ModbusTcpClient\Packet\ModbusFunction\WriteMultipleRegistersResponse;
1717
use ModbusTcpClient\Packet\ModbusFunction\WriteSingleCoilResponse;
1818
use ModbusTcpClient\Packet\ModbusFunction\WriteSingleRegisterResponse;
19+
use ModbusTcpClient\Utils\Endian;
1920
use ModbusTcpClient\Utils\Types;
2021

2122
class ResponseFactory
@@ -40,7 +41,7 @@ public static function parseResponse(string|null $binaryString): ModbusResponse|
4041
return new ErrorResponse(ModbusApplicationHeader::parse($binaryString), $functionCode, $exceptionCode);
4142
}
4243

43-
$transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1]);
44+
$transactionId = Types::parseUInt16($binaryString[0] . $binaryString[1], Endian::BIG_ENDIAN);
4445
$unitId = Types::parseByte($binaryString[6]);
4546

4647
$rawData = substr($binaryString, 8);

src/Packet/StartAddressResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace ModbusTcpClient\Packet;
55

66

7+
use ModbusTcpClient\Utils\Endian;
78
use ModbusTcpClient\Utils\Types;
89

910
abstract class StartAddressResponse extends ProtocolDataUnit implements ModbusResponse
@@ -16,7 +17,7 @@ abstract class StartAddressResponse extends ProtocolDataUnit implements ModbusRe
1617
public function __construct(string $rawData, int $unitId = 0, int $transactionId = null)
1718
{
1819
parent::__construct($unitId, $transactionId);
19-
$this->startAddress = Types::parseUInt16(substr($rawData, 0, 2));
20+
$this->startAddress = Types::parseUInt16(substr($rawData, 0, 2), Endian::BIG_ENDIAN);
2021
}
2122

2223
public function __toString(): string

tests/unit/Packet/ModbusFunction/MaskWriteRegisterRequestTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@
77
use ModbusTcpClient\Packet\ModbusFunction\MaskWriteRegisterRequest;
88
use ModbusTcpClient\Packet\ModbusPacket;
99
use ModbusTcpClient\Packet\Word;
10+
use ModbusTcpClient\Utils\Endian;
1011
use ModbusTcpClient\Utils\Types;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class MaskWriteRegisterRequestTest extends TestCase
1415
{
16+
protected function setUp(): void
17+
{
18+
Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output
19+
}
20+
21+
protected function tearDown(): void
22+
{
23+
Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST;
24+
}
25+
1526
public function testOnPacketToString()
1627
{
1728
// Field: Size in packet

tests/unit/Packet/ModbusFunction/MaskWriteRegisterResponseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55
use ModbusTcpClient\Packet\ModbusFunction\MaskWriteRegisterResponse;
66
use ModbusTcpClient\Packet\ModbusPacket;
77
use ModbusTcpClient\Packet\Word;
8+
use ModbusTcpClient\Utils\Endian;
89
use PHPUnit\Framework\TestCase;
910

1011
class MaskWriteRegisterResponseTest extends TestCase
1112
{
13+
protected function setUp(): void
14+
{
15+
Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output
16+
}
17+
18+
protected function tearDown(): void
19+
{
20+
Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST;
21+
}
22+
1223
public function testOnPacketToString()
1324
{
1425
// Field: Size in packet

tests/unit/Packet/ModbusFunction/ReadCoilsRequestTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66
use ModbusTcpClient\Packet\ErrorResponse;
77
use ModbusTcpClient\Packet\ModbusFunction\ReadCoilsRequest;
88
use ModbusTcpClient\Packet\ModbusPacket;
9+
use ModbusTcpClient\Utils\Endian;
910
use PHPUnit\Framework\TestCase;
1011

1112
class ReadCoilsRequestTest extends TestCase
1213
{
14+
protected function setUp(): void
15+
{
16+
Endian::$defaultEndian = Endian::LITTLE_ENDIAN; // packets are big endian. setting to default to little should not change output
17+
}
18+
19+
protected function tearDown(): void
20+
{
21+
Endian::$defaultEndian = Endian::BIG_ENDIAN_LOW_WORD_FIRST;
22+
}
23+
1324
public function testPacketToString()
1425
{
1526
$this->assertEquals(

0 commit comments

Comments
 (0)