Install
At the first, it is supposed an PHP solution has been already installed on
your server (LAMP, WAMP, MSS+CGI etc.).
Copy the Phpmodbus library to your PHP project folder.
Create a PHP script and assign the library using require_once() command
require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php';
To create UDP Modbus master object communicating to an modbus slave
e.g. at IP address 192.168.1.1 write
$modbus = new ModbusMaster("192.168.1.1", "UDP");
To create TCP Modbus master use
$modbus = new ModbusMaster("192.168.1.1", "TCP");
To read 5 words (10 bytes) from the device ID=0 and its memory address 12288
use try-catch method call
try {
// Function processing
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
}
catch (Exception $e) {
// Exception processing, e.g. print error information
echo $modbus;
echo $e;
exit;
}
To process the function byte stream, use conversion to PHP types in
echo PhpType::bytes2string($recData);
For other examples see sections bellow.
Examples
This section presents library features and examples
FC1 - read mutliple coils
FC1 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
try {
// FC 1
$recData = $modbus->readCoils(0, 12288, 12);
}
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>";
echo "</br>";
FC2 - read input discretes
FC2 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
try {
// FC 2
// read 2 input bits from address 0x0 (Wago input image)
$recData = $modbus->readInputDiscretes(0, 0, 2);
}
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>";
echo "</br>";
FC3 - read mutliple registers
FC3 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
try {
// FC 3
$recData = $modbus->readMultipleRegisters(0, 12288, 6);
}
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>";
echo "</br>";
?>
FC4 - read multiple input registers
FC4 functionality example
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object
try {
// Read input discretes - FC 4
$recData = $modbus->readMultipleInputRegisters(0, 0, 2);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
FC5 - write single coil
FC5 functionality example
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object
// Data to be writen - TRUE, FALSE
$data_true = array(TRUE);
$data_false = array(FALSE);
try {
// Write single coil - FC5
$modbus->writeSingleCoil(0, 12288, $data_true);
$modbus->writeSingleCoil(0, 12289, $data_false);
$modbus->writeSingleCoil(0, 12290, $data_true);
$modbus->writeSingleCoil(0, 12291, $data_false);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
FC6 - write single register
FC6 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
// Data to be writen
$data = array(-1000);
$dataTypes = array("INT");
try {
// FC6
$modbus->writeSingleRegister(0, 12288, $data, $dataTypes);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
// Print status information
echo $modbus;
?>
FC15 - write mutliple coils
FC15 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
// Data to be writen
$data = array(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
try {
// FC15
$modbus->writeMultipleCoils(0, 12288, $data);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
// Print status information
echo $modbus;
FC16 - write mutliple registers
FC16 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
// Data to be writen
$data = array(10, -1000, 2000, 3.0);
$dataTypes = array("WORD", "INT", "DINT", "REAL");
try {
// FC16
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
// Print status information
echo $modbus;
?>
FC23 - read write registers
FC23 functionality example
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
// Data to be writen
$data = array(10, -1000, 2000, 3.0);
$dataTypes = array("WORD", "INT", "DINT", "REAL");
try {
// FC23
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
}
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>";
echo "</br>";
?>
Dump of M-memory from WAGO 750-84x series coupler.
Dump of M-memory from WAGO 750-84x series coupler.
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
$ip = "192.192.15.51";
try {
// FC 3
$moduleId = 0;
$reference = 12288;
$mw0address = 12288;
$quantity = 6;
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity);
}
catch (Exception $e) {
echo $modbus;
echo $e;
exit;
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>WAGO 750-841 M-memory dump</title>
</head>
<body>
<h1>Dump of M-memory from WAGO 750-84x series coupler.</h1>
<ul>
<li>PLC: 750-84x series</li>
<li>IP: <?php echo $ip?></li>
<li>Modbus module ID: <?php echo $moduleId?></li>
<li>Modbus memory reference: <?php echo $reference?></li>
<li>Modbus memory quantity: <?php echo $quantity?></li>
</ul>
<h2>M-memory dump</h2>
<table border="1px" width="400px">
<tr>
<td>Modbus address</td>
<td>MWx</td>
<td>value</td>
</tr>
<?php
for($i=
0;$i<
count($recData);$i+=
2) {
?>
<tr>
<td><?php echo $i+$reference?></td>
<td>MW<?php echo ($i + $reference - $mw0address)/2?></td>
<td><?php echo ($recData[$i] << 8)+ $recData[$i+1]?></td>
</tr>
<?php
}
?>
</table>
<h2>Modbus class status</h2>
<?php
echo $modbus;
?>
</body>
</html>
Data conversion to PHP types.
Conversion of the data bytes, received from Modbus, to a PHP type.
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
// Create Modbus object
try {
// FC 3
// read 10 words (20 bytes) from device ID=0, address=12288
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
}
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
}
// Received data
echo "<h1>Received Data</h1>\n";
// Conversion
echo "<h2>32 bits types</h2>\n";
// Chunk the data array to set of 4 bytes
// Get float from REAL interpretation
echo "<h3>REAL to Float</h3>\n";
foreach($values as $bytes)
// Get integer from DINT interpretation
echo "<h3>DINT to integer </h3>\n";
foreach($values as $bytes)
// Get integer of float from DINT interpretation
echo "<h3>DWORD to integer (or float) </h3>\n";
foreach($values as $bytes)
echo "<h2>16 bit types</h2>\n";
// Chunk the data array to set of 4 bytes
// Get signed integer from INT interpretation
echo "<h3>INT to integer </h3>\n";
foreach($values as $bytes)
// Get unsigned integer from WORD interpretation
echo "<h3>WORD to integer </h3>\n";
foreach($values as $bytes)
// Get string from STRING interpretation
echo "<h3>STRING to string </h3>\n";
?>