Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ac0018444 | ||
|
|
5300b2a867 | ||
|
|
c676c06e2e | ||
|
|
697daedba3 | ||
|
|
cd52263b8e | ||
|
|
a2bde03574 | ||
|
|
24d16bc1aa | ||
|
|
bb0cdd467a | ||
|
|
9ef8e72f3e | ||
|
|
ad507ed46c | ||
|
|
5bb5dfe654 |
@@ -0,0 +1,4 @@
|
|||||||
|
/vendor
|
||||||
|
composer.lock
|
||||||
|
|
||||||
|
*~
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
a88b5a87fb4cd49f16ce72062cce19d55f1fbf98 0.2
|
|
||||||
a7f90c18859d31922bdd8c61299e2790f5e2eb1a 0.3
|
|
||||||
bdf8b3f414c4d08fc0edf9fd1953f55db26e4bac 0.4
|
|
||||||
1e09b0da7d8c62a9a292ee51540feac9de4c1222 0.4.1
|
|
||||||
e1262f028e180c2719564f840ffa1a9f8672b6be 0.5
|
|
||||||
079f8ac1f49faf4e6bbb84d62c39eee1507805f2 0.6
|
|
||||||
@@ -1,3 +1,26 @@
|
|||||||
|
+---------------------------------------------------------+
|
||||||
|
| ACKNOWLEDGEMENTS |
|
||||||
|
+---------------------------------------------------------+
|
||||||
|
|
||||||
|
This project is based on work by Jan Krákora, (c) 2004-2013.
|
||||||
|
The original license is reproduced below.
|
||||||
|
|
||||||
|
https://code.google.com/archive/p/phpmodbus/
|
||||||
|
|
||||||
|
Further improvements - John S. Long and other contributors
|
||||||
|
|
||||||
|
https://github.com/adduc/phpmodbus
|
||||||
|
|
||||||
|
Cleanup and namespaces - Ondřej Hruška, 2016
|
||||||
|
|
||||||
|
https://github.com/MightyPork/phpmodbus
|
||||||
|
|
||||||
|
|
||||||
|
+---------------------------------------------------------+
|
||||||
|
| ORIGINAL LICENSE |
|
||||||
|
+---------------------------------------------------------+
|
||||||
|
|
||||||
|
|
||||||
The Phpmodbus License, Version 1
|
The Phpmodbus License, Version 1
|
||||||
============================
|
============================
|
||||||
|
|
||||||
@@ -60,3 +83,7 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|||||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
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
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
End of file.
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
# phpmodbus
|
||||||
|
|
||||||
|
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/
|
||||||
|
|
||||||
|
> **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
|
||||||
|
require_once dirname(__FILE__) . '/Phpmodbus/ModbusMaster.php';
|
||||||
|
|
||||||
|
// Modbus master UDP
|
||||||
|
$modbus = new ModbusMaster("192.168.1.1", "UDP");
|
||||||
|
// Read multiple registers
|
||||||
|
try {
|
||||||
|
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
||||||
|
}
|
||||||
|
catch (Exception $e) {
|
||||||
|
// Print error information if any
|
||||||
|
echo $modbus;
|
||||||
|
echo $e;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
// Print data in string format
|
||||||
|
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].
|
||||||
|
|
||||||
|
Note:
|
||||||
|
* The PHP extension php_sockets.dll should be enabled (server php.ini file)
|
||||||
+10
-6
@@ -1,7 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "adduc/phpmodbus",
|
"name": "MightyPork/phpmodbus",
|
||||||
"description": "Composer version of PhpModBus",
|
"keywords": ["phpmodbus","modbus"],
|
||||||
|
"description": "PhpModbus with namespaces and updated to PHP 7",
|
||||||
"license": "LGPL",
|
"license": "LGPL",
|
||||||
|
"require": {
|
||||||
|
"php": "^5.3.2 || ^7.0",
|
||||||
|
"ext-sockets": "*"
|
||||||
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Honza Krakora",
|
"name": "Honza Krakora",
|
||||||
@@ -9,9 +14,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"classmap": [ "Phpmodbus" ]
|
"psr-4": {
|
||||||
},
|
"PHPModbus\\": "src/"
|
||||||
"require": {
|
}
|
||||||
"ext-sockets": "*"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
||||||
|
|
||||||
|
// Create Modbus object
|
||||||
|
$modbus = new ModbusMasterUdp("192.192.15.51");
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Read input discretes - FC 4
|
||||||
|
$recData = $modbus->readMultipleInputRegisters(0, 0, 2);
|
||||||
|
}
|
||||||
|
catch (Exception $e) {
|
||||||
|
// Print error information if any
|
||||||
|
echo $modbus;
|
||||||
|
echo $e;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
var_dump($recData);
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
||||||
|
|
||||||
|
// Create Modbus object
|
||||||
|
$modbus = new ModbusMasterUdp("192.192.15.51");
|
||||||
|
|
||||||
|
// Data to be writen - TRUE, FALSE
|
||||||
|
$data_true = array(TRUE);
|
||||||
|
$data_false = array(FALSE);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Write single coil - FC5
|
||||||
|
$modbus->writeSingleCoil(0, 12288, $data_true);
|
||||||
|
$modbus->writeSingleCoil(0, 12289, $data_false);
|
||||||
|
$modbus->writeSingleCoil(0, 12290, $data_true);
|
||||||
|
$modbus->writeSingleCoil(0, 12291, $data_false);
|
||||||
|
}
|
||||||
|
catch (Exception $e) {
|
||||||
|
// Print error information if any
|
||||||
|
echo $modbus;
|
||||||
|
echo $e;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
||||||
*
|
*
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
||||||
*
|
*
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
* with this package in the file license.txt.
|
* with this package in the file license.txt.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* @copyright Copyright (c) 2004, 2013 Jan Krakora
|
||||||
* @license PhpModbus license
|
* @license PhpModbus license
|
||||||
* @category Phpmodbus
|
* @category Phpmodbus
|
||||||
* @tutorial Phpmodbus.pkg
|
* @tutorial Phpmodbus.pkg
|
||||||
@@ -15,9 +18,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/IecType.php';
|
|
||||||
require_once dirname(__FILE__) . '/PhpType.php';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ModbusMaster
|
* ModbusMaster
|
||||||
*
|
*
|
||||||
@@ -27,13 +27,16 @@ require_once dirname(__FILE__) . '/PhpType.php';
|
|||||||
* - FC 1: read coils
|
* - FC 1: read coils
|
||||||
* - FC 2: read input discretes
|
* - FC 2: read input discretes
|
||||||
* - FC 3: read multiple registers
|
* - FC 3: read multiple registers
|
||||||
|
* - FC 4: read multiple input registers
|
||||||
|
* - FC 5: write single coil
|
||||||
* - 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
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
* @copyright Copyright (c) 2004, 2013 Jan Krakora
|
||||||
* @package Phpmodbus
|
* @package Phpmodbus
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -512,6 +515,207 @@ class ModbusMaster {
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readMultipleInputRegisters
|
||||||
|
*
|
||||||
|
* Modbus function FC 4(0x04) - Read Multiple Input Registers.
|
||||||
|
*
|
||||||
|
* This function reads {@link $quantity} of Words (2 bytes) from reference
|
||||||
|
* {@link $referenceRead} of a memory of a Modbus device given by
|
||||||
|
* {@link $unitId}.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param int $unitId usually ID of Modbus device
|
||||||
|
* @param int $reference Reference in the device memory to read data.
|
||||||
|
* @param int $quantity Amounth of the data to be read from device.
|
||||||
|
* @return false|Array Success flag or array of received data.
|
||||||
|
*/
|
||||||
|
function readMultipleInputRegisters($unitId, $reference, $quantity){
|
||||||
|
$this->status .= "readMultipleInputRegisters: START\n";
|
||||||
|
// connect
|
||||||
|
$this->connect();
|
||||||
|
// send FC 4
|
||||||
|
$packet = $this->readMultipleInputRegistersPacketBuilder($unitId, $reference, $quantity);
|
||||||
|
$this->status .= $this->printPacket($packet);
|
||||||
|
$this->send($packet);
|
||||||
|
// receive response
|
||||||
|
$rpacket = $this->rec();
|
||||||
|
$this->status .= $this->printPacket($rpacket);
|
||||||
|
// parse packet
|
||||||
|
$receivedData = $this->readMultipleInputRegistersParser($rpacket);
|
||||||
|
// disconnect
|
||||||
|
$this->disconnect();
|
||||||
|
$this->status .= "readMultipleInputRegisters: DONE\n";
|
||||||
|
// return
|
||||||
|
return $receivedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fc4
|
||||||
|
*
|
||||||
|
* Alias to {@link readMultipleInputRegisters} method.
|
||||||
|
*
|
||||||
|
* @param int $unitId
|
||||||
|
* @param int $reference
|
||||||
|
* @param int $quantity
|
||||||
|
* @return false|Array
|
||||||
|
*/
|
||||||
|
function fc4($unitId, $reference, $quantity){
|
||||||
|
return $this->readMultipleInputRegisters($unitId, $reference, $quantity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readMultipleInputRegistersPacketBuilder
|
||||||
|
*
|
||||||
|
* Packet FC 4 builder - read multiple input registers
|
||||||
|
*
|
||||||
|
* @param int $unitId
|
||||||
|
* @param int $reference
|
||||||
|
* @param int $quantity
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function readMultipleInputRegistersPacketBuilder($unitId, $reference, $quantity){
|
||||||
|
$dataLen = 0;
|
||||||
|
// build data section
|
||||||
|
$buffer1 = "";
|
||||||
|
// build body
|
||||||
|
$buffer2 = "";
|
||||||
|
$buffer2 .= iecType::iecBYTE(4); // FC 4 = 4(0x04)
|
||||||
|
// build body - read section
|
||||||
|
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
|
||||||
|
$buffer2 .= iecType::iecINT($quantity); // quantity
|
||||||
|
$dataLen += 5;
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* readMultipleInputRegistersParser
|
||||||
|
*
|
||||||
|
* FC 4 response parser
|
||||||
|
*
|
||||||
|
* @param string $packet
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function readMultipleInputRegistersParser($packet){
|
||||||
|
$data = array();
|
||||||
|
// check Response code
|
||||||
|
$this->responseCode($packet);
|
||||||
|
// get data
|
||||||
|
for($i=0;$i<ord($packet[8]);$i++){
|
||||||
|
$data[$i] = ord($packet[9+$i]);
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* writeSingleCoil
|
||||||
|
*
|
||||||
|
* Modbus function FC5(0x05) - Write Single Register.
|
||||||
|
*
|
||||||
|
* This function writes {@link $data} single coil at {@link $reference} position of
|
||||||
|
* memory of a Modbus device given by {@link $unitId}.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @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 array $data value to be written (TRUE|FALSE).
|
||||||
|
* @return bool Success flag
|
||||||
|
*/
|
||||||
|
function writeSingleCoil($unitId, $reference, $data){
|
||||||
|
$this->status .= "writeSingleCoil: START\n";
|
||||||
|
// connect
|
||||||
|
$this->connect();
|
||||||
|
// send FC5
|
||||||
|
$packet = $this->writeSingleCoilPacketBuilder($unitId, $reference, $data);
|
||||||
|
$this->status .= $this->printPacket($packet);
|
||||||
|
$this->send($packet);
|
||||||
|
// receive response
|
||||||
|
$rpacket = $this->rec();
|
||||||
|
$this->status .= $this->printPacket($rpacket);
|
||||||
|
// parse packet
|
||||||
|
$this->writeSingleCoilParser($rpacket);
|
||||||
|
// disconnect
|
||||||
|
$this->disconnect();
|
||||||
|
$this->status .= "writeSingleCoil: DONE\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fc5
|
||||||
|
*
|
||||||
|
* Alias to {@link writeSingleCoil} method
|
||||||
|
*
|
||||||
|
* @param int $unitId
|
||||||
|
* @param int $reference
|
||||||
|
* @param array $data
|
||||||
|
* @param array $dataTypes
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function fc5($unitId, $reference, $data, $dataTypes){
|
||||||
|
return $this->writeSingleCoil($unitId, $reference, $data, $dataTypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* writeSingleCoilPacketBuilder
|
||||||
|
*
|
||||||
|
* Packet builder FC5 - WRITE single register
|
||||||
|
*
|
||||||
|
* @param int $unitId
|
||||||
|
* @param int $reference
|
||||||
|
* @param array $data
|
||||||
|
* @param array $dataTypes
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function writeSingleCoilPacketBuilder($unitId, $reference, $data){
|
||||||
|
$dataLen = 0;
|
||||||
|
// build data section
|
||||||
|
$buffer1 = "";
|
||||||
|
foreach($data as $key=>$dataitem) {
|
||||||
|
if($dataitem == TRUE){
|
||||||
|
$buffer1 = iecType::iecINT(0xFF00);
|
||||||
|
} else {
|
||||||
|
$buffer1 = iecType::iecINT(0x0000);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
$dataLen += 2;
|
||||||
|
// build body
|
||||||
|
$buffer2 = "";
|
||||||
|
$buffer2 .= iecType::iecBYTE(5); // FC5 = 5(0x05)
|
||||||
|
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
|
||||||
|
$dataLen += 3;
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* writeSingleCoilParser
|
||||||
|
*
|
||||||
|
* FC5 response parser
|
||||||
|
*
|
||||||
|
* @param string $packet
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function writeSingleCoilParser($packet){
|
||||||
|
$this->responseCode($packet);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* writeSingleRegister
|
* writeSingleRegister
|
||||||
*
|
*
|
||||||
@@ -846,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,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
*
|
||||||
@@ -15,8 +18,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/ModbusMaster.php';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ModbusMasterTcp
|
* ModbusMasterTcp
|
||||||
*
|
*
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
*
|
||||||
@@ -15,8 +18,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/ModbusMaster.php';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ModbusMasterUdp
|
* ModbusMasterUdp
|
||||||
*
|
*
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
*
|
*
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
Test should pass when %IX0.0==FALSE and %IX0.1==TRUE
|
||||||
|
array(4) {
|
||||||
|
[0]=>
|
||||||
|
int(0)
|
||||||
|
[1]=>
|
||||||
|
int(2)
|
||||||
|
[2]=>
|
||||||
|
int(0)
|
||||||
|
[3]=>
|
||||||
|
int(0)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
Array
|
||||||
|
(
|
||||||
|
[0] => 0
|
||||||
|
[1] => 5
|
||||||
|
)
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
||||||
|
require_once dirname(__FILE__) . '/../config.php';
|
||||||
|
|
||||||
|
// Create Modbus object
|
||||||
|
$modbus = new ModbusMasterUdp($test_host_ip);
|
||||||
|
|
||||||
|
// Test requirements
|
||||||
|
echo "Test should pass when %IX0.0==FALSE and %IX0.1==TRUE\n";
|
||||||
|
|
||||||
|
// Read input discretes - FC 4
|
||||||
|
$recData = $modbus->readMultipleInputRegisters(0, 0, 2);
|
||||||
|
|
||||||
|
var_dump($recData);
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
||||||
|
require_once dirname(__FILE__) . '/../config.php';
|
||||||
|
|
||||||
|
// Create Modbus object
|
||||||
|
$modbus = new ModbusMasterUdp($test_host_ip);
|
||||||
|
|
||||||
|
// Data to be writen - TRUE, FALSE
|
||||||
|
$data_true = array(TRUE);
|
||||||
|
$data_false = array(FALSE);
|
||||||
|
|
||||||
|
// Reset target WORD
|
||||||
|
$modbus->writeSingleRegister(0, 12288, array(0), array('WORD'));
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Read data - FC3
|
||||||
|
$recData = $modbus->readMultipleRegisters(0, 12288, 1);
|
||||||
|
print_r($recData);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Probably broken and dubious.
|
||||||
|
|
||||||
|
Left from the original repo.
|
||||||
+2
-2
@@ -1,3 +1,3 @@
|
|||||||
<?php
|
<?php
|
||||||
$test_host_ip = "192.168.1.105";
|
$test_host_ip = "192.192.1.10";
|
||||||
$test_bind_client_ip = "192.168.1.106";
|
$test_bind_client_ip = "192.192.1.100";
|
||||||
@@ -1,190 +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 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_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