-
Notifications
You must be signed in to change notification settings - Fork 56
/
ReadInputRegistersRequest.php
49 lines (45 loc) · 1.52 KB
/
ReadInputRegistersRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Packet\ErrorResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Utils\Endian;
use ModbusTcpClient\Utils\Types;
/**
* Request for Read Input Registers (FC=04)
*
* Example packet: \x00\x01\x00\x00\x00\x06\x01\x04\x00\x6B\x00\x01
* \x00\x01 - transaction id
* \x00\x00 - protocol id
* \x00\x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x01 - unit id
* \x04 - function code
* \x00\x6B - start address
* \x00\x01 - input registers quantity to return
*
*/
class ReadInputRegistersRequest extends ReadHoldingRegistersRequest
{
public function getFunctionCode(): int
{
return ModbusPacket::READ_INPUT_REGISTERS;
}
/**
* Parses binary string to ReadInputRegistersRequest or return ErrorResponse on failure
*
* @param string $binaryString
* @return ReadInputRegistersRequest|ErrorResponse
*/
public static function parse(string $binaryString): ReadInputRegistersRequest|ErrorResponse
{
return self::parseStartAddressPacket(
$binaryString,
12,
ModbusPacket::READ_INPUT_REGISTERS,
function (int $transactionId, int $unitId, int $startAddress) use ($binaryString) {
$quantity = Types::parseUInt16($binaryString[10] . $binaryString[11], Endian::BIG_ENDIAN);
return new self($startAddress, $quantity, $unitId, $transactionId);
}
);
}
}