/
ReadHoldingRegistersResponse.php
220 lines (194 loc) · 6.62 KB
/
ReadHoldingRegistersResponse.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Exception\InvalidArgumentException;
use ModbusTcpClient\Exception\ModbusException;
use ModbusTcpClient\Packet\ByteCountResponse;
use ModbusTcpClient\Packet\DoubleWord;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\QuadWord;
use ModbusTcpClient\Packet\Word;
use ModbusTcpClient\Utils\Types;
use Traversable;
/**
* Response for Read Holding Registers (FC=03)
*
* Example packet: \x81\x80\x00\x00\x00\x05\x01\x03\x02\xCD\x6B
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x01 - unit id
* \x03 - function code
* \x02 - returned registers byte count
* \xCD\x6B - holding registers data (1 register)
*
* @implements \IteratorAggregate<int, Word>
* @implements \ArrayAccess<int, Word>
*/
class ReadHoldingRegistersResponse extends ByteCountResponse implements \ArrayAccess, \IteratorAggregate
{
/**
* @var string
*/
private string $data;
/** @var int[] */
private array $dataBytes;
public function __construct(string $rawData, int $unitId = 0, int $transactionId = null)
{
parent::__construct($rawData, $unitId, $transactionId);
$this->data = substr($rawData, 1); //first byte is byteCount. remove it
$this->dataBytes = Types::parseByteArray($this->data);
}
public function getFunctionCode(): int
{
return ModbusPacket::READ_HOLDING_REGISTERS;
}
/**
* @return int[]
*/
public function getData(): array
{
return $this->dataBytes;
}
/**
* Iterator returning data by words. Each word contains 2 bytes
*
* @return Traversable<Word>|\Generator<Word>
* @throws ModbusException
*/
public function asWords(): Traversable|\Generator
{
if ($this->getByteCount() % 2 !== 0) {
throw new ModbusException('getWords needs packet byte count to be multiple of 2');
}
$index = $this->getStartAddress();
foreach (str_split($this->data, 2) as $str) {
yield $index => new Word($str);
$index++;
}
}
/**
* Return data as splitted into words. Each word contains 2 bytes
*
* @return Word[] array of Words. each arrays contains 2 bytes
* @throws ModbusException
*/
public function getWords(): array
{
return iterator_to_array($this->asWords());
}
/**
* Iterator returning data by double words. Each dword contains 4 bytes
*
* @return Traversable<DoubleWord>|\Generator<DoubleWord>
*/
public function asDoubleWords(): Traversable|\Generator
{
$byteCount = $this->getByteCount();
if ($byteCount % 4 !== 0) {
throw new ModbusException('getDoubleWords needs packet byte count to be multiple of 4');
}
$index = $this->getStartAddress();
foreach (str_split($this->data, 4) as $str) {
yield $index => new DoubleWord($str);
$index += 2; // double word is 2 words :)
}
}
/**
* Return data as split into double words. Each dword contains 4 bytes
*
* @return DoubleWord[] array of Double Words. each arrays contains 4 bytes
* @throws ModbusException
*/
public function getDoubleWords(): array
{
return iterator_to_array($this->asDoubleWords());
}
public function __toString(): string
{
return parent::__toString()
. $this->data;
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + $this->getByteCount();
}
public function offsetSet($offset, $value): void
{
throw new ModbusException('setting value in response is not supported!');
}
public function offsetUnset($offset): void
{
throw new ModbusException('unsetting value in response is not supported!');
}
public function offsetExists($offset): bool
{
$address = ($offset - $this->getStartAddress()) * 2;
return isset($this->dataBytes[$address]);
}
public function offsetGet($offset): Word
{
$address = ($offset - $this->getStartAddress()) * 2;
$byteCount = $this->getByteCount();
if ($address < 0 || $address >= $byteCount) {
throw new InvalidArgumentException('offset out of bounds');
}
return new Word(substr($this->data, $address, 2));
}
public function getWordAt(int $wordAddress): Word
{
return $this->offsetGet($wordAddress);
}
public function getIterator(): Traversable|\Generator
{
return $this->asWords();
}
/**
* @param int $firstWordAddress
* @return DoubleWord
*/
public function getDoubleWordAt(int $firstWordAddress): DoubleWord
{
$address = ($firstWordAddress - $this->getStartAddress()) * 2;
$byteCount = $this->getByteCount();
if ($address < 0 || ($address + 4) > $byteCount) {
throw new InvalidArgumentException('address out of bounds');
}
return new DoubleWord(substr($this->data, $address, 4));
}
/**
* @param int $firstWordAddress
* @return QuadWord
*/
public function getQuadWordAt(int $firstWordAddress): QuadWord
{
$address = ($firstWordAddress - $this->getStartAddress()) * 2;
$byteCount = $this->getByteCount();
if ($address < 0 || ($address + 8) > $byteCount) {
throw new InvalidArgumentException('address out of bounds');
}
return new QuadWord(substr($this->data, $address, 8));
}
/**
* Parse ascii string from registers to utf-8 string
*
* @param int $startFromWord start parsing string from that word/register
* @param int $length count of characters to parse
* @param int|null $endianness byte and word order for modbus binary data
* @return string
*/
public function getAsciiStringAt(int $startFromWord, int $length, int $endianness = null): string
{
$address = ($startFromWord - $this->getStartAddress()) * 2;
$byteCount = $this->getByteCount();
if ($address < 0 || $address >= $byteCount) {
throw new InvalidArgumentException('startFromWord out of bounds');
}
if ($length < 1) {
// length can be bigger than bytes count - we will just parse less as there is nothing to parse
throw new InvalidArgumentException('length out of bounds');
}
$binaryData = substr($this->data, $address);
return Types::parseAsciiStringFromRegister($binaryData, $length, $endianness);
}
}