-
Notifications
You must be signed in to change notification settings - Fork 57
/
ReportServerIDRequest.php
63 lines (56 loc) · 1.7 KB
/
ReportServerIDRequest.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Packet\ErrorResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\ModbusRequest;
use ModbusTcpClient\Packet\ProtocolDataUnit;
use ModbusTcpClient\Utils\Types;
/**
* Request for Report Server ID (FC=17, 0x11)
*
* Example packet: \x81\x80\x00\x00\x00\x02\x10\x11
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x08 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x10 - unit id
* \x11 - function code
*
*/
class ReportServerIDRequest extends ProtocolDataUnit implements ModbusRequest
{
public function __construct(int $unitId = 0, ?int $transactionId = null)
{
parent::__construct($unitId, $transactionId);
}
public function getFunctionCode(): int
{
return ModbusPacket::REPORT_SERVER_ID; // 17 (0x11)
}
protected function getLengthInternal(): int
{
return 1; // size of function code (1 byte)
}
public function __toString(): string
{
return b''
. $this->getHeader()->__toString()
. Types::toByte($this->getFunctionCode());
}
/**
* Parses binary string to ReportServerIDRequest or return ErrorResponse on failure
*
* @param string $binaryString
* @return ReportServerIDRequest|ErrorResponse
*/
public static function parse(string $binaryString): ReportServerIDRequest|ErrorResponse
{
return self::parsePacket(
$binaryString,
8,
ModbusPacket::REPORT_SERVER_ID,
function (int $transactionId, int $unitId) {
return new self($unitId, $transactionId);
}
);
}
}