fixed the examples

pull/2/merge 0.10.0
Ondřej Hruška 8 years ago
parent 61e7a690eb
commit 4256b6cef7
  1. 53
      README.md
  2. 98
      examples/example_750841_Mmemory.php
  3. 41
      examples/example_datatype.php
  4. 17
      examples/example_fc1.php
  5. 29
      examples/example_fc15.php
  6. 17
      examples/example_fc16.php
  7. 19
      examples/example_fc2.php
  8. 23
      examples/example_fc22.php
  9. 19
      examples/example_fc23.php
  10. 18
      examples/example_fc3.php
  11. 17
      examples/example_fc4.php
  12. 27
      examples/example_fc5.php
  13. 20
      examples/example_fc6.php

@ -2,38 +2,44 @@
Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP. Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP.
This is a fork of the original project at https://code.google.com/p/phpmodbus/ **NOTE: This is a fork to fix & update the library code (and code alone). Notably, the tests are probably all broken.**
> **What's new** > **What's new**
> >
> This fork adds a namespace and fixes issues encountered when porting to PHP 7 > This fork adds a namespace and fixes issues encountered when porting to PHP 7
**WARNING: Everything except the actual code in this repo may be broken and outdated.**
Implemented features: ## Implemented features
* Modbus master * Modbus master
* FC1 - Read coils * FC1 - Read coils
* FC2 - Read input discretes * FC2 - Read input discretes
* FC3 - Read holding registers * FC3 - Read holding registers
* FC4 - Read holding input registers * FC4 - Read holding input registers
* FC5 - Write single coil * FC5 - Write single coil
* FC6 - Write single register * FC6 - Write single register
* FC15 - Write multiple coils * FC15 - Write multiple coils
* FC16 - Write multiple registers * FC16 - Write multiple registers
* FC23 - Read/Write multiple registers * FC23 - Read/Write multiple registers
Example:
## Requirements
* The PHP extension php_sockets.dll should be enabled (server php.ini file)
## Example
```php ```php
require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php'; use PHPModbus/ModbusMaster;
use PHPModbus/PhpType;
// Modbus master UDP // Modbus master UDP
$modbus = new ModbusMaster("192.168.1.1", "UDP"); $modbus = new ModbusMaster("192.168.1.1", "UDP");
// Read multiple registers // Read multiple registers
try { try {
$recData = $modbus->readMultipleRegisters(0, 12288, 5); $recData = $modbus->readMultipleRegisters(0, 12288, 5);
} } catch (Exception $e) {
catch (Exception $e) {
// Print error information if any // Print error information if any
echo $modbus; echo $modbus;
echo $e; echo $e;
@ -43,7 +49,14 @@ catch (Exception $e) {
echo PhpType::bytes2string($recData); echo PhpType::bytes2string($recData);
``` ```
For more see [http://code.google.com/p/phpmodbus/downloads/list documentation] or [http://code.google.com/p/phpmodbus/wiki/FAQ FAQ]. Use the `setTimeout($seconds)` and `setSocketTimeout($read_timeout_sec, $write_timeout_sec)` methods to adjust wait times.
Most of the code is (to some extent) commented and documented with PhpDoc. You should get useful tooltips in your IDE.
## GoogleCode legacy docs & downloads
This project was originally hosted on (now defunct) Google Code. It's still archived here:
* [GoogleCode Archived Repo](http://code.google.com/p/phpmodbus)
Note:
* The PHP extension php_sockets.dll should be enabled (server php.ini file)

@ -1,67 +1,65 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$ip = "192.192.15.51"; $ip = "192.192.15.51";
$modbus = new ModbusMaster($ip, "UDP"); $modbus = new ModbusMaster($ip, "UDP");
try { try {
// FC 3 // FC 3
$moduleId = 0; $moduleId = 0;
$reference = 12288; $reference = 12288;
$mw0address = 12288; $mw0address = 12288;
$quantity = 6; $quantity = 6;
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity); $recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity);
} } catch (Exception $e) {
catch (Exception $e) { echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
?> ?>
<html> <html>
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com"> <meta name="generator" content="PSPad editor, www.pspad.com">
<title>WAGO 750-841 M-memory dump</title> <title>WAGO 750-841 M-memory dump</title>
</head> </head>
<body> <body>
<h1>Dump of M-memory from WAGO 750-84x series coupler.</h1> <h1>Dump of M-memory from WAGO 750-84x series coupler.</h1>
<ul> <ul>
<li>PLC: 750-84x series</li> <li>PLC: 750-84x series</li>
<li>IP: <?php echo $ip?></li> <li>IP: <?php echo $ip ?></li>
<li>Modbus module ID: <?php echo $moduleId?></li> <li>Modbus module ID: <?php echo $moduleId ?></li>
<li>Modbus memory reference: <?php echo $reference?></li> <li>Modbus memory reference: <?php echo $reference ?></li>
<li>Modbus memory quantity: <?php echo $quantity?></li> <li>Modbus memory quantity: <?php echo $quantity ?></li>
</ul> </ul>
<h2>M-memory dump</h2>
<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>
<table border="1px" width="400px"> <h2>Modbus class status</h2>
<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> <pre><?= $modbus ?></pre>
<?php
echo $modbus;
?>
</body> </body>
</html> </html>

@ -1,20 +1,20 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
use PHPModbus\PhpType;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
try { try {
// FC 3 // FC 3
// read 10 words (20 bytes) from device ID=0, address=12288 // read 10 words (20 bytes) from device ID=0, address=12288
$recData = $modbus->readMultipleRegisters(0, 12288, 10); $recData = $modbus->readMultipleRegisters(0, 12288, 10);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Received data // Received data
@ -28,18 +28,18 @@ $values = array_chunk($recData, 4);
// Get float from REAL interpretation // Get float from REAL interpretation
echo "<h3>REAL to Float</h3>\n"; echo "<h3>REAL to Float</h3>\n";
foreach($values as $bytes) foreach ($values as $bytes)
echo PhpType::bytes2float($bytes) . "</br>"; echo PhpType::bytes2float($bytes) . "</br>";
// Get integer from DINT interpretation // Get integer from DINT interpretation
echo "<h3>DINT to integer </h3>\n"; echo "<h3>DINT to integer </h3>\n";
foreach($values as $bytes) foreach ($values as $bytes)
echo PhpType::bytes2signedInt($bytes) . "</br>"; echo PhpType::bytes2signedInt($bytes) . "</br>";
// Get integer of float from DINT interpretation // Get integer of float from DINT interpretation
echo "<h3>DWORD to integer (or float) </h3>\n"; echo "<h3>DWORD to integer (or float) </h3>\n";
foreach($values as $bytes) foreach ($values as $bytes)
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; echo PhpType::bytes2unsignedInt($bytes) . "</br>";
echo "<h2>16 bit types</h2>\n"; echo "<h2>16 bit types</h2>\n";
// Chunk the data array to set of 4 bytes // Chunk the data array to set of 4 bytes
@ -47,15 +47,14 @@ $values = array_chunk($recData, 2);
// Get signed integer from INT interpretation // Get signed integer from INT interpretation
echo "<h3>INT to integer </h3>\n"; echo "<h3>INT to integer </h3>\n";
foreach($values as $bytes) foreach ($values as $bytes)
echo PhpType::bytes2signedInt($bytes) . "</br>"; echo PhpType::bytes2signedInt($bytes) . "</br>";
// Get unsigned integer from WORD interpretation // Get unsigned integer from WORD interpretation
echo "<h3>WORD to integer </h3>\n"; echo "<h3>WORD to integer </h3>\n";
foreach($values as $bytes) foreach ($values as $bytes)
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; echo PhpType::bytes2unsignedInt($bytes) . "</br>";
// Get string from STRING interpretation // Get string from STRING interpretation
echo "<h3>STRING to string </h3>\n"; echo "<h3>STRING to string </h3>\n";
echo PhpType::bytes2string($recData) . "</br>"; echo PhpType::bytes2string($recData) . "</br>";
?>

@ -1,19 +1,18 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
try { try {
// FC 1 // FC 1
$recData = $modbus->readCoils(0, 12288, 12); $recData = $modbus->readCoils(0, 12288, 12);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information

@ -1,25 +1,26 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
// Data to be writen // Data to be written - supports both 0/1 and booleans (true, false)
$data = array(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, $data = array(
TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, 1, 0, 1, 1, 0, 1, 1, 1,
FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, 1, 1, 1, 1, 0, 0, 0, 0,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE); 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
);
try { try {
// FC15 // FC15
$modbus->writeMultipleCoils(0, 12288, $data); $modbus->writeMultipleCoils(0, 12288, $data);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information

@ -1,6 +1,6 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
@ -10,14 +10,13 @@ $data = array(10, -1000, 2000, 3.0);
$dataTypes = array("WORD", "INT", "DINT", "REAL"); $dataTypes = array("WORD", "INT", "DINT", "REAL");
try { try {
// FC16 // FC16
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes); $modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information

@ -1,20 +1,19 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
try { try {
// FC 2 // FC 2
// read 2 input bits from address 0x0 (Wago input image) // read 2 input bits from address 0x0 (Wago input image)
$recData = $modbus->readInputDiscretes(0, 0, 2); $recData = $modbus->readInputDiscretes(0, 0, 2);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information

@ -1,6 +1,6 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
@ -8,21 +8,18 @@ $modbus = new ModbusMaster("192.192.15.51", "UDP");
// Data to be writen // Data to be writen
$bitValue = true; $bitValue = true;
$bitNumber = 2; $bitNumber = 2;
$andMask = 0xFFFF ^ pow(2, $bitNumber) ; $andMask = 0xFFFF ^ pow(2, $bitNumber);
$orMask = 0x0000 ^ (pow(2, $bitNumber) * $bitValue ) ; $orMask = 0x0000 ^ (pow(2, $bitNumber) * $bitValue);
try { try {
// FC22 // FC22
$modbus->maskWriteRegister(0, 12288, $andMask, $orMask); $modbus->maskWriteRegister(0, 12288, $andMask, $orMask);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information
echo $modbus; echo $modbus;
?>

@ -1,6 +1,6 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
@ -10,14 +10,13 @@ $data = array(10, -1000, 2000, 3.0);
$dataTypes = array("WORD", "INT", "DINT", "REAL"); $dataTypes = array("WORD", "INT", "DINT", "REAL");
try { try {
// FC23 // FC23
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes); $recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information
@ -27,5 +26,3 @@ echo "</br>Status:</br>" . $modbus;
echo "</br>Data:</br>"; echo "</br>Data:</br>";
print_r($recData); print_r($recData);
echo "</br>"; echo "</br>";
?>

@ -1,19 +1,18 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
try { try {
// FC 3 // FC 3
$recData = $modbus->readMultipleRegisters(0, 12288, 6); $recData = $modbus->readMultipleRegisters(0, 12288, 6);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information
@ -23,4 +22,3 @@ echo "</br>Status:</br>" . $modbus;
echo "</br>Data:</br>"; echo "</br>Data:</br>";
print_r($recData); print_r($recData);
echo "</br>"; echo "</br>";
?>

@ -1,19 +1,18 @@
<?php <?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php'; use PHPModbus\ModbusMasterUdp;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMasterUdp("192.192.15.51"); $modbus = new ModbusMasterUdp("192.192.15.51");
try { try {
// Read input discretes - FC 4 // Read input discretes - FC 4
$recData = $modbus->readMultipleInputRegisters(0, 0, 2); $recData = $modbus->readMultipleInputRegisters(0, 0, 2);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
var_dump($recData); var_dump($recData);

@ -1,24 +1,23 @@
<?php <?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php'; use PHPModbus\ModbusMasterUdp;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMasterUdp("192.192.15.51"); $modbus = new ModbusMasterUdp("192.192.15.51");
// Data to be writen - TRUE, FALSE // Data to be writen - TRUE, FALSE
$data_true = array(TRUE); $data_true = array(true);
$data_false = array(FALSE); $data_false = array(false);
try { try {
// Write single coil - FC5 // Write single coil - FC5
$modbus->writeSingleCoil(0, 12288, $data_true); $modbus->writeSingleCoil(0, 12288, $data_true);
$modbus->writeSingleCoil(0, 12289, $data_false); $modbus->writeSingleCoil(0, 12289, $data_false);
$modbus->writeSingleCoil(0, 12290, $data_true); $modbus->writeSingleCoil(0, 12290, $data_true);
$modbus->writeSingleCoil(0, 12291, $data_false); $modbus->writeSingleCoil(0, 12291, $data_false);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }

@ -1,26 +1,22 @@
<?php <?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; use PHPModbus\ModbusMaster;
// Create Modbus object // Create Modbus object
$modbus = new ModbusMaster("192.192.15.51", "UDP"); $modbus = new ModbusMaster("192.192.15.51", "UDP");
// Data to be writen // Data to be writen
$data = array(-1000); $data = array(-1000);
$dataTypes = array("INT");
try { try {
// FC6 // FC6
$modbus->writeSingleRegister(0, 12288, $data, $dataTypes); $modbus->writeSingleRegister(0, 12288, $data);
} } catch (Exception $e) {
catch (Exception $e) { // Print error information if any
// Print error information if any echo $modbus;
echo $modbus; echo $e;
echo $e; exit;
exit;
} }
// Print status information // Print status information
echo $modbus; echo $modbus;
?>
Loading…
Cancel
Save