-
Notifications
You must be signed in to change notification settings - Fork 55
/
MaskWriteRegisterResponse.php
93 lines (81 loc) · 2.2 KB
/
MaskWriteRegisterResponse.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\StartAddressResponse;
use ModbusTcpClient\Packet\Word;
use ModbusTcpClient\Utils\Endian;
use ModbusTcpClient\Utils\Types;
/**
* Response for Mask Write Register (FC=22)
*
* Example packet: \x81\x80\x00\x00\x00\x08\x10\x16\x00\x04\x00\xF2\x00\x25
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x08 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x10 - unit id
* \x16 - function code
* \x00\x04 - start address
* \x00\xF2 - AND mask
* \x00\x25 - OR mask
*
*/
class MaskWriteRegisterResponse extends StartAddressResponse
{
/**
* @var int AND mask to be sent to modbus
*/
private int $andMask;
/**
* @var int OR mask to be sent to modbus
*/
private int $orMask;
public function __construct(string $rawData, int $unitId = 0, int $transactionId = null)
{
parent::__construct($rawData, $unitId, $transactionId);
$this->andMask = Types::parseUInt16(substr($rawData, 2, 2), Endian::BIG_ENDIAN);
$this->orMask = Types::parseUInt16(substr($rawData, 4, 2), Endian::BIG_ENDIAN);
}
public function getFunctionCode(): int
{
return ModbusPacket::MASK_WRITE_REGISTER;
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + 4; //register is 4 bytes
}
public function __toString(): string
{
return parent::__toString()
. Types::toRegister($this->getANDMask())
. Types::toRegister($this->getORMask());
}
/**
* @return int
*/
public function getANDMask(): int
{
return $this->andMask;
}
/**
* @return int
*/
public function getORMask(): int
{
return $this->orMask;
}
/**
* @return Word
*/
public function getANDMaskAsWord(): Word
{
return new Word(Types::toInt16($this->andMask, Endian::BIG_ENDIAN));
}
/**
* @return Word
*/
public function getORMaskAsWord(): Word
{
return new Word(Types::toInt16($this->orMask, Endian::BIG_ENDIAN));
}
}