-
Notifications
You must be signed in to change notification settings - Fork 55
/
WriteSingleRegisterResponse.php
56 lines (48 loc) · 1.33 KB
/
WriteSingleRegisterResponse.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
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\StartAddressResponse;
use ModbusTcpClient\Packet\Word;
/**
* Response for Write Single Register (FC=06)
*
* Example packet: \x81\x80\x00\x00\x00\x06\x03\x06\x00\x02\xFF\x00
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x03 - unit id
* \x06 - function code
* \x00\x02 - start address
* \xFF\x00 - register data
*
*/
class WriteSingleRegisterResponse extends StartAddressResponse
{
/**
* @var Word
*/
private Word $word;
public function __construct(string $rawData, int $unitId = 0, int $transactionId = null)
{
parent::__construct($rawData, $unitId, $transactionId);
$this->word = new Word(substr($rawData, 2, 2));
}
public function getFunctionCode(): int
{
return ModbusPacket::WRITE_SINGLE_REGISTER;
}
public function getWord(): Word
{
return $this->word;
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + 2; //register is 2 bytes
}
public function __toString(): string
{
return parent::__toString()
. $this->word->getData();
}
}