-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtu_usb_to_serial_stream.php
53 lines (42 loc) · 2.14 KB
/
rtu_usb_to_serial_stream.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
<?php
use ModbusTcpClient\Network\BinaryStreamConnection;
use ModbusTcpClient\Packet\ModbusFunction\ReadInputRegistersRequest;
use ModbusTcpClient\Packet\ModbusFunction\ReadInputRegistersResponse;
use ModbusTcpClient\Packet\RtuConverter;
require __DIR__ . '/../vendor/autoload.php';
$connection = BinaryStreamConnection::getBuilder()
->setUri('/dev/ttyUSB0')
->setProtocol('serial')
->setIsCompleteCallback(static function ($binaryData, $streamIndex): bool {
// NB: returning always true could lead to problems with reading fragmented packets (so we returning too early)
// safest way would be to calculate bytes length that we expect and error response length.
// Example for error: 5 bytes = 1 unit id + 1 byte for function code + 1 byte for error code + 2 bytes for CRC
// Example for FC4 with quantity 2: 8 bytes = 1 unit id + 1 byte for function code + 2 bytes start address + 2 * quantity
return true;
})
->build();
$startAddress = 1;
$quantity = 2;
$slaveId = 1; // RTU packet slave id equivalent is Modbus TCP unitId
$tcpPacket = new ReadInputRegistersRequest($startAddress, $quantity, $slaveId);
$rtuPacket = RtuConverter::toRtu($tcpPacket);
try {
echo 'RTU Binary to sent (in hex): ' . unpack('H*', $rtuPacket)[1] . PHP_EOL;
$start = microtime(true);
$binaryData = $connection->connect()->sendAndReceive($rtuPacket);
$end = (microtime(true) - $start) * 1000;
echo 'Response in: ' . $end . ' ms' . PHP_EOL;
echo 'RTU Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL;
/** @var ReadInputRegistersResponse $response */
$response = RtuConverter::fromRtu($binaryData)->withStartAddress($startAddress);
echo 'Parsed packet (in hex): ' . $response->toHex() . PHP_EOL;
echo PHP_EOL;
echo 'Temperature: ' . ($response->getWordAt(1)->getInt16() / 10) . PHP_EOL;
echo 'Humidity: ' . ($response->getWordAt(2)->getInt16() / 10) . PHP_EOL;
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
}