From 4256b6cef7cad32efb7570581b27da109a31ae46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 20 Nov 2016 11:29:09 +0100 Subject: [PATCH] fixed the examples --- README.md | 53 +++++++++------ examples/example_750841_Mmemory.php | 100 ++++++++++++++-------------- examples/example_datatype.php | 41 ++++++------ examples/example_fc1.php | 21 +++--- examples/example_fc15.php | 29 ++++---- examples/example_fc16.php | 19 +++--- examples/example_fc2.php | 23 +++---- examples/example_fc22.php | 23 +++---- examples/example_fc23.php | 19 +++--- examples/example_fc3.php | 20 +++--- examples/example_fc4.php | 19 +++--- examples/example_fc5.php | 27 ++++---- examples/example_fc6.php | 20 +++--- 13 files changed, 204 insertions(+), 210 deletions(-) diff --git a/README.md b/README.md index 48069d6..f478cd8 100644 --- a/README.md +++ b/README.md @@ -2,38 +2,44 @@ 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** > > 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 - * FC1 - Read coils - * FC2 - Read input discretes - * FC3 - Read holding registers - * FC4 - Read holding input registers - * FC5 - Write single coil - * FC6 - Write single register - * FC15 - Write multiple coils - * FC16 - Write multiple registers - * FC23 - Read/Write multiple registers - -Example: + * FC1 - Read coils + * FC2 - Read input discretes + * FC3 - Read holding registers + * FC4 - Read holding input registers + * FC5 - Write single coil + * FC6 - Write single register + * FC15 - Write multiple coils + * FC16 - Write multiple registers + * FC23 - Read/Write multiple registers + + +## Requirements + + * The PHP extension php_sockets.dll should be enabled (server php.ini file) + + +## Example ```php -require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php'; +use PHPModbus/ModbusMaster; +use PHPModbus/PhpType; // Modbus master UDP $modbus = new ModbusMaster("192.168.1.1", "UDP"); // Read multiple registers try { $recData = $modbus->readMultipleRegisters(0, 12288, 5); -} -catch (Exception $e) { +} catch (Exception $e) { // Print error information if any echo $modbus; echo $e; @@ -43,7 +49,14 @@ catch (Exception $e) { 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) diff --git a/examples/example_750841_Mmemory.php b/examples/example_750841_Mmemory.php index 6039357..452b801 100644 --- a/examples/example_750841_Mmemory.php +++ b/examples/example_750841_Mmemory.php @@ -1,67 +1,65 @@ readMultipleRegisters($moduleId, $reference, $quantity); -} -catch (Exception $e) { - echo $modbus; - echo $e; - exit; + // FC 3 + $moduleId = 0; + $reference = 12288; + $mw0address = 12288; + $quantity = 6; + $recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity); +} catch (Exception $e) { + echo $modbus; + echo $e; + exit; } ?> - - - - WAGO 750-841 M-memory dump - - -

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

- + + + + WAGO 750-841 M-memory dump + + +

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

+ + +

M-memory dump

-

M-memory dump

+ + + + + + + + + + + + + +
Modbus addressMWxvalue
MW
- - - - - - - - - - - - - -
Modbus addressMWxvalue
MW
+

Modbus class status

-

Modbus class status

- +
- - + + diff --git a/examples/example_datatype.php b/examples/example_datatype.php index d620e08..86fa4dc 100644 --- a/examples/example_datatype.php +++ b/examples/example_datatype.php @@ -1,20 +1,20 @@ readMultipleRegisters(0, 12288, 10); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // 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 @@ -28,18 +28,18 @@ $values = array_chunk($recData, 4); // Get float from REAL interpretation echo "

REAL to Float

\n"; -foreach($values as $bytes) - echo PhpType::bytes2float($bytes) . "
"; +foreach ($values as $bytes) + echo PhpType::bytes2float($bytes) . "
"; // Get integer from DINT interpretation echo "

DINT to integer

\n"; -foreach($values as $bytes) - echo PhpType::bytes2signedInt($bytes) . "
"; +foreach ($values as $bytes) + echo PhpType::bytes2signedInt($bytes) . "
"; // Get integer of float from DINT interpretation echo "

DWORD to integer (or float)

\n"; -foreach($values as $bytes) - echo PhpType::bytes2unsignedInt($bytes) . "
"; +foreach ($values as $bytes) + echo PhpType::bytes2unsignedInt($bytes) . "
"; echo "

16 bit types

\n"; // Chunk the data array to set of 4 bytes @@ -47,15 +47,14 @@ $values = array_chunk($recData, 2); // Get signed integer from INT interpretation echo "

INT to integer

\n"; -foreach($values as $bytes) - echo PhpType::bytes2signedInt($bytes) . "
"; +foreach ($values as $bytes) + echo PhpType::bytes2signedInt($bytes) . "
"; // Get unsigned integer from WORD interpretation echo "

WORD to integer

\n"; -foreach($values as $bytes) - echo PhpType::bytes2unsignedInt($bytes) . "
"; +foreach ($values as $bytes) + echo PhpType::bytes2unsignedInt($bytes) . "
"; // Get string from STRING interpretation echo "

