Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ac0018444 | ||
|
|
5300b2a867 | ||
|
|
c676c06e2e | ||
|
|
697daedba3 | ||
|
|
cd52263b8e | ||
|
|
a2bde03574 | ||
|
|
24d16bc1aa |
@@ -0,0 +1,4 @@
|
|||||||
|
/vendor
|
||||||
|
composer.lock
|
||||||
|
|
||||||
|
*~
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
a88b5a87fb4cd49f16ce72062cce19d55f1fbf98 0.2
|
|
||||||
a7f90c18859d31922bdd8c61299e2790f5e2eb1a 0.3
|
|
||||||
bdf8b3f414c4d08fc0edf9fd1953f55db26e4bac 0.4
|
|
||||||
1e09b0da7d8c62a9a292ee51540feac9de4c1222 0.4.1
|
|
||||||
e1262f028e180c2719564f840ffa1a9f8672b6be 0.5
|
|
||||||
079f8ac1f49faf4e6bbb84d62c39eee1507805f2 0.6
|
|
||||||
5885b1a0d0dc89577eeb871ee2c3fba8e70dd4fb 0.7
|
|
||||||
+89
-62
@@ -1,62 +1,89 @@
|
|||||||
The Phpmodbus License, Version 1
|
+---------------------------------------------------------+
|
||||||
============================
|
| ACKNOWLEDGEMENTS |
|
||||||
|
+---------------------------------------------------------+
|
||||||
Copyright (c) 2004, 2013 Jan Krakora
|
|
||||||
All rights reserved.
|
This project is based on work by Jan Krákora, (c) 2004-2013.
|
||||||
|
The original license is reproduced below.
|
||||||
This license is a legal agreement between you and Jan Krakora (the "Author")
|
|
||||||
for the use of Phpmodbus (the "Software"). By obtaining, using and/or
|
https://code.google.com/archive/p/phpmodbus/
|
||||||
copying the Software, you agree that you have read, understood, and will
|
|
||||||
comply with the terms and conditions of this license.
|
Further improvements - John S. Long and other contributors
|
||||||
|
|
||||||
|
https://github.com/adduc/phpmodbus
|
||||||
PERMITTED USE
|
|
||||||
-------------
|
Cleanup and namespaces - Ondřej Hruška, 2016
|
||||||
|
|
||||||
You are permitted to use, copy, modify, and distribute the Software and its
|
https://github.com/MightyPork/phpmodbus
|
||||||
documentation, with or without modification, for any purpose, provided that
|
|
||||||
the following conditions are met:
|
|
||||||
|
+---------------------------------------------------------+
|
||||||
1. A copy of this license agreement must be included with the distribution.
|
| ORIGINAL LICENSE |
|
||||||
|
+---------------------------------------------------------+
|
||||||
2. Redistributions of source code must retain the above copyright notice in
|
|
||||||
all source code files.
|
|
||||||
|
The Phpmodbus License, Version 1
|
||||||
3. Redistributions in binary form must reproduce the above copyright notice
|
============================
|
||||||
in the documentation and/or other materials provided with the distribution.
|
|
||||||
|
Copyright (c) 2004, 2013 Jan Krakora
|
||||||
4. Products derived from the Software must include an acknowledgment that
|
All rights reserved.
|
||||||
they are derived from "Phpmodbus" in their documentation and/or other
|
|
||||||
materials provided with the distribution.
|
This license is a legal agreement between you and Jan Krakora (the "Author")
|
||||||
|
for the use of Phpmodbus (the "Software"). By obtaining, using and/or
|
||||||
5. The name "Phpmodbus" must not be used to endorse or promote products
|
copying the Software, you agree that you have read, understood, and will
|
||||||
derived from the Software without prior written permission from Author.
|
comply with the terms and conditions of this license.
|
||||||
|
|
||||||
6. Products derived from the Software may not be called "Phpmodbus",
|
|
||||||
nor may "Phpmodbus" appear in their name, without prior written
|
PERMITTED USE
|
||||||
permission from Author.
|
-------------
|
||||||
|
|
||||||
|
You are permitted to use, copy, modify, and distribute the Software and its
|
||||||
INDEMNITY
|
documentation, with or without modification, for any purpose, provided that
|
||||||
---------
|
the following conditions are met:
|
||||||
|
|
||||||
You agree to indemnify and hold harmless the Author and any contributors
|
1. A copy of this license agreement must be included with the distribution.
|
||||||
for any direct, indirect, incidental, or consequential third-party claims,
|
|
||||||
actions or suits, as well as any related expenses, liabilities, damages,
|
2. Redistributions of source code must retain the above copyright notice in
|
||||||
settlements or fees arising from your use or misuse of the Software,
|
all source code files.
|
||||||
or a violation of any terms of this license.
|
|
||||||
|
3. Redistributions in binary form must reproduce the above copyright notice
|
||||||
|
in the documentation and/or other materials provided with the distribution.
|
||||||
DISCLAIMER OF WARRANTY
|
|
||||||
----------------------
|
4. Products derived from the Software must include an acknowledgment that
|
||||||
|
they are derived from "Phpmodbus" in their documentation and/or other
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND
|
materials provided with the distribution.
|
||||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
5. The name "Phpmodbus" must not be used to endorse or promote products
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR
|
derived from the Software without prior written permission from Author.
|
||||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
||||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
6. Products derived from the Software may not be called "Phpmodbus",
|
||||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
nor may "Phpmodbus" appear in their name, without prior written
|
||||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
permission from Author.
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
INDEMNITY
|
||||||
|
---------
|
||||||
|
|
||||||
|
You agree to indemnify and hold harmless the Author and any contributors
|
||||||
|
for any direct, indirect, incidental, or consequential third-party claims,
|
||||||
|
actions or suits, as well as any related expenses, liabilities, damages,
|
||||||
|
settlements or fees arising from your use or misuse of the Software,
|
||||||
|
or a violation of any terms of this license.
|
||||||
|
|
||||||
|
|
||||||
|
DISCLAIMER OF WARRANTY
|
||||||
|
----------------------
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
End of file.
|
||||||
@@ -1,44 +1,49 @@
|
|||||||
phpmodbus
|
# phpmodbus
|
||||||
=========
|
|
||||||
This project deals with an implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP.
|
|
||||||
It's a copy of the releases from the project page over at [Google Code](https://code.google.com/p/phpmodbus/) with
|
|
||||||
composer support added.
|
|
||||||
|
|
||||||
Features
|
Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP.
|
||||||
--------
|
|
||||||
|
|
||||||
* Modbus master
|
This is a fork of the original project at https://code.google.com/p/phpmodbus/
|
||||||
* 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
|
> **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:
|
||||||
|
* 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:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
// Modbus master UDP
|
require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php';
|
||||||
$modbus = new ModbusMaster("192.168.1.1", "UDP");
|
|
||||||
// Read multiple registers
|
// Modbus master UDP
|
||||||
try {
|
$modbus = new ModbusMaster("192.168.1.1", "UDP");
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
// Read multiple registers
|
||||||
}
|
try {
|
||||||
catch (Exception $e) {
|
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
||||||
// Print error information if any
|
}
|
||||||
echo $modbus;
|
catch (Exception $e) {
|
||||||
echo $e;
|
// Print error information if any
|
||||||
exit;
|
echo $modbus;
|
||||||
}
|
echo $e;
|
||||||
// Print data in string format
|
exit;
|
||||||
echo PhpType::bytes2string($recData);
|
}
|
||||||
|
// Print data in string format
|
||||||
|
echo PhpType::bytes2string($recData);
|
||||||
```
|
```
|
||||||
|
|
||||||
For more see [documentation][] or [FAQ][].
|
For more see [http://code.google.com/p/phpmodbus/downloads/list documentation] or [http://code.google.com/p/phpmodbus/wiki/FAQ FAQ].
|
||||||
|
|
||||||
[documentation]: https://code.google.com/p/phpmodbus/downloads/list
|
Note:
|
||||||
[FAQ]: https://code.google.com/p/phpmodbus/wiki/FAQ
|
* The PHP extension php_sockets.dll should be enabled (server php.ini file)
|
||||||
|
|||||||
+18
-14
@@ -1,17 +1,21 @@
|
|||||||
{
|
{
|
||||||
"name": "adduc/phpmodbus",
|
"name": "MightyPork/phpmodbus",
|
||||||
"description": "Composer version of PhpModBus",
|
"keywords": ["phpmodbus","modbus"],
|
||||||
"license": "LGPL",
|
"description": "PhpModbus with namespaces and updated to PHP 7",
|
||||||
"authors": [
|
"license": "LGPL",
|
||||||
{
|
"require": {
|
||||||
"name": "Honza Krakora",
|
"php": "^5.3.2 || ^7.0",
|
||||||
"email": "krakora.jan@googlemail.com"
|
"ext-sockets": "*"
|
||||||
}
|
},
|
||||||
],
|
"authors": [
|
||||||
"autoload": {
|
{
|
||||||
"classmap": [ "Phpmodbus" ]
|
"name": "Honza Krakora",
|
||||||
},
|
"email": "krakora.jan@googlemail.com"
|
||||||
"require": {
|
|
||||||
"ext-sockets": "*"
|
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"PHPModbus\\": "src/"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Those examples have not been updated to use namespaces.
|
||||||
|
|
||||||
|
They may not work. Just for reference.
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
||||||
|
|
||||||
|
// Create Modbus object
|
||||||
|
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
||||||
|
|
||||||
|
// Data to be writen
|
||||||
|
$bitValue = true;
|
||||||
|
$bitNumber = 2;
|
||||||
|
$andMask = 0xFFFF ^ pow(2, $bitNumber) ;
|
||||||
|
$orMask = 0x0000 ^ (pow(2, $bitNumber) * $bitValue ) ;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -1,133 +1,136 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
namespace PHPModbus;
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
/**
|
||||||
* with this package in the file license.txt.
|
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
||||||
*
|
*
|
||||||
* @author Jan Krakora
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
* @copyright Copyright (c) 2004, 2013 Jan Krakora
|
* with this package in the file license.txt.
|
||||||
* @license PhpModbus license
|
*
|
||||||
* @category Phpmodbus
|
* @author Jan Krakora
|
||||||
* @package Phpmodbus
|
* @copyright Copyright (c) 2004, 2013 Jan Krakora
|
||||||
* @version $id$
|
* @license PhpModbus license
|
||||||
*/
|
* @category Phpmodbus
|
||||||
|
* @package Phpmodbus
|
||||||
/**
|
* @version $id$
|
||||||
* IecType
|
*/
|
||||||
*
|
|
||||||
* The class includes set of IEC-1131 data type functions that converts a PHP
|
/**
|
||||||
* data types to a IEC data type.
|
* IecType
|
||||||
*
|
*
|
||||||
* @author Jan Krakora
|
* The class includes set of IEC-1131 data type functions that converts a PHP
|
||||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora
|
* data types to a IEC data type.
|
||||||
* @package Phpmodbus
|
*
|
||||||
*/
|
* @author Jan Krakora
|
||||||
class IecType {
|
* @copyright Copyright (c) 2004, 2010 Jan Krakora
|
||||||
|
* @package Phpmodbus
|
||||||
/**
|
*/
|
||||||
* iecBYTE
|
class IecType {
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 BYTE data type
|
/**
|
||||||
*
|
* iecBYTE
|
||||||
* @param value value from 0 to 255
|
*
|
||||||
* @return value IEC BYTE data type
|
* Converts a value to IEC-1131 BYTE data type
|
||||||
*
|
*
|
||||||
*/
|
* @param value value from 0 to 255
|
||||||
public static function iecBYTE($value) {
|
* @return value IEC BYTE data type
|
||||||
return chr($value & 0xFF);
|
*
|
||||||
}
|
*/
|
||||||
|
public static function iecBYTE($value) {
|
||||||
/**
|
return chr($value & 0xFF);
|
||||||
* iecINT
|
}
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 INT data type
|
/**
|
||||||
*
|
* iecINT
|
||||||
* @param value value to be converted
|
*
|
||||||
* @return value IEC-1131 INT data type
|
* Converts a value to IEC-1131 INT data type
|
||||||
*
|
*
|
||||||
*/
|
* @param value value to be converted
|
||||||
public static function iecINT($value) {
|
* @return value IEC-1131 INT data type
|
||||||
return self::iecBYTE(($value >> 8) & 0x00FF) .
|
*
|
||||||
self::iecBYTE(($value & 0x00FF));
|
*/
|
||||||
}
|
public static function iecINT($value) {
|
||||||
|
return self::iecBYTE(($value >> 8) & 0x00FF) .
|
||||||
/**
|
self::iecBYTE(($value & 0x00FF));
|
||||||
* iecDINT
|
}
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 DINT data type
|
/**
|
||||||
*
|
* iecDINT
|
||||||
* @param value value to be converted
|
*
|
||||||
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
|
* Converts a value to IEC-1131 DINT data type
|
||||||
* @return value IEC-1131 INT data type
|
*
|
||||||
*
|
* @param value value to be converted
|
||||||
*/
|
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
|
||||||
public static function iecDINT($value, $endianness = 0) {
|
* @return value IEC-1131 INT data type
|
||||||
// result with right endianness
|
*
|
||||||
return self::endianness($value, $endianness);
|
*/
|
||||||
}
|
public static function iecDINT($value, $endianness = 0) {
|
||||||
|
// result with right endianness
|
||||||
/**
|
return self::endianness($value, $endianness);
|
||||||
* iecREAL
|
}
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 REAL data type. The function uses function @use float2iecReal.
|
/**
|
||||||
*
|
* iecREAL
|
||||||
* @param value value to be converted
|
*
|
||||||
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
|
* Converts a value to IEC-1131 REAL data type. The function uses function @use float2iecReal.
|
||||||
* @return value IEC-1131 REAL data type
|
*
|
||||||
*/
|
* @param value value to be converted
|
||||||
public static function iecREAL($value, $endianness = 0) {
|
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
|
||||||
// iecREAL representation
|
* @return value IEC-1131 REAL data type
|
||||||
$real = self::float2iecReal($value);
|
*/
|
||||||
// result with right endianness
|
public static function iecREAL($value, $endianness = 0) {
|
||||||
return self::endianness($real, $endianness);
|
// iecREAL representation
|
||||||
}
|
$real = self::float2iecReal($value);
|
||||||
|
// result with right endianness
|
||||||
/**
|
return self::endianness($real, $endianness);
|
||||||
* float2iecReal
|
}
|
||||||
*
|
|
||||||
* This function converts float value to IEC-1131 REAL single precision form.
|
/**
|
||||||
*
|
* float2iecReal
|
||||||
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or
|
*
|
||||||
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] or
|
* This function converts float value to IEC-1131 REAL single precision form.
|
||||||
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
*
|
||||||
*
|
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or
|
||||||
* @param float value to be converted
|
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] or
|
||||||
* @return value IEC REAL data type
|
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
||||||
*/
|
*
|
||||||
private static function float2iecReal($value) {
|
* @param float value to be converted
|
||||||
// get float binary string
|
* @return value IEC REAL data type
|
||||||
$float = pack("f", $value);
|
*/
|
||||||
// set 32-bit unsigned integer of the float
|
private static function float2iecReal($value) {
|
||||||
$w = unpack("L", $float);
|
// get float binary string
|
||||||
return $w[1];
|
$float = pack("f", $value);
|
||||||
}
|
// set 32-bit unsigned integer of the float
|
||||||
|
$w = unpack("L", $float);
|
||||||
/**
|
return $w[1];
|
||||||
* endianness
|
}
|
||||||
*
|
|
||||||
* Make endianess as required.
|
/**
|
||||||
* For more see http://en.wikipedia.org/wiki/Endianness
|
* endianness
|
||||||
*
|
*
|
||||||
* @param int $value
|
* Make endianess as required.
|
||||||
* @param bool $endianness
|
* For more see http://en.wikipedia.org/wiki/Endianness
|
||||||
* @return int
|
*
|
||||||
*/
|
* @param int $value
|
||||||
private static function endianness($value, $endianness = 0) {
|
* @param bool $endianness
|
||||||
if ($endianness == 0)
|
* @return int
|
||||||
return
|
*/
|
||||||
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
private static function endianness($value, $endianness = 0) {
|
||||||
self::iecBYTE(($value & 0x000000FF)) .
|
if ($endianness == 0)
|
||||||
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
return
|
||||||
self::iecBYTE(($value >> 16) & 0x000000FF);
|
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
||||||
else
|
self::iecBYTE(($value & 0x000000FF)) .
|
||||||
return
|
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
||||||
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
self::iecBYTE(($value >> 16) & 0x000000FF);
|
||||||
self::iecBYTE(($value >> 16) & 0x000000FF) .
|
else
|
||||||
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
return
|
||||||
self::iecBYTE(($value & 0x000000FF));
|
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
||||||
}
|
self::iecBYTE(($value >> 16) & 0x000000FF) .
|
||||||
|
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
||||||
}
|
self::iecBYTE(($value & 0x000000FF));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
||||||
*
|
*
|
||||||
@@ -15,9 +18,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/IecType.php';
|
|
||||||
require_once dirname(__FILE__) . '/PhpType.php';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ModbusMaster
|
* ModbusMaster
|
||||||
*
|
*
|
||||||
@@ -32,6 +32,7 @@ require_once dirname(__FILE__) . '/PhpType.php';
|
|||||||
* - FC 6: write single register
|
* - FC 6: write single register
|
||||||
* - FC 15: write multiple coils
|
* - FC 15: write multiple coils
|
||||||
* - FC 16: write multiple registers
|
* - FC 16: write multiple registers
|
||||||
|
* - FC 22: mask write register
|
||||||
* - FC 23: read write registers
|
* - FC 23: read write registers
|
||||||
*
|
*
|
||||||
* @author Jan Krakora
|
* @author Jan Krakora
|
||||||
@@ -1049,6 +1050,106 @@ class ModbusMaster {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* maskWriteRegister
|
||||||
|
*
|
||||||
|
* Modbus function FC22(0x16) - Mask Write Register.
|
||||||
|
*
|
||||||
|
* This function alter single bit(s) at {@link $reference} position of
|
||||||
|
* memory of a Modbus device given by {@link $unitId}.
|
||||||
|
*
|
||||||
|
* Result = (Current Contents AND And_Mask) OR (Or_Mask AND (NOT And_Mask))
|
||||||
|
*
|
||||||
|
* @param int $unitId usually ID of Modbus device
|
||||||
|
* @param int $reference Reference in the device memory (e.g. in device WAGO 750-841, memory MW0 starts at address 12288)
|
||||||
|
* @param int $andMask
|
||||||
|
* @param int $orMask
|
||||||
|
* @return bool Success flag
|
||||||
|
*/
|
||||||
|
function maskWriteRegister($unitId, $reference, $andMask, $orMask){
|
||||||
|
$this->status .= "maskWriteRegister: START\n";
|
||||||
|
// connect
|
||||||
|
$this->connect();
|
||||||
|
// send FC22
|
||||||
|
$packet = $this->maskWriteRegisterPacketBuilder($unitId, $reference, $andMask, $orMask);
|
||||||
|
$this->status .= $this->printPacket($packet);
|
||||||
|
$this->send($packet);
|
||||||
|
// receive response
|
||||||
|
$rpacket = $this->rec();
|
||||||
|
$this->status .= $this->printPacket($rpacket);
|
||||||
|
// parse packet
|
||||||
|
$this->maskWriteRegisterParser($rpacket);
|
||||||
|
// disconnect
|
||||||
|
$this->disconnect();
|
||||||
|
$this->status .= "maskWriteRegister: DONE\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fc22
|
||||||
|
*
|
||||||
|
* Alias to {@link maskWriteRegister} method
|
||||||
|
*
|
||||||
|
* @param int $unitId
|
||||||
|
* @param int $reference
|
||||||
|
* @param int $andMask
|
||||||
|
* @param int $orMask
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function fc22($unitId, $reference, $andMask, $orMask){
|
||||||
|
return $this->maskWriteRegister($unitId, $reference, $andMask, $orMask);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* maskWriteRegisterPacketBuilder
|
||||||
|
*
|
||||||
|
* Packet builder FC22 - MASK WRITE register
|
||||||
|
*
|
||||||
|
* @param int $unitId
|
||||||
|
* @param int $reference
|
||||||
|
* @param int $andMask
|
||||||
|
* @param int $orMask
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function maskWriteRegisterPacketBuilder($unitId, $reference, $andMask, $orMask){
|
||||||
|
$dataLen = 0;
|
||||||
|
// build data section
|
||||||
|
$buffer1 = "";
|
||||||
|
// build body
|
||||||
|
$buffer2 = "";
|
||||||
|
$buffer2 .= iecType::iecBYTE(22); // FC 22 = 22(0x16)
|
||||||
|
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
|
||||||
|
$buffer2 .= iecType::iecINT($andMask); // AND mask
|
||||||
|
$buffer2 .= iecType::iecINT($orMask); // OR mask
|
||||||
|
$dataLen += 7;
|
||||||
|
// build header
|
||||||
|
$buffer3 = '';
|
||||||
|
$buffer3 .= iecType::iecINT(rand(0,65000)); // transaction ID
|
||||||
|
$buffer3 .= iecType::iecINT(0); // protocol ID
|
||||||
|
$buffer3 .= iecType::iecINT($dataLen + 1); // lenght
|
||||||
|
$buffer3 .= iecType::iecBYTE($unitId); //unit ID
|
||||||
|
// return packet string
|
||||||
|
return $buffer3. $buffer2. $buffer1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* maskWriteRegisterParser
|
||||||
|
*
|
||||||
|
* FC22 response parser
|
||||||
|
*
|
||||||
|
* @param string $packet
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function maskWriteRegisterParser($packet){
|
||||||
|
$this->responseCode($packet);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* readWriteRegisters
|
* readWriteRegisters
|
||||||
*
|
*
|
||||||
@@ -1,49 +1,50 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
namespace PHPModbus;
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
/**
|
||||||
* with this package in the file license.txt.
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
*
|
||||||
*
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* with this package in the file license.txt.
|
||||||
* @license PhpModbus license
|
*
|
||||||
* @category Phpmodbus
|
*
|
||||||
* @tutorial Phpmodbus.pkg
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
* @package Phpmodbus
|
* @license PhpModbus license
|
||||||
* @version $id$
|
* @category Phpmodbus
|
||||||
*
|
* @tutorial Phpmodbus.pkg
|
||||||
*/
|
* @package Phpmodbus
|
||||||
|
* @version $id$
|
||||||
require_once dirname(__FILE__) . '/ModbusMaster.php';
|
*
|
||||||
|
*/
|
||||||
/**
|
|
||||||
* ModbusMasterTcp
|
/**
|
||||||
*
|
* ModbusMasterTcp
|
||||||
* This class deals with the MODBUS master using TCP. Extends ModbusMaster class.
|
*
|
||||||
*
|
* This class deals with the MODBUS master using TCP. Extends ModbusMaster class.
|
||||||
* Implemented MODBUS functions:
|
*
|
||||||
* - FC 1: read coils
|
* Implemented MODBUS functions:
|
||||||
* - FC 3: read multiple registers
|
* - FC 1: read coils
|
||||||
* - FC 15: write multiple coils
|
* - FC 3: read multiple registers
|
||||||
* - FC 16: write multiple registers
|
* - FC 15: write multiple coils
|
||||||
* - FC 23: read write registers
|
* - FC 16: write multiple registers
|
||||||
*
|
* - FC 23: read write registers
|
||||||
* @author Jan Krakora
|
*
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* @author Jan Krakora
|
||||||
* @package Phpmodbus
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
* @package Phpmodbus
|
||||||
*/
|
*
|
||||||
class ModbusMasterTcp extends ModbusMaster {
|
*/
|
||||||
/**
|
class ModbusMasterTcp extends ModbusMaster {
|
||||||
* ModbusMasterTcp
|
/**
|
||||||
*
|
* ModbusMasterTcp
|
||||||
* This is the constructor that defines {@link $host} IP address of the object.
|
*
|
||||||
*
|
* This is the constructor that defines {@link $host} IP address of the object.
|
||||||
* @param String $host An IP address of a Modbus TCP device. E.g. "192.168.1.1".
|
*
|
||||||
*/
|
* @param String $host An IP address of a Modbus TCP device. E.g. "192.168.1.1".
|
||||||
function ModbusMasterTcp($host){
|
*/
|
||||||
$this->host = $host;
|
function ModbusMasterTcp($host){
|
||||||
$this->socket_protocol = "TCP";
|
$this->host = $host;
|
||||||
}
|
$this->socket_protocol = "TCP";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -1,50 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
namespace PHPModbus;
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
/**
|
||||||
* with this package in the file license.txt.
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
*
|
||||||
*
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* with this package in the file license.txt.
|
||||||
* @license PhpModbus license
|
*
|
||||||
* @category Phpmodbus
|
*
|
||||||
* @tutorial Phpmodbus.pkg
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
* @package Phpmodbus
|
* @license PhpModbus license
|
||||||
* @version $id$
|
* @category Phpmodbus
|
||||||
*
|
* @tutorial Phpmodbus.pkg
|
||||||
*/
|
* @package Phpmodbus
|
||||||
|
* @version $id$
|
||||||
require_once dirname(__FILE__) . '/ModbusMaster.php';
|
*
|
||||||
|
*/
|
||||||
/**
|
|
||||||
* ModbusMasterUdp
|
/**
|
||||||
*
|
* ModbusMasterUdp
|
||||||
* This class deals with the MODBUS master using UDP stack.
|
*
|
||||||
*
|
* This class deals with the MODBUS master using UDP stack.
|
||||||
* Implemented MODBUS master functions:
|
*
|
||||||
* - FC 1: read coils
|
* Implemented MODBUS master functions:
|
||||||
* - FC 3: read multiple registers
|
* - FC 1: read coils
|
||||||
* - FC 15: write multiple coils
|
* - FC 3: read multiple registers
|
||||||
* - FC 16: write multiple registers
|
* - FC 15: write multiple coils
|
||||||
* - FC 23: read write registers
|
* - FC 16: write multiple registers
|
||||||
*
|
* - FC 23: read write registers
|
||||||
* @author Jan Krakora
|
*
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* @author Jan Krakora
|
||||||
* @package Phpmodbus
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
* @package Phpmodbus
|
||||||
*/
|
*
|
||||||
class ModbusMasterUdp extends ModbusMaster {
|
*/
|
||||||
|
class ModbusMasterUdp extends ModbusMaster {
|
||||||
/**
|
|
||||||
* ModbusMasterUdp
|
/**
|
||||||
*
|
* ModbusMasterUdp
|
||||||
* This is the constructor that defines {@link $host} IP address of the object.
|
*
|
||||||
*
|
* This is the constructor that defines {@link $host} IP address of the object.
|
||||||
* @param String $host An IP address of a Modbus UDP device. E.g. "192.168.1.1".
|
*
|
||||||
*/
|
* @param String $host An IP address of a Modbus UDP device. E.g. "192.168.1.1".
|
||||||
function ModbusMasterUdp($host){
|
*/
|
||||||
$this->host = $host;
|
function ModbusMasterUdp($host){
|
||||||
$this->socket_protocol = "UDP";
|
$this->host = $host;
|
||||||
}
|
$this->socket_protocol = "UDP";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -1,249 +1,252 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
namespace PHPModbus;
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
/**
|
||||||
* with this package in the file license.txt.
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
*
|
||||||
* @author Jan Krakora
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* with this package in the file license.txt.
|
||||||
* @license PhpModbus license
|
*
|
||||||
* @category Phpmodbus
|
* @author Jan Krakora
|
||||||
* @package Phpmodbus
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
* @version $id$
|
* @license PhpModbus license
|
||||||
*
|
* @category Phpmodbus
|
||||||
*/
|
* @package Phpmodbus
|
||||||
|
* @version $id$
|
||||||
/**
|
*
|
||||||
* PhpType
|
*/
|
||||||
*
|
|
||||||
* The class includes set of methods that convert the received Modbus data
|
/**
|
||||||
* (array of bytes) to the PHP data type, i.e. signed int, unsigned int and float.
|
* PhpType
|
||||||
*
|
*
|
||||||
* @author Jan Krakora
|
* The class includes set of methods that convert the received Modbus data
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* (array of bytes) to the PHP data type, i.e. signed int, unsigned int and float.
|
||||||
* @package Phpmodbus
|
*
|
||||||
*
|
* @author Jan Krakora
|
||||||
*/
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
class PhpType {
|
* @package Phpmodbus
|
||||||
|
*
|
||||||
/**
|
*/
|
||||||
* bytes2float
|
class PhpType {
|
||||||
*
|
|
||||||
* The function converts array of 4 bytes to float. The return value
|
/**
|
||||||
* depends on order of the input bytes (endianning).
|
* bytes2float
|
||||||
*
|
*
|
||||||
* @param array $values
|
* The function converts array of 4 bytes to float. The return value
|
||||||
* @param bool $endianness
|
* depends on order of the input bytes (endianning).
|
||||||
* @return float
|
*
|
||||||
*/
|
* @param array $values
|
||||||
public static function bytes2float($values, $endianness = 0) {
|
* @param bool $endianness
|
||||||
$data = array();
|
* @return float
|
||||||
$real = 0;
|
*/
|
||||||
|
public static function bytes2float($values, $endianness = 0) {
|
||||||
// Set the array to correct form
|
$data = array();
|
||||||
$data = self::checkData($values);
|
$real = 0;
|
||||||
// Combine bytes
|
|
||||||
$real = self::combineBytes($data, $endianness);
|
// Set the array to correct form
|
||||||
// Convert the real value to float
|
$data = self::checkData($values);
|
||||||
return (float) self::real2float($real);
|
// Combine bytes
|
||||||
}
|
$real = self::combineBytes($data, $endianness);
|
||||||
|
// Convert the real value to float
|
||||||
/**
|
return (float) self::real2float($real);
|
||||||
* bytes2signedInt
|
}
|
||||||
*
|
|
||||||
* The function converts array of 2 or 4 bytes to signed integer.
|
/**
|
||||||
* The return value depends on order of the input bytes (endianning).
|
* bytes2signedInt
|
||||||
*
|
*
|
||||||
* @param array $values
|
* The function converts array of 2 or 4 bytes to signed integer.
|
||||||
* @param bool $endianness
|
* The return value depends on order of the input bytes (endianning).
|
||||||
* @return int
|
*
|
||||||
*/
|
* @param array $values
|
||||||
public static function bytes2signedInt($values, $endianness = 0) {
|
* @param bool $endianness
|
||||||
$data = array();
|
* @return int
|
||||||
$int = 0;
|
*/
|
||||||
// Set the array to correct form
|
public static function bytes2signedInt($values, $endianness = 0) {
|
||||||
$data = self::checkData($values);
|
$data = array();
|
||||||
// Combine bytes
|
$int = 0;
|
||||||
$int = self::combineBytes($data, $endianness);
|
// Set the array to correct form
|
||||||
// In the case of signed 2 byte value convert it to 4 byte one
|
$data = self::checkData($values);
|
||||||
if ((count($values) == 2) && ((0x8000 & $int) > 0)) {
|
// Combine bytes
|
||||||
$int = 0xFFFF8000 | $int;
|
$int = self::combineBytes($data, $endianness);
|
||||||
}
|
// In the case of signed 2 byte value convert it to 4 byte one
|
||||||
// Convert the value
|
if ((count($values) == 2) && ((0x8000 & $int) > 0)) {
|
||||||
return (int) self::dword2signedInt($int);
|
$int = 0xFFFF8000 | $int;
|
||||||
}
|
}
|
||||||
|
// Convert the value
|
||||||
/**
|
return (int) self::dword2signedInt($int);
|
||||||
* bytes2unsignedInt
|
}
|
||||||
*
|
|
||||||
* The function converts array of 2 or 4 bytes to unsigned integer.
|
/**
|
||||||
* The return value depends on order of the input bytes (endianning).
|
* bytes2unsignedInt
|
||||||
*
|
*
|
||||||
* @param array $values
|
* The function converts array of 2 or 4 bytes to unsigned integer.
|
||||||
* @param bool $endianness
|
* The return value depends on order of the input bytes (endianning).
|
||||||
* @return int|float
|
*
|
||||||
*/
|
* @param array $values
|
||||||
public static function bytes2unsignedInt($values, $endianness = 0) {
|
* @param bool $endianness
|
||||||
$data = array();
|
* @return int|float
|
||||||
$int = 0;
|
*/
|
||||||
// Set the array to correct form
|
public static function bytes2unsignedInt($values, $endianness = 0) {
|
||||||
$data = self::checkData($values);
|
$data = array();
|
||||||
// Combine bytes
|
$int = 0;
|
||||||
$int = self::combineBytes($data, $endianness);
|
// Set the array to correct form
|
||||||
// Convert the value
|
$data = self::checkData($values);
|
||||||
return self::dword2unsignedInt($int);
|
// Combine bytes
|
||||||
}
|
$int = self::combineBytes($data, $endianness);
|
||||||
|
// Convert the value
|
||||||
/**
|
return self::dword2unsignedInt($int);
|
||||||
* bytes2string
|
}
|
||||||
*
|
|
||||||
* The function converts an values array to the string. The function detects
|
/**
|
||||||
* the end of the string by 0x00 character as defined by string standards.
|
* bytes2string
|
||||||
*
|
*
|
||||||
* @param array $values
|
* The function converts an values array to the string. The function detects
|
||||||
* @param bool $endianness
|
* the end of the string by 0x00 character as defined by string standards.
|
||||||
* @return string
|
*
|
||||||
*/
|
* @param array $values
|
||||||
public static function bytes2string($values, $endianness = 0) {
|
* @param bool $endianness
|
||||||
// Prepare string variable
|
* @return string
|
||||||
$str = "";
|
*/
|
||||||
// Parse the received data word array
|
public static function bytes2string($values, $endianness = 0) {
|
||||||
for($i=0;$i<count($values);$i+=2) {
|
// Prepare string variable
|
||||||
if ($endianness) {
|
$str = "";
|
||||||
if($values[$i] != 0)
|
// Parse the received data word array
|
||||||
$str .= chr($values[$i]);
|
for($i=0;$i<count($values);$i+=2) {
|
||||||
else
|
if ($endianness) {
|
||||||
break;
|
if($values[$i] != 0)
|
||||||
if($values[$i+1] != 0)
|
$str .= chr($values[$i]);
|
||||||
$str .= chr($values[$i+1]);
|
else
|
||||||
else
|
break;
|
||||||
break;
|
if($values[$i+1] != 0)
|
||||||
}
|
$str .= chr($values[$i+1]);
|
||||||
else {
|
else
|
||||||
if($values[$i+1] != 0)
|
break;
|
||||||
$str .= chr($values[$i+1]);
|
}
|
||||||
else
|
else {
|
||||||
break;
|
if($values[$i+1] != 0)
|
||||||
if($values[$i] != 0)
|
$str .= chr($values[$i+1]);
|
||||||
$str .= chr($values[$i]);
|
else
|
||||||
else
|
break;
|
||||||
break;
|
if($values[$i] != 0)
|
||||||
}
|
$str .= chr($values[$i]);
|
||||||
}
|
else
|
||||||
// return string
|
break;
|
||||||
return $str;
|
}
|
||||||
}
|
}
|
||||||
|
// return string
|
||||||
/**
|
return $str;
|
||||||
* real2float
|
}
|
||||||
*
|
|
||||||
* This function converts a value in IEC-1131 REAL single precision form to float.
|
/**
|
||||||
*
|
* real2float
|
||||||
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or
|
*
|
||||||
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] or
|
* This function converts a value in IEC-1131 REAL single precision form to float.
|
||||||
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
*
|
||||||
*
|
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or
|
||||||
* @param value value in IEC REAL data type to be converted
|
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] or
|
||||||
* @return float float value
|
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
||||||
*/
|
*
|
||||||
private static function real2float($value) {
|
* @param value value in IEC REAL data type to be converted
|
||||||
// get unsigned long
|
* @return float float value
|
||||||
$ulong = pack("L", $value);
|
*/
|
||||||
// set float
|
private static function real2float($value) {
|
||||||
$float = unpack("f", $ulong);
|
// get unsigned long
|
||||||
|
$ulong = pack("L", $value);
|
||||||
return $float[1];
|
// set float
|
||||||
}
|
$float = unpack("f", $ulong);
|
||||||
|
|
||||||
/**
|
return $float[1];
|
||||||
* dword2signedInt
|
}
|
||||||
*
|
|
||||||
* Switch double word to signed integer
|
/**
|
||||||
*
|
* dword2signedInt
|
||||||
* @param int $value
|
*
|
||||||
* @return int
|
* Switch double word to signed integer
|
||||||
*/
|
*
|
||||||
private static function dword2signedInt($value) {
|
* @param int $value
|
||||||
if ((0x80000000 & $value) != 0) {
|
* @return int
|
||||||
return -(0x7FFFFFFF & ~$value)-1;
|
*/
|
||||||
} else {
|
private static function dword2signedInt($value) {
|
||||||
return (0x7FFFFFFF & $value);
|
if ((0x80000000 & $value) != 0) {
|
||||||
}
|
return -(0x7FFFFFFF & ~$value)-1;
|
||||||
}
|
} else {
|
||||||
|
return (0x7FFFFFFF & $value);
|
||||||
/**
|
}
|
||||||
* dword2signedInt
|
}
|
||||||
*
|
|
||||||
* Switch double word to unsigned integer
|
/**
|
||||||
*
|
* dword2signedInt
|
||||||
* @param int $value
|
*
|
||||||
* @return int|float
|
* Switch double word to unsigned integer
|
||||||
*/
|
*
|
||||||
private static function dword2unsignedInt($value) {
|
* @param int $value
|
||||||
if ((0x80000000 & $value) != 0) {
|
* @return int|float
|
||||||
return ((float) (0x7FFFFFFF & $value)) + 2147483648;
|
*/
|
||||||
} else {
|
private static function dword2unsignedInt($value) {
|
||||||
return (int) (0x7FFFFFFF & $value);
|
if ((0x80000000 & $value) != 0) {
|
||||||
}
|
return ((float) (0x7FFFFFFF & $value)) + 2147483648;
|
||||||
}
|
} else {
|
||||||
|
return (int) (0x7FFFFFFF & $value);
|
||||||
/**
|
}
|
||||||
* checkData
|
}
|
||||||
*
|
|
||||||
* Check if the data variable is array, and check if the values are numeric
|
/**
|
||||||
*
|
* checkData
|
||||||
* @param int $data
|
*
|
||||||
* @return int
|
* Check if the data variable is array, and check if the values are numeric
|
||||||
*/
|
*
|
||||||
private static function checkData($data) {
|
* @param int $data
|
||||||
// Check the data
|
* @return int
|
||||||
if (!is_array($data) ||
|
*/
|
||||||
count($data)<2 ||
|
private static function checkData($data) {
|
||||||
count($data)>4 ||
|
// Check the data
|
||||||
count($data)==3) {
|
if (!is_array($data) ||
|
||||||
throw new Exception('The input data should be an array of 2 or 4 bytes.');
|
count($data)<2 ||
|
||||||
}
|
count($data)>4 ||
|
||||||
// Fill the rest of array by zeroes
|
count($data)==3) {
|
||||||
if (count($data) == 2) {
|
throw new Exception('The input data should be an array of 2 or 4 bytes.');
|
||||||
$data[2] = 0;
|
}
|
||||||
$data[3] = 0;
|
// Fill the rest of array by zeroes
|
||||||
}
|
if (count($data) == 2) {
|
||||||
// Check the values to be number
|
$data[2] = 0;
|
||||||
if (!is_numeric($data[0]) ||
|
$data[3] = 0;
|
||||||
!is_numeric($data[1]) ||
|
}
|
||||||
!is_numeric($data[2]) ||
|
// Check the values to be number
|
||||||
!is_numeric($data[3])) {
|
if (!is_numeric($data[0]) ||
|
||||||
throw new Exception('Data are not numeric or the array keys are not indexed by 0,1,2 and 3');
|
!is_numeric($data[1]) ||
|
||||||
}
|
!is_numeric($data[2]) ||
|
||||||
|
!is_numeric($data[3])) {
|
||||||
return $data;
|
throw new Exception('Data are not numeric or the array keys are not indexed by 0,1,2 and 3');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return $data;
|
||||||
* combineBytes
|
}
|
||||||
*
|
|
||||||
* Combine bytes together
|
/**
|
||||||
*
|
* combineBytes
|
||||||
* @param int $data
|
*
|
||||||
* @param bool $endianness
|
* Combine bytes together
|
||||||
* @return int
|
*
|
||||||
*/
|
* @param int $data
|
||||||
private static function combineBytes($data, $endianness) {
|
* @param bool $endianness
|
||||||
$value = 0;
|
* @return int
|
||||||
// Combine bytes
|
*/
|
||||||
if ($endianness == 0)
|
private static function combineBytes($data, $endianness) {
|
||||||
$value = (($data[3] & 0xFF)<<16) |
|
$value = 0;
|
||||||
(($data[2] & 0xFF)<<24) |
|
// Combine bytes
|
||||||
(($data[1] & 0xFF)) |
|
if ($endianness == 0)
|
||||||
(($data[0] & 0xFF)<<8);
|
$value = (($data[3] & 0xFF)<<16) |
|
||||||
else
|
(($data[2] & 0xFF)<<24) |
|
||||||
$value = (($data[3] & 0xFF)<<24) |
|
(($data[1] & 0xFF)) |
|
||||||
(($data[2] & 0xFF)<<16) |
|
(($data[0] & 0xFF)<<8);
|
||||||
(($data[1] & 0xFF)<<8) |
|
else
|
||||||
(($data[0] & 0xFF));
|
$value = (($data[3] & 0xFF)<<24) |
|
||||||
|
(($data[2] & 0xFF)<<16) |
|
||||||
return $value;
|
(($data[1] & 0xFF)<<8) |
|
||||||
}
|
(($data[0] & 0xFF));
|
||||||
}
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Probably broken and dubious.
|
||||||
|
|
||||||
|
Left from the original repo.
|
||||||
@@ -1,210 +0,0 @@
|
|||||||
<refentry id="{@id}">
|
|
||||||
<refnamediv>
|
|
||||||
<refname>Phpmodbus user's guide</refname>
|
|
||||||
<refpurpose>Phpmodbus How-to</refpurpose>
|
|
||||||
</refnamediv>
|
|
||||||
<refsynopsisdiv>
|
|
||||||
<author>
|
|
||||||
Jan Krakora
|
|
||||||
<authorblurb>
|
|
||||||
{@link mailto:krakora.jan@googlemail.com email}
|
|
||||||
</authorblurb>
|
|
||||||
</author>
|
|
||||||
</refsynopsisdiv>
|
|
||||||
{@toc}
|
|
||||||
<refsect1 id="{@id intro}">
|
|
||||||
<title>Introduction</title>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
The library implements:
|
|
||||||
<itemizedlist>
|
|
||||||
<listitem><para>FC 1: read multiple coils</para></listitem>
|
|
||||||
<listitem><para>FC 2: read input discretes</para></listitem>
|
|
||||||
<listitem><para>FC 3: read multiple registers</para></listitem>
|
|
||||||
<listitem><para>FC 4: read multiple input registers</para></listitem>
|
|
||||||
<listitem><para>FC 5: write single coil</para></listitem>
|
|
||||||
<listitem><para>FC 6: write single register</para></listitem>
|
|
||||||
<listitem><para>FC 15: write multiple coils</para></listitem>
|
|
||||||
<listitem><para>FC 16: write multiple registers</para></listitem>
|
|
||||||
<listitem><para>FC 23: read write registers</para></listitem>
|
|
||||||
</itemizedlist>
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
For more about Modbus protocol see [{@link http://www.modbus.org}] or
|
|
||||||
[{@link http://en.wikipedia.org/wiki/Modbus Wiki}]
|
|
||||||
</para>
|
|
||||||
</refsect1>
|
|
||||||
<refsect1 id="{@id install}">
|
|
||||||
<title>Install</title>
|
|
||||||
<para>
|
|
||||||
At the first, it is supposed an PHP solution has been already installed on
|
|
||||||
your server (LAMP, WAMP, MSS+CGI etc.).
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
Copy the Phpmodbus library to your PHP project folder.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
Create a PHP script and assign the library using require_once() command
|
|
||||||
<programlisting role="c">
|
|
||||||
<![CDATA[require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php'; ]]>
|
|
||||||
</programlisting>
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
To create UDP Modbus master object communicating to an modbus slave
|
|
||||||
e.g. at IP address 192.168.1.1 write
|
|
||||||
<programlisting role="c" linenumbering="numbered" startinglinenumber="2">
|
|
||||||
<![CDATA[ $modbus = new ModbusMaster("192.168.1.1", "UDP"); ]]>
|
|
||||||
</programlisting>
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
To create TCP Modbus master use
|
|
||||||
<programlisting role="c" linenumbering="numbered" startinglinenumber="2">
|
|
||||||
<![CDATA[ $modbus = new ModbusMaster("192.168.1.1", "TCP"); ]]>
|
|
||||||
</programlisting>
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
To read 5 words (10 bytes) from the device ID=0 and its memory address 12288
|
|
||||||
use try-catch method call
|
|
||||||
<programlisting role="c" linenumbering="numbered" startinglinenumber="3">
|
|
||||||
<![CDATA[try {
|
|
||||||
// Function processing
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
|
||||||
// Exception processing, e.g. print error information
|
|
||||||
echo $modbus;
|
|
||||||
echo $e;
|
|
||||||
exit;
|
|
||||||
}]]>
|
|
||||||
</programlisting>
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
To process the function byte stream, use conversion to PHP types in
|
|
||||||
</para>
|
|
||||||
<programlisting role="c" linenumbering="numbered" startinglinenumber="15">
|
|
||||||
<![CDATA[echo PhpType::bytes2string($recData); ]]>
|
|
||||||
</programlisting>
|
|
||||||
<para>
|
|
||||||
For other examples see sections bellow.
|
|
||||||
</para>
|
|
||||||
</refsect1>
|
|
||||||
<refsect1 id="{@id examples}">
|
|
||||||
<title>Examples</title>
|
|
||||||
<para>
|
|
||||||
This section presents library features and examples
|
|
||||||
</para>
|
|
||||||
<refsect2 id="{@id example_fc1}">
|
|
||||||
<title>FC1 - read mutliple coils</title>
|
|
||||||
<para>
|
|
||||||
FC1 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc1.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc2}">
|
|
||||||
<title>FC2 - read input discretes</title>
|
|
||||||
<para>
|
|
||||||
FC2 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc2.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc3}">
|
|
||||||
<title>FC3 - read mutliple registers</title>
|
|
||||||
<para>
|
|
||||||
FC3 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc3.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc4}">
|
|
||||||
<title>FC4 - read multiple input registers </title>
|
|
||||||
<para>
|
|
||||||
FC4 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc4.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc5}">
|
|
||||||
<title>FC5 - write single coil</title>
|
|
||||||
<para>
|
|
||||||
FC5 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc5.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc6}">
|
|
||||||
<title>FC6 - write single register</title>
|
|
||||||
<para>
|
|
||||||
FC6 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc6.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc15}">
|
|
||||||
<title>FC15 - write mutliple coils</title>
|
|
||||||
<para>
|
|
||||||
FC15 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc15.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc16}">
|
|
||||||
<title>FC16 - write mutliple registers</title>
|
|
||||||
<para>
|
|
||||||
FC16 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc16.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id example_fc23}">
|
|
||||||
<title>FC23 - read write registers</title>
|
|
||||||
<para>
|
|
||||||
FC23 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc23.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id wago_example}">
|
|
||||||
<title>Dump of M-memory from WAGO 750-84x series coupler.</title>
|
|
||||||
<para>
|
|
||||||
Dump of M-memory from WAGO 750-84x series coupler.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_750841_Mmemory.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2 id="{@id datatype}">
|
|
||||||
<title>Data conversion to PHP types.</title>
|
|
||||||
<para>
|
|
||||||
Conversion of the data bytes, received from Modbus, to a PHP type.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_datatype.php}
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
</refsect1>
|
|
||||||
<refsect1 id="{@id back_comp}">
|
|
||||||
<title>Back compatibility</title>
|
|
||||||
<para>
|
|
||||||
This version is back compatible to the last versions. Just use
|
|
||||||
<programlisting role="c">
|
|
||||||
<![CDATA[require_once dirname(__FILE__) . '/Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
$modbus = new ModbusMasterUdp("192.168.1.1");
|
|
||||||
// ... ]]>
|
|
||||||
</programlisting>
|
|
||||||
</para>
|
|
||||||
</refsect1>
|
|
||||||
</refentry>
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.2 KiB |
Reference in New Issue
Block a user