-
Notifications
You must be signed in to change notification settings - Fork 56
/
ReadCoilsResponse.php
124 lines (109 loc) · 3.1 KB
/
ReadCoilsResponse.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
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Exception\InvalidArgumentException;
use ModbusTcpClient\Exception\ModbusException;
use ModbusTcpClient\Packet\ByteCountResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Utils\Types;
/**
* Response for Read Coils (FC=01)
*
* Example packet: \x81\x80\x00\x00\x00\x05\x03\x01\x02\xCD\x6B
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x03 - unit id
* \x01 - function code
* \x02 - coils byte count
* \xCD\x6B - coils data (2 bytes = 2 * 8 coils)
*
* @implements \IteratorAggregate<int, bool>
* @implements \ArrayAccess<int, bool>
*/
class ReadCoilsResponse extends ByteCountResponse implements \ArrayAccess, \IteratorAggregate
{
/**
* @var bool[]
*/
private array $coils;
/**
* @var int
*/
private int $coilsBytesLength;
public function __construct(string $rawData, int $unitId = 0, int $transactionId = null)
{
$data = substr($rawData, 1);
$this->coilsBytesLength = strlen($data);
$this->coils = Types::binaryStringToBooleanArray($data);
parent::__construct($rawData, $unitId, $transactionId);
}
public function getFunctionCode(): int
{
return ModbusPacket::READ_COILS;
}
/**
* @return bool[]
*/
public function getCoils(): array
{
return iterator_to_array($this->getIterator());
}
public function __toString(): string
{
return parent::__toString()
. Types::byteArrayToByte(Types::booleanArrayToByteArray($this->coils));
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + $this->coilsBytesLength;
}
/**
* @return \Generator<bool>
*/
public function getIterator(): \Generator
{
$index = $this->getStartAddress();
foreach ($this->coils as $coil) {
yield $index++ => $coil;
}
}
/**
* @param int $offset
* @param bool $value
* @throws ModbusException
*/
public function offsetSet($offset, $value): void
{
throw new ModbusException('setting value in response is not supported!');
}
/**
* @param int $offset
* @throws ModbusException
*/
public function offsetUnset($offset): void
{
throw new ModbusException('unsetting value in response is not supported!');
}
/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return isset($this->coils[$offset - $this->getStartAddress()]);
}
/**
* @param int $offset
* @return bool
*/
public function offsetGet($offset): bool
{
$address = $offset - $this->getStartAddress();
$endAddress = ($this->getByteCount() * 8);
if ($address < 0 || $address >= $endAddress) {
throw new InvalidArgumentException('offset out of bounds');
}
return $this->coils[$offset - $this->getStartAddress()];
}
}