News:

  • June 10, 2026, 05:54:44 AM

Login with username, password and session length

Author Topic: phpmodbus  (Read 8689 times)

Bolt

  • Hero Member
  • *****
  • Posts: 598
phpmodbus
« on: February 06, 2018, 12:57:32 PM »
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:
Code: [Select]
<!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(0484);
}
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:

Code: [Select]
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.

Code: [Select]
<!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(TRUETRUEFALSETRUE);
try {  
//(UnitID, Offset, Data)
    
$modbus->writeMultipleCoils(01003$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:

Code: [Select]
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.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: phpmodbus
« Reply #1 on: February 06, 2018, 01:30:50 PM »
The last block of PHP source code had a different/invalid IP Address:

"192.192.0.91"

The second tuple should be ".168", not ".192"

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: phpmodbus
« Reply #2 on: February 06, 2018, 03:41:13 PM »
Boy, I feel smart now.  I wrote it after midnight, and didn't catch it this morning either...  Too much copying and pasting.  Anyways, this is working great now.  Thank you for your very simple assistance!