-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackuptcp2.php
More file actions
95 lines (87 loc) · 4.01 KB
/
backuptcp2.php
File metadata and controls
95 lines (87 loc) · 4.01 KB
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
<?php
//require_once 'ModbusMaster.php';
//require_once 'PhpType.php';
use ModbusTcpClient\Network\BinaryStreamConnection;
use ModbusTcpClient\Packet\ModbusFunction\WriteSingleCoilRequest;
use ModbusTcpClient\Packet\ModbusFunction\WriteSingleCoilResponse;
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["FC"]) && isset($_POST["Coil"]) && isset($_POST["Value"]))
{
$Host=$_POST["Host"];
$Port=$_POST["Port"];
$UnitID=$_POST["UnitID"];
$Address=$_POST["Address"];
//$Quantity=$_POST["Quantity"];
$FunCode=$_POST["FC"];
$Value=$_POST["Value"];
$Coil=$_POST["Coil"];
//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";
//echo "Coil is: $Coil";
//echo "Value is: $Value";
if ($FunCode == "5") {
$connection = BinaryStreamConnection::getBuilder()
->setPort($Port)
->setHost($Host)
->build();
$startAddress = $Address;
$coil = $Coil;
$packet = new WriteSingleCoilRequest($startAddress,$coil);
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 'Coil value parsed from packet:' . PHP_EOL;
print_r($response->isCoil());
session_start();
$_SESSION["d"]=$response->isCoil();
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
echo '<a href="Modbustcp.html"><br /><br />Return to Modbus Page</a>';
}
} elseif ($FunCode == "6") {
$connection = BinaryStreamConnection::getBuilder()
->setPort($Port)
->setHost($Host)
->build();
$startAddress = $Address;
$value = '$Value';
$packet = new WriteSingleRegisterRequest($startAddress,$value);
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 'Coil value parsed from packet:' . PHP_EOL;
print_r($response->getWord()->getInt16());
session_start();
$_SESSION["d"]=$response->getWord()->getInt16();
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
echo '<a href="Modbustcp.html"><br /><br />Return to Modbus Page</a>';
}
} else {
echo "Invalid Function Code";
echo '<a href="Modbustcp.html"><br /><br />Return to Modbus Page</a>';
}
}
?>