-
Notifications
You must be signed in to change notification settings - Fork 56
/
ReadCoilsRequest.php
96 lines (83 loc) · 2.75 KB
/
ReadCoilsRequest.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
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Exception\InvalidArgumentException;
use ModbusTcpClient\Packet\ErrorResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\ModbusRequest;
use ModbusTcpClient\Packet\ProtocolDataUnitRequest;
use ModbusTcpClient\Utils\Endian;
use ModbusTcpClient\Utils\Types;
/**
* Request for Read Coils (FC=01)
*
* Example packet: \x81\x80\x00\x00\x00\x06\x10\x01\x00\x6B\x00\x03
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x10 - unit id
* \x01 - function code
* \x00\x6B - start address
* \x00\x03 - coils quantity to return
*
*/
class ReadCoilsRequest extends ProtocolDataUnitRequest implements ModbusRequest
{
/**
* @var int total number of coils requested. Size 2 bytes
*/
private int $quantity;
public function __construct(int $startAddress, int $quantity, int $unitId = 0, int $transactionId = null)
{
parent::__construct($startAddress, $unitId, $transactionId);
$this->quantity = $quantity;
$this->validate();
}
public function validate(): void
{
parent::validate();
if (($this->quantity > 0 && $this->quantity <= 2048)) {
// 2048 coils is due that in response data size field is 1 byte so max 256*8=2048 coils can be returned
return;
}
throw new InvalidArgumentException("quantity is not set or out of range (1-2048): {$this->quantity}", 3);
}
public function getFunctionCode(): int
{
return ModbusPacket::READ_COILS;
}
public function __toString(): string
{
return parent::__toString()
. Types::toRegister($this->getQuantity());
}
/**
* @return int
*/
public function getQuantity(): int
{
return $this->quantity;
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + 2; // quantity size (2 bytes)
}
/**
* Parses binary string to ReadCoilsRequest or return ErrorResponse on failure
*
* @param string $binaryString
* @return ReadCoilsRequest|ErrorResponse
*/
public static function parse(string $binaryString): ErrorResponse|ReadCoilsRequest
{
return self::parseStartAddressPacket(
$binaryString,
12,
ModbusPacket::READ_COILS,
function (int $transactionId, int $unitId, int $startAddress) use ($binaryString) {
$quantity = Types::parseUInt16($binaryString[10] . $binaryString[11], Endian::BIG_ENDIAN);
return new self($startAddress, $quantity, $unitId, $transactionId);
}
);
}
}