-
Notifications
You must be signed in to change notification settings - Fork 0
/
backuptcp.php
169 lines (149 loc) · 7.23 KB
/
backuptcp.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
<?php
use ModbusTcpClient\Network\BinaryStreamConnection;
use ModbusTcpClient\Packet\ModbusFunction\ReadCoilsRequest;
use ModbusTcpClient\Packet\ModbusFunction\ReadCoilsResponse;
use ModbusTcpClient\Packet\ResponseFactory;
require 'vendor/autoload.php';
include("PhpSerialModbus.php");
if (isset($_POST["Host"]) && isset($_POST["Port"]) && isset($_POST["UnitID"]) && isset($_POST["Address"]) && isset($_POST["Quantity"]) && isset($_POST["FC"]))
{
$Host=$_POST["Host"];
$Port=$_POST["Port"];
$UnitID=$_POST["UnitID"];
$Address=$_POST["Address"];
$Quantity=$_POST["Quantity"];
$FunCode=$_POST["FC"];
//echo "Host is: $Host";
//echo "Port is: $Port";
//echo "Unit ID is: $UnitID";
//echo "Address is: $Address";
//echo "Quantity is: $Quantity";
//echo "Function Code is: $FunCode";
if ($FunCode == "1") {
$connection = BinaryStreamConnection::getBuilder()
->setPort($Port)
->setHost($Host)
->build();
$startAddress = $Address;
$quantity = $Quantity;
$packet = new ReadCoilsRequest($startAddress, $quantity);
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
try {
$binaryData = $connection->connect()
->sendAndReceive($packet);
echo 'Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL;
$response = ResponseFactory::parseResponseOrThrow($binaryData);
echo 'Parsed packet (in hex): ' . $response->toHex() . PHP_EOL;
echo 'Data parsed from packet (bytes):' . PHP_EOL;
print_r($response->getCoils());
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
}
}
elseif ($FunCode == "2")
{
$connection = BinaryStreamConnection::getBuilder()
->setPort($Port)
->setHost($Host)
->build();
$startAddress = $Address;
$quantity = $Quantity;
$packet = new ReadInputDiscretesRequest($startAddress, $quantity);
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
try {
$binaryData = $connection->connect()
->sendAndReceive($packet);
echo 'Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL;
$response = ResponseFactory::parseResponseOrThrow($binaryData);
echo 'Parsed packet (in hex): ' . $response->toHex() . PHP_EOL;
echo 'Data parsed from packet (bytes):' . PHP_EOL;
print_r($response->getCoils());
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
}
}
elseif ($FunCode == "3")
{
$connection = BinaryStreamConnection::getBuilder()
->setPort($Port)
->setHost($Host)
->build();
$startAddress = $Address;
$quantity = $Quantity;
$packet = new ReadHoldingRegistersRequest($startAddress, $quantity);
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
try {
$binaryData = $connection->connect()
->sendAndReceive($packet);
echo 'Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL;
$response = ResponseFactory::parseResponseOrThrow($binaryData);
echo 'Parsed packet (in hex): ' . $response->toHex() . PHP_EOL;
echo 'Data parsed from packet (bytes):' . PHP_EOL;
print_r($response->getData());
foreach ($response as $word) {
print_r($word->getBytes());
}
foreach ($response->asDoubleWords() as $doubleWord) {
print_r($doubleWord->getBytes());
}
$responseWithStartAddress = $response->withStartAddress($startAddress);
print_r($responseWithStartAddress[256]->getBytes()); // use array access to get word
print_r($responseWithStartAddress->getDoubleWordAt(257)->getFloat());
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
}
}
elseif ($FunCode == "4")
{
$connection = BinaryStreamConnection::getBuilder()
->setPort($Port)
->setHost($Host)
->build();
$startAddress = $Address;
$quantity = $Quantity;
$packet = new ReadInputRegistersRequest($startAddress, $quantity);
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
try {
$binaryData = $connection->connect()
->sendAndReceive($packet);
echo 'Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL;
$response = ResponseFactory::parseResponseOrThrow($binaryData);
echo 'Parsed packet (in hex): ' . $response->toHex() . PHP_EOL;
echo 'Data parsed from packet (bytes):' . PHP_EOL;
print_r($response->getData());
foreach ($response as $word) {
print_r($word->getBytes());
}
foreach ($response->asDoubleWords() as $doubleWord) {
print_r($doubleWord->getBytes());
}
$responseWithStartAddress = $response->withStartAddress($startAddress);
print_r($responseWithStartAddress[256]->getBytes()); // use array access to get word
print_r($responseWithStartAddress->getDoubleWordAt(257)->getFloat());
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
}
}
else
{
echo "Invalid Function Code";
echo '<a href="Modbustcp.html"><br /><br />Return to Modbus Page</a>';
}
}
?>