STRING to string

\n"; echo PhpType::bytes2string($recData) . "
"; -?> \ No newline at end of file diff --git a/examples/example_fc1.php b/examples/example_fc1.php index 724da2c..d6c7497 100644 --- a/examples/example_fc1.php +++ b/examples/example_fc1.php @@ -1,19 +1,18 @@ readCoils(0, 12288, 12); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // FC 1 + $recData = $modbus->readCoils(0, 12288, 12); +} catch (Exception $e) { + // Print error information if any + echo $modbus; + echo $e; + exit; } // Print status information @@ -21,5 +20,5 @@ echo "
Status:
" . $modbus; // Print read data echo "
Data:
"; -var_dump($recData); -echo "
"; \ No newline at end of file +var_dump($recData); +echo "
"; diff --git a/examples/example_fc15.php b/examples/example_fc15.php index 962f35d..23de38b 100644 --- a/examples/example_fc15.php +++ b/examples/example_fc15.php @@ -1,25 +1,26 @@ writeMultipleCoils(0, 12288, $data); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // FC15 + $modbus->writeMultipleCoils(0, 12288, $data); +} catch (Exception $e) { + // Print error information if any + echo $modbus; + echo $e; + exit; } // Print status information diff --git a/examples/example_fc16.php b/examples/example_fc16.php index f2983d6..1bff6f2 100644 --- a/examples/example_fc16.php +++ b/examples/example_fc16.php @@ -1,6 +1,6 @@ writeMultipleRegister(0, 12288, $data, $dataTypes); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // 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; -?> \ No newline at end of file +?> diff --git a/examples/example_fc2.php b/examples/example_fc2.php index 62c0157..1563938 100644 --- a/examples/example_fc2.php +++ b/examples/example_fc2.php @@ -1,20 +1,19 @@ readInputDiscretes(0, 0, 2); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // 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 @@ -22,5 +21,5 @@ echo "
Status:
" . $modbus; // Print read data echo "
Data:
"; -var_dump($recData); -echo "
"; \ No newline at end of file +var_dump($recData); +echo "
"; diff --git a/examples/example_fc22.php b/examples/example_fc22.php index 24e235f..34ccd2e 100644 --- a/examples/example_fc22.php +++ b/examples/example_fc22.php @@ -1,6 +1,6 @@ maskWriteRegister(0, 12288, $andMask, $orMask); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // FC22 + $modbus->maskWriteRegister(0, 12288, $andMask, $orMask); +} catch (Exception $e) { + // Print error information if any + echo $modbus; + echo $e; + exit; } // Print status information echo $modbus; - -?> diff --git a/examples/example_fc23.php b/examples/example_fc23.php index 310c295..0fa0ebe 100644 --- a/examples/example_fc23.php +++ b/examples/example_fc23.php @@ -1,6 +1,6 @@ readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // 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 @@ -27,5 +26,3 @@ echo "
Status:
" . $modbus; echo "
Data:
"; print_r($recData); echo "
"; - -?> \ No newline at end of file diff --git a/examples/example_fc3.php b/examples/example_fc3.php index 3fd6bc6..859e1c0 100644 --- a/examples/example_fc3.php +++ b/examples/example_fc3.php @@ -1,19 +1,18 @@ readMultipleRegisters(0, 12288, 6); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // FC 3 + $recData = $modbus->readMultipleRegisters(0, 12288, 6); +} catch (Exception $e) { + // Print error information if any + echo $modbus; + echo $e; + exit; } // Print status information @@ -21,6 +20,5 @@ echo "
Status:
" . $modbus; // Print read data echo "
Data:
"; -print_r($recData); +print_r($recData); echo "
"; -?> \ No newline at end of file diff --git a/examples/example_fc4.php b/examples/example_fc4.php index cb75cf9..94478c6 100644 --- a/examples/example_fc4.php +++ b/examples/example_fc4.php @@ -1,19 +1,18 @@ readMultipleInputRegisters(0, 0, 2); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // Read input discretes - FC 4 + $recData = $modbus->readMultipleInputRegisters(0, 0, 2); +} catch (Exception $e) { + // Print error information if any + echo $modbus; + echo $e; + exit; } -var_dump($recData); \ No newline at end of file +var_dump($recData); diff --git a/examples/example_fc5.php b/examples/example_fc5.php index 764a805..7f47680 100644 --- a/examples/example_fc5.php +++ b/examples/example_fc5.php @@ -1,24 +1,23 @@ 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; + // 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; } diff --git a/examples/example_fc6.php b/examples/example_fc6.php index 4db6b04..1d7c733 100644 --- a/examples/example_fc6.php +++ b/examples/example_fc6.php @@ -1,26 +1,22 @@ writeSingleRegister(0, 12288, $data, $dataTypes); -} -catch (Exception $e) { - // Print error information if any - echo $modbus; - echo $e; - exit; + // FC6 + $modbus->writeSingleRegister(0, 12288, $data); +} catch (Exception $e) { + // Print error information if any + echo $modbus; + echo $e; + exit; } // Print status information echo $modbus; - -?> \ No newline at end of file