Phpmodbus user's guide

Phpmodbus How-to

Jan Krakora

Table of Contents

Introduction

Phpmodbus is a PHP library for the Modbus protocol. The library implements Modbus TCP/UDP master class with subset of the most used Modbus commands.

The library implements:

  • FC 1: read multiple coils

  • FC 2: read input discretes

  • FC 3: read multiple registers

  • FC 4: read multiple input registers

  • FC 5: write single coil

  • FC 6: write single register

  • FC 15: write multiple coils

  • FC 16: write multiple registers

  • FC 23: read write registers

For more about Modbus protocol see [http://www.modbus.org] or [Wiki]

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

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. try {
  9.     // FC 1
  10.     $recData $modbus->readCoils(01228812);
  11. }
  12. catch (Exception $e{
  13.     // Print error information if any
  14.     echo $modbus;
  15.     echo $e;
  16.     exit;
  17. }
  18.  
  19. // Print status information
  20. echo "</br>Status:</br>" $modbus;
  21.  
  22. // Print read data
  23. echo "</br>Data:</br>";
  24. var_dump($recData)
  25. echo "</br>";

FC2 - read input discretes

FC2 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. try {
  9.     // FC 2
  10.     // read 2 input bits from address 0x0 (Wago input image)
  11.     $recData $modbus->readInputDiscretes(002);
  12. }
  13. catch (Exception $e{
  14.     // Print error information if any
  15.     echo $modbus;
  16.     echo $e;
  17.     exit;
  18. }
  19.  
  20. // Print status information
  21. echo "</br>Status:</br>" $modbus;
  22.  
  23. // Print read data
  24. echo "</br>Data:</br>";
  25. var_dump($recData)
  26. echo "</br>";

FC3 - read mutliple registers

FC3 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. try {
  9.     // FC 3
  10.     $recData $modbus->readMultipleRegisters(0122886);
  11. }
  12. catch (Exception $e{
  13.     // Print error information if any
  14.     echo $modbus;
  15.     echo $e;
  16.     exit;
  17. }
  18.  
  19. // Print status information
  20. echo "</br>Status:</br>" $modbus;
  21.  
  22. // Print read data
  23. echo "</br>Data:</br>";
  24. print_r($recData)
  25. echo "</br>";
  26. ?>

FC4 - read multiple input registers

FC4 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../../Phpmodbus/ModbusMasterUdp.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMasterUdp("192.192.15.51");
  7.  
  8. try {
  9.     // Read input discretes - FC 4
  10.     $recData $modbus->readMultipleInputRegisters(002);
  11. }
  12. catch (Exception $e{
  13.     // Print error information if any
  14.     echo $modbus;
  15.     echo $e;
  16.     exit;
  17. }
  18.  
  19. var_dump($recData);

FC5 - write single coil

FC5 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../../Phpmodbus/ModbusMasterUdp.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMasterUdp("192.192.15.51");
  7.  
  8. // Data to be writen - TRUE, FALSE
  9. $data_true array(TRUE);
  10. $data_false array(FALSE);
  11.  
  12. try {
  13.     // Write single coil - FC5
  14.     $modbus->writeSingleCoil(012288$data_true);
  15.     $modbus->writeSingleCoil(012289$data_false);
  16.     $modbus->writeSingleCoil(012290$data_true);
  17.     $modbus->writeSingleCoil(012291$data_false);
  18. }
  19. catch (Exception $e{
  20.     // Print error information if any
  21.     echo $modbus;
  22.     echo $e;
  23.     exit;
  24. }

FC6 - write single register

FC6 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. // Data to be writen
  9. $data array(-1000);
  10. $dataTypes array("INT");
  11.  
  12. try {
  13.     // FC6
  14.     $modbus->writeSingleRegister(012288$data$dataTypes);
  15. }
  16. catch (Exception $e{
  17.     // Print error information if any
  18.     echo $modbus;
  19.     echo $e;
  20.     exit;
  21. }
  22.  
  23. // Print status information
  24. echo $modbus;
  25.  
  26. ?>

FC15 - write mutliple coils

FC15 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. // Data to be writen
  9. $data array(TRUEFALSETRUETRUEFALSETRUETRUETRUE
  10.               TRUETRUETRUETRUEFALSEFALSEFALSEFALSE,
  11.               FALSEFALSEFALSEFALSETRUETRUETRUETRUE,
  12.               TRUETRUETRUETRUETRUETRUETRUETRUE);
  13.  
  14. try {
  15.     // FC15
  16.     $modbus->writeMultipleCoils(012288$data);
  17. }
  18. catch (Exception $e{
  19.     // Print error information if any
  20.     echo $modbus;
  21.     echo $e;
  22.     exit;
  23. }
  24.  
  25. // Print status information
  26. echo $modbus;

FC16 - write mutliple registers

FC16 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. // Data to be writen
  9. $data array(10-100020003.0);
  10. $dataTypes array("WORD""INT""DINT""REAL");
  11.  
  12. try {
  13.     // FC16
  14.     $modbus->writeMultipleRegister(012288$data$dataTypes);
  15. }
  16. catch (Exception $e{
  17.     // Print error information if any
  18.     echo $modbus;
  19.     echo $e;
  20.     exit;
  21. }
  22.  
  23. // Print status information
  24. echo $modbus;
  25.  
  26. ?>

FC23 - read write registers

FC23 functionality example

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. // Data to be writen
  9. $data array(10-100020003.0);
  10. $dataTypes array("WORD""INT""DINT""REAL");
  11.  
  12. try {
  13.     // FC23
  14.     $recData $modbus->readWriteRegisters(012288612288$data$dataTypes);
  15. }
  16. catch (Exception $e{
  17.     // Print error information if any
  18.     echo $modbus;
  19.     echo $e;
  20.     exit;
  21. }
  22.  
  23. // Print status information
  24. echo "</br>Status:</br>" $modbus;
  25.  
  26. // Print read data
  27. echo "</br>Data:</br>";
  28. print_r($recData);
  29. echo "</br>";
  30.  
  31. ?>

Dump of M-memory from WAGO 750-84x series coupler.

Dump of M-memory from WAGO 750-84x series coupler.

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $ip "192.192.15.51";
  7. $modbus new ModbusMaster($ip"UDP");
  8.  
  9. try {
  10.     // FC 3
  11.     $moduleId 0;
  12.     $reference 12288;
  13.     $mw0address 12288;
  14.     $quantity 6;
  15.     $recData $modbus->readMultipleRegisters($moduleId$reference$quantity);
  16. }
  17. catch (Exception $e{
  18.     echo $modbus;
  19.     echo $e;
  20.     exit;
  21. }
  22.  
  23. ?>
  24. <html>
  25.     <head>
  26.         <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  27.         <meta name="generator" content="PSPad editor, www.pspad.com">
  28.         <title>WAGO 750-841 M-memory dump</title>
  29.     </head>
  30.     <body>
  31.         <h1>Dump of M-memory from WAGO 750-84x series coupler.</h1>
  32.         <ul>
  33.             <li>PLC: 750-84x series</li>
  34.             <li>IP: <?php echo $ip?></li>
  35.             <li>Modbus module ID: <?php echo $moduleId?></li>
  36.             <li>Modbus memory reference: <?php echo $reference?></li>
  37.             <li>Modbus memory quantity: <?php echo $quantity?></li>
  38.         </ul>
  39.  
  40.         <h2>M-memory dump</h2>
  41.  
  42.         <table border="1px" width="400px">
  43.             <tr>
  44.                 <td>Modbus address</td>
  45.                 <td>MWx</td>
  46.                 <td>value</td>
  47.             </tr>
  48.             <?php
  49.             for($i=0;$i<count($recData);$i+=2{
  50.                 ?>
  51.             <tr>
  52.                 <td><?php echo $i+$reference?></td>
  53.                 <td>MW<?php echo ($i $reference $mw0address)/2?></td>
  54.                 <td><?php echo ($recData[$i<< 8)$recData[$i+1]?></td>
  55.             </tr>
  56.                 <?php
  57.             }
  58.             ?>
  59.         </table>
  60.  
  61.         <h2>Modbus class status</h2>
  62.         <?php
  63.         echo $modbus;
  64.         ?>
  65.  
  66.     </body>
  67. </html>

Data conversion to PHP types.

Conversion of the data bytes, received from Modbus, to a PHP type.

  1. <?php
  2.  
  3. require_once dirname(__FILE__'/../Phpmodbus/ModbusMaster.php';
  4.  
  5. // Create Modbus object
  6. $modbus new ModbusMaster("192.192.15.51""UDP");
  7.  
  8. try {
  9.     // FC 3
  10.     // read 10 words (20 bytes) from device ID=0, address=12288
  11.     $recData $modbus->readMultipleRegisters(01228810);
  12. }
  13. catch (Exception $e{
  14.     // Print error information if any
  15.     echo $modbus;
  16.     echo $e;
  17.     exit;
  18. }
  19.  
  20. // Received data
  21. echo "<h1>Received Data</h1>\n";
  22. print_r($recData);
  23.  
  24. // Conversion
  25. echo "<h2>32 bits types</h2>\n";
  26. // Chunk the data array to set of 4 bytes
  27. $values array_chunk($recData4);
  28.  
  29. // Get float from REAL interpretation
  30. echo "<h3>REAL to Float</h3>\n";
  31. foreach($values as $bytes)
  32.     echo PhpType::bytes2float($bytes"</br>";
  33.  
  34. // Get integer from DINT interpretation
  35. echo "<h3>DINT to integer </h3>\n";
  36. foreach($values as $bytes)
  37.     echo PhpType::bytes2signedInt($bytes"</br>";
  38.  
  39. // Get integer of float from DINT interpretation
  40. echo "<h3>DWORD to integer (or float) </h3>\n";
  41. foreach($values as $bytes)
  42.     echo PhpType::bytes2unsignedInt($bytes"</br>";
  43.  
  44. echo "<h2>16 bit types</h2>\n";
  45. // Chunk the data array to set of 4 bytes
  46. $values array_chunk($recData2);
  47.  
  48. // Get signed integer from INT interpretation
  49. echo "<h3>INT to integer </h3>\n";
  50. foreach($values as $bytes)
  51.     echo PhpType::bytes2signedInt($bytes"</br>";
  52.  
  53. // Get unsigned integer from WORD interpretation
  54. echo "<h3>WORD to integer </h3>\n";
  55. foreach($values as $bytes)
  56.     echo PhpType::bytes2unsignedInt($bytes"</br>";
  57.  
  58. // Get string from STRING interpretation
  59. echo "<h3>STRING to string </h3>\n";
  60. echo PhpType::bytes2string($recData"</br>";
  61. ?>

Back compatibility

This version is back compatible to the last versions. Just use

require_once dirname(__FILE__) . '/Phpmodbus/ModbusMasterUdp.php';
$modbus = new ModbusMasterUdp("192.168.1.1"); 
// ... 

Documentation generated on Tue, 09 Jul 2013 23:46:22 +0200 by phpDocumentor 1.4.1