Has anyone used phpmodbus,
https://code.google.com/archive/p/phpmodbus/, a php library to read/write modbus TCP via PHP scripts? There are a few (old) posts on here regarding it.
I have successfully been able to read from my BRX, with this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Read</title>
</head>
<body>
<?php
require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php';
// Create Modbus object
$modbus = new ModbusMaster("192.168.0.91", "TCP");
try { //(UnitID, Offset, Number)
$recData = $modbus->readCoils(0, 48, 4);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
// Print status information
echo "</br>Status:</br>" . $modbus;
// Print read data
echo "</br>Data:</br>";
print_r($recData);
echo "</br>";
?>
</body>
</html>
It responds:
Status:
readCoils: START
Connected
Packet: 186c00000006000100300004
Send
Wait data ...
Data received
Packet: 186c0000000400010108
Modbus response error code: NOERROR
Disconnected
readCoils: DONE
Data:
Array ( [0] => [1] => [2] => [3] => 1 )
But when I try to write, I don't get as much.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Write</title>
</head>
<body>
<?php
require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php';
// Create Modbus object
$modbus = new ModbusMaster("192.192.0.91", "TCP");
// Data to be writen
$data = array(TRUE, TRUE, FALSE, TRUE);
try { //(UnitID, Offset, Data)
$modbus->writeMultipleCoils(0, 1003, $data);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
// Print status information
echo $modbus;
?>
</body>
</html>
I get responses like:
writeMultipleCoils: START
Exception: socket_connect() failed.
Reason: ()A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\Phpmodbus\ModbusMaster.php:102 Stack trace: #0 C:\xampp\htdocs\Phpmodbus\ModbusMaster.php(527): ModbusMaster->connect() #1 C:\xampp\htdocs\index2.php(15): ModbusMaster->writeMultipleCoils(0, 1003, Array) #2 {main}
Anybody have ideas as to where I am going wrong?
What unit ID should I use to communicate with my PLC? I've tried 0, 1, and 255. I've also tried 1's and 0's vs. TRUE and FALSE.