Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ec605cf00
|
||
|
|
f96dffe848
|
||
|
|
c6be1e064b
|
||
|
|
be97fbd98c | ||
|
|
fd2ae72b79 | ||
|
|
4256b6cef7 | ||
|
|
61e7a690eb | ||
|
|
7365700b72 | ||
|
|
cff8493205 | ||
|
|
e10bb250d8 | ||
|
|
c18696a718 | ||
|
|
dd5a892fb6 | ||
|
|
7d058ee526 | ||
|
|
0ac0018444 | ||
|
|
5300b2a867 | ||
|
|
c676c06e2e | ||
|
|
697daedba3 | ||
|
|
cd52263b8e | ||
|
|
a2bde03574 | ||
|
|
24d16bc1aa | ||
|
|
bb0cdd467a | ||
|
|
9ef8e72f3e | ||
|
|
ad507ed46c | ||
|
|
5bb5dfe654 | ||
|
|
83d9baa9f6 | ||
|
|
753bff38ec | ||
|
|
5f069276eb | ||
|
|
7f4c4f2af1 |
@@ -0,0 +1,5 @@
|
|||||||
|
/vendor
|
||||||
|
composer.lock
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
*~
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
a88b5a87fb4cd49f16ce72062cce19d55f1fbf98 0.2
|
|
||||||
a7f90c18859d31922bdd8c61299e2790f5e2eb1a 0.3
|
|
||||||
bdf8b3f414c4d08fc0edf9fd1953f55db26e4bac 0.4
|
|
||||||
1e09b0da7d8c62a9a292ee51540feac9de4c1222 0.4.1
|
|
||||||
+31
-2
@@ -1,10 +1,35 @@
|
|||||||
|
+---------------------------------------------------------+
|
||||||
|
| 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
|
||||||
|
|
||||||
|
https://git.ondrovo.com/packages/php-modbus
|
||||||
|
|
||||||
|
|
||||||
|
+---------------------------------------------------------+
|
||||||
|
| ORIGINAL LICENSE |
|
||||||
|
+---------------------------------------------------------+
|
||||||
|
|
||||||
|
|
||||||
The Phpmodbus License, Version 1
|
The Phpmodbus License, Version 1
|
||||||
============================
|
============================
|
||||||
|
|
||||||
Copyright (c) 2004, 2011 Jan Krakora, Wago (http://www.wago.com)
|
Copyright (c) 2004, 2013 Jan Krakora
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
This license is a legal agreement between you and Jan Krakora, Wago (the "Author")
|
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
|
for the use of Phpmodbus (the "Software"). By obtaining, using and/or
|
||||||
copying the Software, you agree that you have read, understood, and will
|
copying the Software, you agree that you have read, understood, and will
|
||||||
comply with the terms and conditions of this license.
|
comply with the terms and conditions of this license.
|
||||||
@@ -60,3 +85,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.
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
|
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
|
||||||
* with this package in the file license.txt.
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
|
|
||||||
* @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.
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
|
|
||||||
* @package Phpmodbus
|
|
||||||
*/
|
|
||||||
class IecType {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* iecBYTE
|
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 BYTE data type
|
|
||||||
*
|
|
||||||
* @param value value from 0 to 255
|
|
||||||
* @return value IEC BYTE data type
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
function iecBYTE($value) {
|
|
||||||
return chr($value & 0xFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* iecINT
|
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 INT data type
|
|
||||||
*
|
|
||||||
* @param value value to be converted
|
|
||||||
* @return value IEC-1131 INT data type
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
function iecINT($value) {
|
|
||||||
return self::iecBYTE(($value >> 8) & 0x00FF) .
|
|
||||||
self::iecBYTE(($value & 0x00FF));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* iecDINT
|
|
||||||
*
|
|
||||||
* Converts a value to IEC-1131 DINT data type
|
|
||||||
*
|
|
||||||
* @param value value to be converted
|
|
||||||
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
|
|
||||||
* @return value IEC-1131 INT data type
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
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.
|
|
||||||
*
|
|
||||||
* @param value value to be converted
|
|
||||||
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
|
|
||||||
* @return value IEC-1131 REAL data type
|
|
||||||
*/
|
|
||||||
function iecREAL($value, $endianness = 0) {
|
|
||||||
// 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.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
|
||||||
*
|
|
||||||
* @param float value to be converted
|
|
||||||
* @return value IEC REAL data type
|
|
||||||
*/
|
|
||||||
private function float2iecReal($value) {
|
|
||||||
// get float binary string
|
|
||||||
$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
|
|
||||||
*
|
|
||||||
* @param int $value
|
|
||||||
* @param bool $endianness
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private function endianness($value, $endianness = 0) {
|
|
||||||
if ($endianness == 0)
|
|
||||||
return
|
|
||||||
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
|
||||||
self::iecBYTE(($value & 0x000000FF)) .
|
|
||||||
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
|
||||||
self::iecBYTE(($value >> 16) & 0x000000FF);
|
|
||||||
else
|
|
||||||
return
|
|
||||||
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
|
||||||
self::iecBYTE(($value >> 16) & 0x000000FF) .
|
|
||||||
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
|
||||||
self::iecBYTE(($value & 0x000000FF));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,798 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
|
||||||
* with this package in the file license.txt.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
* @license PhpModbus license
|
|
||||||
* @category Phpmodbus
|
|
||||||
* @tutorial Phpmodbus.pkg
|
|
||||||
* @package Phpmodbus
|
|
||||||
* @version $id$
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/IecType.php';
|
|
||||||
require_once dirname(__FILE__) . '/PhpType.php';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ModbusMaster
|
|
||||||
*
|
|
||||||
* This class deals with the MODBUS master
|
|
||||||
*
|
|
||||||
* Implemented MODBUS master functions:
|
|
||||||
* - FC 1: read coils
|
|
||||||
* - FC 3: read multiple registers
|
|
||||||
* - FC 15: write multiple coils
|
|
||||||
* - FC 16: write multiple registers
|
|
||||||
* - FC 23: read write registers
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
* @package Phpmodbus
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class ModbusMaster {
|
|
||||||
private $sock;
|
|
||||||
public $host = "192.168.1.1";
|
|
||||||
public $port = "502";
|
|
||||||
public $client = "";
|
|
||||||
public $client_port = "502";
|
|
||||||
public $status;
|
|
||||||
public $timeout_sec = 5; // Timeout 5 sec
|
|
||||||
public $endianess = 0; // Endianess codding (little endian == 0, big endian == 1)
|
|
||||||
public $socket_protocol = "UDP"; // Socket protocol (TCP, UDP)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ModbusMaster
|
|
||||||
*
|
|
||||||
* 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 $protocol Socket protocol (TCP, UDP)
|
|
||||||
*/
|
|
||||||
function ModbusMaster($host, $protocol){
|
|
||||||
$this->socket_protocol = $protocol;
|
|
||||||
$this->host = $host;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* __toString
|
|
||||||
*
|
|
||||||
* Magic method
|
|
||||||
*/
|
|
||||||
function __toString() {
|
|
||||||
return "<pre>" . $this->status . "</pre>";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* connect
|
|
||||||
*
|
|
||||||
* Connect the socket
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function connect(){
|
|
||||||
// Create a protocol specific socket
|
|
||||||
if ($this->socket_protocol == "TCP"){
|
|
||||||
// TCP socket
|
|
||||||
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
|
||||||
} elseif ($this->socket_protocol == "UDP"){
|
|
||||||
// UDP socket
|
|
||||||
$this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
|
||||||
} else {
|
|
||||||
throw new Exception("Unknown socket protocol, should be 'TCP' or 'UDP'");
|
|
||||||
}
|
|
||||||
// Bind the client socket to a specific local port
|
|
||||||
if (strlen($this->client)>0){
|
|
||||||
$result = socket_bind($this->sock, $this->client, $this->client_port);
|
|
||||||
if ($result === false) {
|
|
||||||
throw new Exception("socket_bind() failed.</br>Reason: ($result)".
|
|
||||||
socket_strerror(socket_last_error($this->sock)));
|
|
||||||
} else {
|
|
||||||
$this->status .= "Bound\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Connect the socket
|
|
||||||
$result = @socket_connect($this->sock, $this->host, $this->port);
|
|
||||||
if ($result === false) {
|
|
||||||
throw new Exception("socket_connect() failed.</br>Reason: ($result)".
|
|
||||||
socket_strerror(socket_last_error($this->sock)));
|
|
||||||
} else {
|
|
||||||
$this->status .= "Connected\n";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* disconnect
|
|
||||||
*
|
|
||||||
* Disconnect the socket
|
|
||||||
*/
|
|
||||||
private function disconnect(){
|
|
||||||
socket_close($this->sock);
|
|
||||||
$this->status .= "Disconnected\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* send
|
|
||||||
*
|
|
||||||
* Send the packet via Modbus
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
*/
|
|
||||||
private function send($packet){
|
|
||||||
socket_write($this->sock, $packet, strlen($packet));
|
|
||||||
$this->status .= "Send\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* rec
|
|
||||||
*
|
|
||||||
* Receive data from the socket
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function rec(){
|
|
||||||
socket_set_nonblock($this->sock);
|
|
||||||
$readsocks[] = $this->sock;
|
|
||||||
$writesocks = NULL;
|
|
||||||
$exceptsocks = NULL;
|
|
||||||
$rec = "";
|
|
||||||
$lastAccess = time();
|
|
||||||
while (socket_select($readsocks,
|
|
||||||
$writesocks,
|
|
||||||
$exceptsocks,
|
|
||||||
0,
|
|
||||||
300000) !== FALSE) {
|
|
||||||
$this->status .= "Wait data ... \n";
|
|
||||||
if (in_array($this->sock, $readsocks)) {
|
|
||||||
while (@socket_recv($this->sock, $rec, 2000, 0)) {
|
|
||||||
$this->status .= "Data received\n";
|
|
||||||
return $rec;
|
|
||||||
}
|
|
||||||
$lastAccess = time();
|
|
||||||
} else {
|
|
||||||
if (time()-$lastAccess >= $this->timeout_sec) {
|
|
||||||
throw new Exception( "Watchdog time expired [ " .
|
|
||||||
$this->timeout_sec . " sec]!!! Connection to " .
|
|
||||||
$this->host . " is not established.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$readsocks[] = $this->sock;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* responseCode
|
|
||||||
*
|
|
||||||
* Check the Modbus response code
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function responseCode($packet){
|
|
||||||
if(($packet[7] & 0x80) > 0) {
|
|
||||||
throw new Exception("Modbus response error code:" . ord($packet[8]));
|
|
||||||
} else {
|
|
||||||
$this->status .= "Modbus response error code: NOERROR\n";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readCoils
|
|
||||||
*
|
|
||||||
* Modbus function FC 1(0x01) - Read Coils
|
|
||||||
*
|
|
||||||
* Reads {@link $quantity} of Coils (boolean) from reference
|
|
||||||
* {@link $referenceRead} of a memory of a Modbus device given by
|
|
||||||
* {@link $unitId}.
|
|
||||||
*
|
|
||||||
* @param type $unitId
|
|
||||||
* @param type $reference
|
|
||||||
* @param type $quantity
|
|
||||||
*/
|
|
||||||
function readCoils($unitId, $reference, $quantity){
|
|
||||||
$this->status .= "readCoils: START\n";
|
|
||||||
// connect
|
|
||||||
$this->connect();
|
|
||||||
// send FC 3
|
|
||||||
$packet = $this->readCoilsPacketBuilder($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->readCoilsParser($rpacket, $quantity);
|
|
||||||
// disconnect
|
|
||||||
$this->disconnect();
|
|
||||||
$this->status .= "readCoils: DONE\n";
|
|
||||||
// return
|
|
||||||
return $receivedData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fc1
|
|
||||||
*
|
|
||||||
* Alias to {@link readMultipleCoils} method
|
|
||||||
*
|
|
||||||
* @param type $unitId
|
|
||||||
* @param type $reference
|
|
||||||
* @param type $quantity
|
|
||||||
* @return type
|
|
||||||
*/
|
|
||||||
function fc1($unitId, $reference, $quantity){
|
|
||||||
return $this->readCoils($unitId, $reference, $quantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readCoilsPacketBuilder
|
|
||||||
*
|
|
||||||
* FC1 packet builder - read coils
|
|
||||||
*
|
|
||||||
* @param type $unitId
|
|
||||||
* @param type $reference
|
|
||||||
* @param type $quantity
|
|
||||||
* @return type
|
|
||||||
*/
|
|
||||||
private function readCoilsPacketBuilder($unitId, $reference, $quantity){
|
|
||||||
$dataLen = 0;
|
|
||||||
// build data section
|
|
||||||
$buffer1 = "";
|
|
||||||
// build body
|
|
||||||
$buffer2 = "";
|
|
||||||
$buffer2 .= iecType::iecBYTE(1); // FC 1 = 1(0x01)
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readCoilsParser
|
|
||||||
*
|
|
||||||
* FC 1 response parser
|
|
||||||
*
|
|
||||||
* @param type $packet
|
|
||||||
* @param type $quantity
|
|
||||||
* @return type
|
|
||||||
*/
|
|
||||||
private function readCoilsParser($packet, $quantity){
|
|
||||||
$data = array();
|
|
||||||
// check Response code
|
|
||||||
$this->responseCode($packet);
|
|
||||||
// get data from stream
|
|
||||||
for($i=0;$i<ord($packet[8]);$i++){
|
|
||||||
$data[$i] = ord($packet[9+$i]);
|
|
||||||
}
|
|
||||||
// get bool values to array
|
|
||||||
$data_bolean_array = array();
|
|
||||||
$di = 0;
|
|
||||||
foreach($data as $value){
|
|
||||||
for($i=0;$i<8;$i++){
|
|
||||||
if($di > $quantity) continue;
|
|
||||||
// get boolean value
|
|
||||||
$v = ($value >> $i) & 0x01;
|
|
||||||
// build boolean array
|
|
||||||
if($v == 0){
|
|
||||||
$data_bolean_array[] = FALSE;
|
|
||||||
} else {
|
|
||||||
$data_bolean_array[] = TRUE;
|
|
||||||
}
|
|
||||||
$di++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $data_bolean_array;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readMultipleRegisters
|
|
||||||
*
|
|
||||||
* Modbus function FC 3(0x03) - Read Multiple 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 (e.g. in device WAGO 750-841, memory MW0 starts at address 12288).
|
|
||||||
* @param int $quantity Amounth of the data to be read from device.
|
|
||||||
* @return false|Array Success flag or array of received data.
|
|
||||||
*/
|
|
||||||
function readMultipleRegisters($unitId, $reference, $quantity){
|
|
||||||
$this->status .= "readMultipleRegisters: START\n";
|
|
||||||
// connect
|
|
||||||
$this->connect();
|
|
||||||
// send FC 3
|
|
||||||
$packet = $this->readMultipleRegistersPacketBuilder($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->readMultipleRegistersParser($rpacket);
|
|
||||||
// disconnect
|
|
||||||
$this->disconnect();
|
|
||||||
$this->status .= "readMultipleRegisters: DONE\n";
|
|
||||||
// return
|
|
||||||
return $receivedData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fc3
|
|
||||||
*
|
|
||||||
* Alias to {@link readMultipleRegisters} method.
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $reference
|
|
||||||
* @param int $quantity
|
|
||||||
* @return false|Array
|
|
||||||
*/
|
|
||||||
function fc3($unitId, $reference, $quantity){
|
|
||||||
return $this->readMultipleRegisters($unitId, $reference, $quantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readMultipleRegistersPacketBuilder
|
|
||||||
*
|
|
||||||
* Packet FC 3 builder - read multiple registers
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $reference
|
|
||||||
* @param int $quantity
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){
|
|
||||||
$dataLen = 0;
|
|
||||||
// build data section
|
|
||||||
$buffer1 = "";
|
|
||||||
// build body
|
|
||||||
$buffer2 = "";
|
|
||||||
$buffer2 .= iecType::iecBYTE(3); // FC 3 = 3(0x03)
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readMultipleRegistersParser
|
|
||||||
*
|
|
||||||
* FC 3 response parser
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function readMultipleRegistersParser($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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* writeMultipleCoils
|
|
||||||
*
|
|
||||||
* Modbus function FC15(0x0F) - Write Multiple Coils
|
|
||||||
*
|
|
||||||
* This function writes {@link $data} array at {@link $reference} position of
|
|
||||||
* memory of a Modbus device given by {@link $unitId}.
|
|
||||||
*
|
|
||||||
* @param type $unitId
|
|
||||||
* @param type $reference
|
|
||||||
* @param type $data
|
|
||||||
* @return type
|
|
||||||
*/
|
|
||||||
function writeMultipleCoils($unitId, $reference, $data){
|
|
||||||
$this->status .= "writeMultipleCoils: START\n";
|
|
||||||
// connect
|
|
||||||
$this->connect();
|
|
||||||
// send FC16
|
|
||||||
$packet = $this->writeMultipleCoilsPacketBuilder($unitId, $reference, $data);
|
|
||||||
$this->status .= $this->printPacket($packet);
|
|
||||||
$this->send($packet);
|
|
||||||
// receive response
|
|
||||||
$rpacket = $this->rec();
|
|
||||||
$this->status .= $this->printPacket($rpacket);
|
|
||||||
// parse packet
|
|
||||||
$this->writeMultipleCoilsParser($rpacket);
|
|
||||||
// disconnect
|
|
||||||
$this->disconnect();
|
|
||||||
$this->status .= "writeMultipleCoils: DONE\n";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fc15
|
|
||||||
*
|
|
||||||
* Alias to {@link writeMultipleCoils} method
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $reference
|
|
||||||
* @param array $data
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
function fc15($unitId, $reference, $data){
|
|
||||||
return $this->writeMultipleCoils($unitId, $reference, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* writeMultipleCoilsPacketBuilder
|
|
||||||
*
|
|
||||||
* Packet builder FC15 - Write multiple coils
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $reference
|
|
||||||
* @param array $data
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function writeMultipleCoilsPacketBuilder($unitId, $reference, $data){
|
|
||||||
$dataLen = 0;
|
|
||||||
// build bool stream to the WORD array
|
|
||||||
$data_word_stream = array();
|
|
||||||
$data_word = 0;
|
|
||||||
$shift = 0;
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
if((($i % 8) == 0) && ($i > 0)) {
|
|
||||||
$data_word_stream[] = $data_word;
|
|
||||||
$shift = 0;
|
|
||||||
$data_word = 0;
|
|
||||||
$data_word |= (0x01 && $data[$i]) << $shift;
|
|
||||||
$shift++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$data_word |= (0x01 && $data[$i]) << $shift;
|
|
||||||
$shift++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$data_word_stream[] = $data_word;
|
|
||||||
// show binary stream to status string
|
|
||||||
foreach($data_word_stream as $d){
|
|
||||||
$this->status .= sprintf("byte=b%08b\n", $d);
|
|
||||||
}
|
|
||||||
// build data section
|
|
||||||
$buffer1 = "";
|
|
||||||
foreach($data_word_stream as $key=>$dataitem) {
|
|
||||||
$buffer1 .= iecType::iecBYTE($dataitem); // register values x
|
|
||||||
$dataLen += 1;
|
|
||||||
}
|
|
||||||
// build body
|
|
||||||
$buffer2 = "";
|
|
||||||
$buffer2 .= iecType::iecBYTE(15); // FC 15 = 15(0x0f)
|
|
||||||
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
|
|
||||||
$buffer2 .= iecType::iecINT(count($data)); // bit count
|
|
||||||
$buffer2 .= iecType::iecBYTE((count($data)+7)/8); // byte count
|
|
||||||
$dataLen += 6;
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* writeMultipleCoilsParser
|
|
||||||
*
|
|
||||||
* FC15 response parser
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function writeMultipleCoilsParser($packet){
|
|
||||||
$this->responseCode($packet);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* writeMultipleRegister
|
|
||||||
*
|
|
||||||
* Modbus function FC16(0x10) - Write Multiple Register.
|
|
||||||
*
|
|
||||||
* This function writes {@link $data} array 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 Array of values to be written.
|
|
||||||
* @param array $dataTypes Array of types of values to be written. The array should consists of string "INT", "DINT" and "REAL".
|
|
||||||
* @return bool Success flag
|
|
||||||
*/
|
|
||||||
function writeMultipleRegister($unitId, $reference, $data, $dataTypes){
|
|
||||||
$this->status .= "writeMultipleRegister: START\n";
|
|
||||||
// connect
|
|
||||||
$this->connect();
|
|
||||||
// send FC16
|
|
||||||
$packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes);
|
|
||||||
$this->status .= $this->printPacket($packet);
|
|
||||||
$this->send($packet);
|
|
||||||
// receive response
|
|
||||||
$rpacket = $this->rec();
|
|
||||||
$this->status .= $this->printPacket($rpacket);
|
|
||||||
// parse packet
|
|
||||||
$this->writeMultipleRegisterParser($rpacket);
|
|
||||||
// disconnect
|
|
||||||
$this->disconnect();
|
|
||||||
$this->status .= "writeMultipleRegister: DONE\n";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fc16
|
|
||||||
*
|
|
||||||
* Alias to {@link writeMultipleRegister} method
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $reference
|
|
||||||
* @param array $data
|
|
||||||
* @param array $dataTypes
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
function fc16($unitId, $reference, $data, $dataTypes){
|
|
||||||
return $this->writeMultipleRegister($unitId, $reference, $data, $dataTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* writeMultipleRegisterPacketBuilder
|
|
||||||
*
|
|
||||||
* Packet builder FC16 - WRITE multiple register
|
|
||||||
* e.g.: 4dd90000000d0010300000030603e807d00bb8
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $reference
|
|
||||||
* @param array $data
|
|
||||||
* @param array $dataTypes
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes){
|
|
||||||
$dataLen = 0;
|
|
||||||
// build data section
|
|
||||||
$buffer1 = "";
|
|
||||||
foreach($data as $key=>$dataitem) {
|
|
||||||
if($dataTypes[$key]=="INT"){
|
|
||||||
$buffer1 .= iecType::iecINT($dataitem); // register values x
|
|
||||||
$dataLen += 2;
|
|
||||||
}
|
|
||||||
elseif($dataTypes[$key]=="DINT"){
|
|
||||||
$buffer1 .= iecType::iecDINT($dataitem, $this->endianness); // register values x
|
|
||||||
$dataLen += 4;
|
|
||||||
}
|
|
||||||
elseif($dataTypes[$key]=="REAL") {
|
|
||||||
$buffer1 .= iecType::iecREAL($dataitem, $this->endianness); // register values x
|
|
||||||
$dataLen += 4;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$buffer1 .= iecType::iecINT($dataitem); // register values x
|
|
||||||
$dataLen += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// build body
|
|
||||||
$buffer2 = "";
|
|
||||||
$buffer2 .= iecType::iecBYTE(16); // FC 16 = 16(0x10)
|
|
||||||
$buffer2 .= iecType::iecINT($reference); // refnumber = 12288
|
|
||||||
$buffer2 .= iecType::iecINT($dataLen/2); // word count
|
|
||||||
$buffer2 .= iecType::iecBYTE($dataLen); // byte count
|
|
||||||
$dataLen += 6;
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* writeMultipleRegisterParser
|
|
||||||
*
|
|
||||||
* FC16 response parser
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function writeMultipleRegisterParser($packet){
|
|
||||||
$this->responseCode($packet);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readWriteRegisters
|
|
||||||
*
|
|
||||||
* Modbus function FC23(0x17) - Read Write Registers.
|
|
||||||
*
|
|
||||||
* This function writes {@link $data} array at reference {@link $referenceWrite}
|
|
||||||
* position of memory of a Modbus device given by {@link $unitId}. Simultanously,
|
|
||||||
* it returns {@link $quantity} of Words (2 bytes) from reference {@link $referenceRead}.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param int $unitId usually ID of Modbus device
|
|
||||||
* @param int $referenceRead Reference in the device memory to read data (e.g. in device WAGO 750-841, memory MW0 starts at address 12288).
|
|
||||||
* @param int $quantity Amounth of the data to be read from device.
|
|
||||||
* @param int $referenceWrite Reference in the device memory to write data.
|
|
||||||
* @param array $data Array of values to be written.
|
|
||||||
* @param array $dataTypes Array of types of values to be written. The array should consists of string "INT", "DINT" and "REAL".
|
|
||||||
* @return false|Array Success flag or array of data.
|
|
||||||
*/
|
|
||||||
function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
|
|
||||||
$this->status .= "readWriteRegisters: START\n";
|
|
||||||
// connect
|
|
||||||
$this->connect();
|
|
||||||
// send FC23
|
|
||||||
$packet = $this->readWriteRegistersPacketBuilder($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes);
|
|
||||||
$this->status .= $this->printPacket($packet);
|
|
||||||
$this->send($packet);
|
|
||||||
// receive response
|
|
||||||
$rpacket = $this->rec();
|
|
||||||
$this->status .= $this->printPacket($rpacket);
|
|
||||||
// parse packet
|
|
||||||
$receivedData = $this->readWriteRegistersParser($rpacket);
|
|
||||||
// disconnect
|
|
||||||
$this->disconnect();
|
|
||||||
$this->status .= "writeMultipleRegister: DONE\n";
|
|
||||||
// return
|
|
||||||
return $receivedData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* fc23
|
|
||||||
*
|
|
||||||
* Alias to {@link readWriteRegisters} method.
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $referenceRead
|
|
||||||
* @param int $quantity
|
|
||||||
* @param int $referenceWrite
|
|
||||||
* @param array $data
|
|
||||||
* @param array $dataTypes
|
|
||||||
* @return false|Array
|
|
||||||
*/
|
|
||||||
function fc23($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
|
|
||||||
return $this->readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readWriteRegistersPacketBuilder
|
|
||||||
*
|
|
||||||
* Packet FC23 builder - READ WRITE registers
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param int $unitId
|
|
||||||
* @param int $referenceRead
|
|
||||||
* @param int $quantity
|
|
||||||
* @param int $referenceWrite
|
|
||||||
* @param array $data
|
|
||||||
* @param array $dataTypes
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function readWriteRegistersPacketBuilder($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
|
|
||||||
$dataLen = 0;
|
|
||||||
// build data section
|
|
||||||
$buffer1 = "";
|
|
||||||
foreach($data as $key => $dataitem) {
|
|
||||||
if($dataTypes[$key]=="INT"){
|
|
||||||
$buffer1 .= iecType::iecINT($dataitem); // register values x
|
|
||||||
$dataLen += 2;
|
|
||||||
}
|
|
||||||
elseif($dataTypes[$key]=="DINT"){
|
|
||||||
$buffer1 .= iecType::iecDINT($dataitem, $this->endianness); // register values x
|
|
||||||
$dataLen += 4;
|
|
||||||
}
|
|
||||||
elseif($dataTypes[$key]=="REAL") {
|
|
||||||
$buffer1 .= iecType::iecREAL($dataitem, $this->endianness); // register values x
|
|
||||||
$dataLen += 4;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$buffer1 .= iecType::iecINT($dataitem); // register values x
|
|
||||||
$dataLen += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// build body
|
|
||||||
$buffer2 = "";
|
|
||||||
$buffer2 .= iecType::iecBYTE(23); // FC 23 = 23(0x17)
|
|
||||||
// build body - read section
|
|
||||||
$buffer2 .= iecType::iecINT($referenceRead); // refnumber = 12288
|
|
||||||
$buffer2 .= iecType::iecINT($quantity); // quantity
|
|
||||||
// build body - write section
|
|
||||||
$buffer2 .= iecType::iecINT($referenceWrite); // refnumber = 12288
|
|
||||||
$buffer2 .= iecType::iecINT($dataLen/2); // word count
|
|
||||||
$buffer2 .= iecType::iecBYTE($dataLen); // byte count
|
|
||||||
$dataLen += 10;
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* readWriteRegistersParser
|
|
||||||
*
|
|
||||||
* FC23 response parser
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function readWriteRegistersParser($packet){
|
|
||||||
$data = array();
|
|
||||||
// if not exception
|
|
||||||
if(!$this->responseCode($packet))
|
|
||||||
return false;
|
|
||||||
// get data
|
|
||||||
for($i=0;$i<ord($packet[8]);$i++){
|
|
||||||
$data[$i] = ord($packet[9+$i]);
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* byte2hex
|
|
||||||
*
|
|
||||||
* Parse data and get it to the Hex form
|
|
||||||
*
|
|
||||||
* @param char $value
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function byte2hex($value){
|
|
||||||
$h = dechex(($value >> 4) & 0x0F);
|
|
||||||
$l = dechex($value & 0x0F);
|
|
||||||
return "$h$l";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* printPacket
|
|
||||||
*
|
|
||||||
* Print a packet in the hex form
|
|
||||||
*
|
|
||||||
* @param string $packet
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function printPacket($packet){
|
|
||||||
$str = "";
|
|
||||||
$str .= "Packet: ";
|
|
||||||
for($i=0;$i<strlen($packet);$i++){
|
|
||||||
$str .= $this->byte2hex(ord($packet[$i]));
|
|
||||||
}
|
|
||||||
$str .= "\n";
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
|
||||||
* with this package in the file license.txt.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
* @license PhpModbus license
|
|
||||||
* @category Phpmodbus
|
|
||||||
* @tutorial Phpmodbus.pkg
|
|
||||||
* @package Phpmodbus
|
|
||||||
* @version $id$
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/ModbusMaster.php';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ModbusMasterTcp
|
|
||||||
*
|
|
||||||
* This class deals with the MODBUS master using TCP. Extends ModbusMaster class.
|
|
||||||
*
|
|
||||||
* Implemented MODBUS functions:
|
|
||||||
* - FC 1: read coils
|
|
||||||
* - FC 3: read multiple registers
|
|
||||||
* - FC 15: write multiple coils
|
|
||||||
* - FC 16: write multiple registers
|
|
||||||
* - FC 23: read write registers
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
* @package Phpmodbus
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class ModbusMasterTcp extends ModbusMaster {
|
|
||||||
/**
|
|
||||||
* ModbusMasterTcp
|
|
||||||
*
|
|
||||||
* 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".
|
|
||||||
*/
|
|
||||||
function ModbusMasterTcp($host){
|
|
||||||
$this->host = $host;
|
|
||||||
$this->socket_protocol = "TCP";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
|
||||||
* with this package in the file license.txt.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
* @license PhpModbus license
|
|
||||||
* @category Phpmodbus
|
|
||||||
* @tutorial Phpmodbus.pkg
|
|
||||||
* @package Phpmodbus
|
|
||||||
* @version $id$
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/ModbusMaster.php';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ModbusMasterUdp
|
|
||||||
*
|
|
||||||
* This class deals with the MODBUS master using UDP stack.
|
|
||||||
*
|
|
||||||
* Implemented MODBUS master functions:
|
|
||||||
* - FC 1: read coils
|
|
||||||
* - FC 3: read multiple registers
|
|
||||||
* - FC 15: write multiple coils
|
|
||||||
* - FC 16: write multiple registers
|
|
||||||
* - FC 23: read write registers
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
|
||||||
* @package Phpmodbus
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class ModbusMasterUdp extends ModbusMaster {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ModbusMasterUdp
|
|
||||||
*
|
|
||||||
* 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".
|
|
||||||
*/
|
|
||||||
function ModbusMasterUdp($host){
|
|
||||||
$this->host = $host;
|
|
||||||
$this->socket_protocol = "UDP";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,249 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Phpmodbus Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
|
|
||||||
*
|
|
||||||
* This source file is subject to the "PhpModbus license" that is bundled
|
|
||||||
* with this package in the file license.txt.
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
|
|
||||||
* @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.
|
|
||||||
*
|
|
||||||
* @author Jan Krakora
|
|
||||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
|
|
||||||
* @package Phpmodbus
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class PhpType {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* bytes2float
|
|
||||||
*
|
|
||||||
* The function converts array of 4 bytes to float. The return value
|
|
||||||
* depends on order of the input bytes (endianning).
|
|
||||||
*
|
|
||||||
* @param array $values
|
|
||||||
* @param bool $endianness
|
|
||||||
* @return float
|
|
||||||
*/
|
|
||||||
public static function bytes2float($values, $endianness = 0) {
|
|
||||||
$data = array();
|
|
||||||
$real = 0;
|
|
||||||
|
|
||||||
// Set the array to correct form
|
|
||||||
$data = self::checkData($values);
|
|
||||||
// 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).
|
|
||||||
*
|
|
||||||
* @param array $values
|
|
||||||
* @param bool $endianness
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
public static function bytes2signedInt($values, $endianness = 0) {
|
|
||||||
$data = array();
|
|
||||||
$int = 0;
|
|
||||||
// Set the array to correct form
|
|
||||||
$data = self::checkData($values);
|
|
||||||
// Combine bytes
|
|
||||||
$int = self::combineBytes($data, $endianness);
|
|
||||||
// In the case of signed 2 byte value convert it to 4 byte one
|
|
||||||
if ((count($values) == 2) && ((0x8000 & $int) > 0)) {
|
|
||||||
$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).
|
|
||||||
*
|
|
||||||
* @param array $values
|
|
||||||
* @param bool $endianness
|
|
||||||
* @return int|float
|
|
||||||
*/
|
|
||||||
public static function bytes2unsignedInt($values, $endianness = 0) {
|
|
||||||
$data = array();
|
|
||||||
$int = 0;
|
|
||||||
// Set the array to correct form
|
|
||||||
$data = self::checkData($values);
|
|
||||||
// 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.
|
|
||||||
*
|
|
||||||
* @param array $values
|
|
||||||
* @param bool $endianness
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function bytes2string($values, $endianness = 0) {
|
|
||||||
// Prepare string variable
|
|
||||||
$str = "";
|
|
||||||
// Parse the received data word array
|
|
||||||
for($i=0;$i<count($values);$i+=2) {
|
|
||||||
if ($endianness) {
|
|
||||||
if($values[$i] != 0)
|
|
||||||
$str .= chr($values[$i]);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
if($values[$i+1] != 0)
|
|
||||||
$str .= chr($values[$i+1]);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if($values[$i+1] != 0)
|
|
||||||
$str .= chr($values[$i+1]);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
if($values[$i] != 0)
|
|
||||||
$str .= chr($values[$i]);
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// return string
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* real2float
|
|
||||||
*
|
|
||||||
* This function converts a value in IEC-1131 REAL single precision form to float.
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
|
||||||
*
|
|
||||||
* @param value value in IEC REAL data type to be converted
|
|
||||||
* @return float float value
|
|
||||||
*/
|
|
||||||
private static function real2float($value) {
|
|
||||||
// get unsigned long
|
|
||||||
$ulong = pack("L", $value);
|
|
||||||
// set float
|
|
||||||
$float = unpack("f", $ulong);
|
|
||||||
|
|
||||||
return $float[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* dword2signedInt
|
|
||||||
*
|
|
||||||
* Switch double word to signed integer
|
|
||||||
*
|
|
||||||
* @param int $value
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private static function dword2signedInt($value) {
|
|
||||||
if ((0x80000000 & $value) != 0) {
|
|
||||||
return -(0x7FFFFFFF & ~$value)-1;
|
|
||||||
} else {
|
|
||||||
return (0x7FFFFFFF & $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* dword2signedInt
|
|
||||||
*
|
|
||||||
* Switch double word to unsigned integer
|
|
||||||
*
|
|
||||||
* @param int $value
|
|
||||||
* @return int|float
|
|
||||||
*/
|
|
||||||
private static function dword2unsignedInt($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
|
|
||||||
*
|
|
||||||
* @param int $data
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private static function checkData($data) {
|
|
||||||
// Check the data
|
|
||||||
if (!is_array($data) ||
|
|
||||||
count($data)<2 ||
|
|
||||||
count($data)>4 ||
|
|
||||||
count($data)==3) {
|
|
||||||
throw new Exception('The input data should be an array of 2 or 4 bytes.');
|
|
||||||
}
|
|
||||||
// Fill the rest of array by zeroes
|
|
||||||
if (count($data) == 2) {
|
|
||||||
$data[2] = 0;
|
|
||||||
$data[3] = 0;
|
|
||||||
}
|
|
||||||
// Check the values to be number
|
|
||||||
if (!is_numeric($data[0]) ||
|
|
||||||
!is_numeric($data[1]) ||
|
|
||||||
!is_numeric($data[2]) ||
|
|
||||||
!is_numeric($data[3])) {
|
|
||||||
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
|
|
||||||
*
|
|
||||||
* @param int $data
|
|
||||||
* @param bool $endianness
|
|
||||||
* @return int
|
|
||||||
*/
|
|
||||||
private static function combineBytes($data, $endianness) {
|
|
||||||
$value = 0;
|
|
||||||
// Combine bytes
|
|
||||||
if ($endianness == 0)
|
|
||||||
$value = (($data[3] & 0xFF)<<16) |
|
|
||||||
(($data[2] & 0xFF)<<24) |
|
|
||||||
(($data[1] & 0xFF)) |
|
|
||||||
(($data[0] & 0xFF)<<8);
|
|
||||||
else
|
|
||||||
$value = (($data[3] & 0xFF)<<24) |
|
|
||||||
(($data[2] & 0xFF)<<16) |
|
|
||||||
(($data[1] & 0xFF)<<8) |
|
|
||||||
(($data[0] & 0xFF));
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
# phpmodbus
|
||||||
|
|
||||||
|
Implementation of the basic functionality of the Modbus TCP and UDP based protocol using PHP.
|
||||||
|
|
||||||
|
**NOTE: This is a fork to fix & update the library code.**
|
||||||
|
|
||||||
|
> **What's new**
|
||||||
|
>
|
||||||
|
> This fork adds a namespace and fixes issues encountered when porting to PHP 7
|
||||||
|
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
* The PHP extension php_sockets.dll should be enabled (server php.ini file)
|
||||||
|
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
```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) {
|
||||||
|
// Print error information if any
|
||||||
|
echo $modbus . "\n";
|
||||||
|
echo $e;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
// Print data in string format
|
||||||
|
echo PhpType::bytes2string($recData);
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
## GoogleCode legacy docs & downloads
|
||||||
|
|
||||||
|
This project was originally hosted on Google Code. It is still archived here:
|
||||||
|
|
||||||
|
* [GoogleCode Archived Repo](http://code.google.com/p/phpmodbus)
|
||||||
|
|
||||||
+15
-7
@@ -1,17 +1,25 @@
|
|||||||
{
|
{
|
||||||
"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",
|
||||||
|
"license": "LGPL-2.1-or-later",
|
||||||
|
"require": {
|
||||||
|
"php": "^5.3.2 || ^7.0",
|
||||||
|
"ext-sockets": "*"
|
||||||
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Honza Krakora",
|
"name": "Honza Krakora",
|
||||||
"email": "krakora.jan@googlemail.com"
|
"email": "krakora.jan@googlemail.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Ondřej Hruška",
|
||||||
|
"email": "ondra@ondrovo.com"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"classmap": [ "Phpmodbus" ]
|
"psr-4": {
|
||||||
},
|
"PHPModbus\\": "src/"
|
||||||
"require": {
|
}
|
||||||
"ext-sockets": "*"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,67 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$ip = "192.192.15.51";
|
|
||||||
$modbus = new ModbusMaster($ip, "UDP");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// FC 3
|
|
||||||
$moduleId = 0;
|
|
||||||
$reference = 12288;
|
|
||||||
$mw0address = 12288;
|
|
||||||
$quantity = 6;
|
|
||||||
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity);
|
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
|
||||||
echo $modbus;
|
|
||||||
echo $e;
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
|
|
||||||
<meta name="generator" content="PSPad editor, www.pspad.com">
|
|
||||||
<title>WAGO 750-841 M-memory dump</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Dump of M-memory from WAGO 750-84x series coupler.</h1>
|
|
||||||
<ul>
|
|
||||||
<li>PLC: 750-84x series</li>
|
|
||||||
<li>IP: <?php echo $ip?></li>
|
|
||||||
<li>Modbus module ID: <?php echo $moduleId?></li>
|
|
||||||
<li>Modbus memory reference: <?php echo $reference?></li>
|
|
||||||
<li>Modbus memory quantity: <?php echo $quantity?></li>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<h2>M-memory dump</h2>
|
|
||||||
|
|
||||||
<table border="1px" width="400px">
|
|
||||||
<tr>
|
|
||||||
<td>Modbus address</td>
|
|
||||||
<td>MWx</td>
|
|
||||||
<td>value</td>
|
|
||||||
</tr>
|
|
||||||
<?php
|
|
||||||
for($i=0;$i<count($recData);$i+=2) {
|
|
||||||
?>
|
|
||||||
<tr>
|
|
||||||
<td><?php echo $i+$reference?></td>
|
|
||||||
<td>MW<?php echo ($i + $reference - $mw0address)/2?></td>
|
|
||||||
<td><?php echo ($recData[$i] << 8)+ $recData[$i+1]?></td>
|
|
||||||
</tr>
|
|
||||||
<?php
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
<h2>Modbus class status</h2>
|
|
||||||
<?php
|
|
||||||
echo $modbus;
|
|
||||||
?>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 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
|
|
||||||
echo "<h1>Received Data</h1>\n";
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Conversion
|
|
||||||
echo "<h2>32 bits types</h2>\n";
|
|
||||||
// Chunk the data array to set of 4 bytes
|
|
||||||
$values = array_chunk($recData, 4);
|
|
||||||
|
|
||||||
// Get float from REAL interpretation
|
|
||||||
echo "<h3>REAL to Float</h3>\n";
|
|
||||||
foreach($values as $bytes)
|
|
||||||
echo PhpType::bytes2float($bytes) . "</br>";
|
|
||||||
|
|
||||||
// Get integer from DINT interpretation
|
|
||||||
echo "<h3>DINT to integer </h3>\n";
|
|
||||||
foreach($values as $bytes)
|
|
||||||
echo PhpType::bytes2signedInt($bytes) . "</br>";
|
|
||||||
|
|
||||||
// Get integer of float from DINT interpretation
|
|
||||||
echo "<h3>DWORD to integer (or float) </h3>\n";
|
|
||||||
foreach($values as $bytes)
|
|
||||||
echo PhpType::bytes2unsignedInt($bytes) . "</br>";
|
|
||||||
|
|
||||||
echo "<h2>16 bit types</h2>\n";
|
|
||||||
// Chunk the data array to set of 4 bytes
|
|
||||||
$values = array_chunk($recData, 2);
|
|
||||||
|
|
||||||
// Get signed integer from INT interpretation
|
|
||||||
echo "<h3>INT to integer </h3>\n";
|
|
||||||
foreach($values as $bytes)
|
|
||||||
echo PhpType::bytes2signedInt($bytes) . "</br>";
|
|
||||||
|
|
||||||
// Get unsigned integer from WORD interpretation
|
|
||||||
echo "<h3>WORD to integer </h3>\n";
|
|
||||||
foreach($values as $bytes)
|
|
||||||
echo PhpType::bytes2unsignedInt($bytes) . "</br>";
|
|
||||||
|
|
||||||
// Get string from STRING interpretation
|
|
||||||
echo "<h3>STRING to string </h3>\n";
|
|
||||||
echo PhpType::bytes2string($recData) . "</br>";
|
|
||||||
?>
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// FC 1
|
|
||||||
$recData = $modbus->readCoils(0, 12288, 12);
|
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
|
||||||
// Print error information if any
|
|
||||||
echo $modbus;
|
|
||||||
echo $e;
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "</br>Status:</br>" . $modbus;
|
|
||||||
|
|
||||||
// Print read data
|
|
||||||
echo "</br>Data:</br>";
|
|
||||||
var_dump($recData);
|
|
||||||
echo "</br>";
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE,
|
|
||||||
TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE,
|
|
||||||
FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE,
|
|
||||||
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// FC15
|
|
||||||
$modbus->writeMultipleCoils(0, 12288, $data);
|
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
|
||||||
// Print error information if any
|
|
||||||
echo $modbus;
|
|
||||||
echo $e;
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo $modbus;
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(10, -1000, 2000, 3.0);
|
|
||||||
$dataTypes = array("WORD", "INT", "DINT", "REAL");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(10, -1000, 2000, 3.0);
|
|
||||||
$dataTypes = array("WORD", "INT", "DINT", "REAL");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 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
|
|
||||||
echo "</br>Status:</br>" . $modbus;
|
|
||||||
|
|
||||||
// Print read data
|
|
||||||
echo "</br>Data:</br>";
|
|
||||||
print_r($recData);
|
|
||||||
echo "</br>";
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster("192.192.15.51", "UDP");
|
|
||||||
|
|
||||||
try {
|
|
||||||
// FC 3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 6);
|
|
||||||
}
|
|
||||||
catch (Exception $e) {
|
|
||||||
// Print error information if any
|
|
||||||
echo $modbus;
|
|
||||||
echo $e;
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "</br>Status:</br>" . $modbus;
|
|
||||||
|
|
||||||
// Print read data
|
|
||||||
echo "</br>Data:</br>";
|
|
||||||
print_r($recData);
|
|
||||||
echo "</br>";
|
|
||||||
?>
|
|
||||||
+141
@@ -0,0 +1,141 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
|
||||||
|
*
|
||||||
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
|
* with this package in the file license.txt.
|
||||||
|
*
|
||||||
|
* @author Jan Krakora
|
||||||
|
* @copyright Copyright (c) 2004, 2013 Jan Krakora
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* @author Jan Krakora
|
||||||
|
* @copyright Copyright (c) 2004, 2010 Jan Krakora
|
||||||
|
* @package Phpmodbus
|
||||||
|
*/
|
||||||
|
class IecType
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* iecBYTE
|
||||||
|
*
|
||||||
|
* Converts a value to IEC-1131 BYTE data type
|
||||||
|
*
|
||||||
|
* @param int $value from 0 to 255
|
||||||
|
* @return int IEC BYTE data type
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function iecBYTE($value)
|
||||||
|
{
|
||||||
|
return chr($value & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* iecINT
|
||||||
|
*
|
||||||
|
* Converts a value to IEC-1131 INT data type
|
||||||
|
*
|
||||||
|
* @param int $value to be converted
|
||||||
|
* @return int IEC-1131 INT data type
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
*
|
||||||
|
* @param int $value to be converted
|
||||||
|
* @param int $bigEndian defines endian codding (little endian == 0, big endian == 1)
|
||||||
|
* @return int IEC-1131 INT data type
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function iecDINT($value, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
// result with right endianness
|
||||||
|
return self::endianness($value, $bigEndian);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* iecREAL
|
||||||
|
*
|
||||||
|
* Converts a value to IEC-1131 REAL data type. The function uses function @use float2iecReal.
|
||||||
|
*
|
||||||
|
* @param int $value to be converted
|
||||||
|
* @param bool $bigEndian defines endian codding (little endian == 0, big endian == 1)
|
||||||
|
* @return int IEC-1131 REAL data type
|
||||||
|
*/
|
||||||
|
public static function iecREAL($value, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
// iecREAL representation
|
||||||
|
$real = self::float2iecReal($value);
|
||||||
|
// result with right endianness
|
||||||
|
return self::endianness($real, $bigEndian);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* float2iecReal
|
||||||
|
*
|
||||||
|
* This function converts float value to IEC-1131 REAL single precision form.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
||||||
|
*
|
||||||
|
* @param float $value to be converted
|
||||||
|
* @return int IEC REAL data type
|
||||||
|
*/
|
||||||
|
private static function float2iecReal($value)
|
||||||
|
{
|
||||||
|
// get float binary string
|
||||||
|
$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
|
||||||
|
*
|
||||||
|
* @param int $value
|
||||||
|
* @param bool $bigEndian
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private static function endianness($value, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
if ($bigEndian == 0) {
|
||||||
|
return
|
||||||
|
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
||||||
|
self::iecBYTE(($value & 0x000000FF)) .
|
||||||
|
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
||||||
|
self::iecBYTE(($value >> 16) & 0x000000FF);
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
self::iecBYTE(($value >> 24) & 0x000000FF) .
|
||||||
|
self::iecBYTE(($value >> 16) & 0x000000FF) .
|
||||||
|
self::iecBYTE(($value >> 8) & 0x000000FF) .
|
||||||
|
self::iecBYTE(($value & 0x000000FF));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
*
|
||||||
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
|
* with this package in the file license.txt.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
* @license PhpModbus license
|
||||||
|
* @category Phpmodbus
|
||||||
|
* @tutorial Phpmodbus.pkg
|
||||||
|
* @package Phpmodbus
|
||||||
|
* @version $id$
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ModbusMasterTcp
|
||||||
|
*
|
||||||
|
* This class deals with the MODBUS master using TCP. Extends ModbusMaster class.
|
||||||
|
*
|
||||||
|
* Implemented MODBUS functions:
|
||||||
|
* - FC 1: read coils
|
||||||
|
* - FC 3: read multiple registers
|
||||||
|
* - FC 15: write multiple coils
|
||||||
|
* - FC 16: write multiple registers
|
||||||
|
* - FC 23: read write registers
|
||||||
|
*
|
||||||
|
* @author Jan Krakora
|
||||||
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
* @package Phpmodbus
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ModbusMasterTcp extends ModbusMaster
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ModbusMasterTcp
|
||||||
|
*
|
||||||
|
* 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".
|
||||||
|
*/
|
||||||
|
public function __construct($host)
|
||||||
|
{
|
||||||
|
parent::__construct($host, "TCP");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
*
|
||||||
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
|
* with this package in the file license.txt.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
* @license PhpModbus license
|
||||||
|
* @category Phpmodbus
|
||||||
|
* @tutorial Phpmodbus.pkg
|
||||||
|
* @package Phpmodbus
|
||||||
|
* @version $id$
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ModbusMasterUdp
|
||||||
|
*
|
||||||
|
* This class deals with the MODBUS master using UDP stack.
|
||||||
|
*
|
||||||
|
* Implemented MODBUS master functions:
|
||||||
|
* - FC 1: read coils
|
||||||
|
* - FC 3: read multiple registers
|
||||||
|
* - FC 15: write multiple coils
|
||||||
|
* - FC 16: write multiple registers
|
||||||
|
* - FC 23: read write registers
|
||||||
|
*
|
||||||
|
* @author Jan Krakora
|
||||||
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
* @package Phpmodbus
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class ModbusMasterUdp extends ModbusMaster
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ModbusMasterUdp
|
||||||
|
*
|
||||||
|
* 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".
|
||||||
|
*/
|
||||||
|
public function __construct($host)
|
||||||
|
{
|
||||||
|
parent::__construct($host, "UDP");
|
||||||
|
}
|
||||||
|
}
|
||||||
+266
@@ -0,0 +1,266 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PHPModbus;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
*
|
||||||
|
* This source file is subject to the "PhpModbus license" that is bundled
|
||||||
|
* with this package in the file license.txt.
|
||||||
|
*
|
||||||
|
* @author Jan Krakora
|
||||||
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* @author Jan Krakora
|
||||||
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||||
|
* @package Phpmodbus
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class PhpType
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* bytes2float
|
||||||
|
*
|
||||||
|
* The function converts array of 4 bytes to float. The return value
|
||||||
|
* depends on order of the input bytes (endianning).
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* @param bool $bigEndian
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
public static function bytes2float($values, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
// Set the array to correct form
|
||||||
|
$data = self::checkData($values);
|
||||||
|
// Combine bytes
|
||||||
|
$real = self::combineBytes($data, $bigEndian);
|
||||||
|
// 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).
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* @param bool $bigEndian
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public static function bytes2signedInt($values, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
$data = array();
|
||||||
|
$int = 0;
|
||||||
|
// Set the array to correct form
|
||||||
|
$data = self::checkData($values);
|
||||||
|
// Combine bytes
|
||||||
|
$int = self::combineBytes($data, $bigEndian);
|
||||||
|
// In the case of signed 2 byte value convert it to 4 byte one
|
||||||
|
if ((count($values) == 2) && ((0x8000 & $int) > 0)) {
|
||||||
|
$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).
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* @param bool $bigEndian
|
||||||
|
* @return int|float
|
||||||
|
*/
|
||||||
|
public static function bytes2unsignedInt($values, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
$data = array();
|
||||||
|
$int = 0;
|
||||||
|
// Set the array to correct form
|
||||||
|
$data = self::checkData($values);
|
||||||
|
// Combine bytes
|
||||||
|
$int = self::combineBytes($data, $bigEndian);
|
||||||
|
// 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.
|
||||||
|
*
|
||||||
|
* @param array $values
|
||||||
|
* @param bool $bigEndian
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function bytes2string($values, $bigEndian = 0)
|
||||||
|
{
|
||||||
|
// Prepare string variable
|
||||||
|
$str = "";
|
||||||
|
// Parse the received data word array
|
||||||
|
for ($i = 0; $i < count($values); $i += 2) {
|
||||||
|
if ($bigEndian) {
|
||||||
|
if ($values[$i] != 0) {
|
||||||
|
$str .= chr($values[$i]);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ($values[$i + 1] != 0) {
|
||||||
|
$str .= chr($values[$i + 1]);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($values[$i + 1] != 0) {
|
||||||
|
$str .= chr($values[$i + 1]);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ($values[$i] != 0) {
|
||||||
|
$str .= chr($values[$i]);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// return string
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* real2float
|
||||||
|
*
|
||||||
|
* This function converts a value in IEC-1131 REAL single precision form to float.
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
||||||
|
*
|
||||||
|
* @param int $value in IEC REAL data type to be converted
|
||||||
|
* @return float float value
|
||||||
|
*/
|
||||||
|
private static function real2float($value)
|
||||||
|
{
|
||||||
|
// get unsigned long
|
||||||
|
$ulong = pack("L", $value);
|
||||||
|
// set float
|
||||||
|
$float = unpack("f", $ulong);
|
||||||
|
|
||||||
|
return $float[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dword2signedInt
|
||||||
|
*
|
||||||
|
* Switch double word to signed integer
|
||||||
|
*
|
||||||
|
* @param int $value
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private static function dword2signedInt($value)
|
||||||
|
{
|
||||||
|
if ((0x80000000 & $value) != 0) {
|
||||||
|
return -(0x7FFFFFFF & ~$value) - 1;
|
||||||
|
} else {
|
||||||
|
return (0x7FFFFFFF & $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dword2signedInt
|
||||||
|
*
|
||||||
|
* Switch double word to unsigned integer
|
||||||
|
*
|
||||||
|
* @param int $value
|
||||||
|
* @return int|float
|
||||||
|
*/
|
||||||
|
private static function dword2unsignedInt($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
|
||||||
|
*
|
||||||
|
* @param int[] $data
|
||||||
|
* @return int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static function checkData($data)
|
||||||
|
{
|
||||||
|
// Check the data
|
||||||
|
if (!is_array($data) ||
|
||||||
|
count($data) < 2 ||
|
||||||
|
count($data) > 4 ||
|
||||||
|
count($data) == 3
|
||||||
|
) {
|
||||||
|
throw new Exception('The input data should be an array of 2 or 4 bytes.');
|
||||||
|
}
|
||||||
|
// Fill the rest of array by zeroes
|
||||||
|
if (count($data) == 2) {
|
||||||
|
$data[2] = 0;
|
||||||
|
$data[3] = 0;
|
||||||
|
}
|
||||||
|
// Check the values to be number
|
||||||
|
if (!is_numeric($data[0]) ||
|
||||||
|
!is_numeric($data[1]) ||
|
||||||
|
!is_numeric($data[2]) ||
|
||||||
|
!is_numeric($data[3])
|
||||||
|
) {
|
||||||
|
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
|
||||||
|
*
|
||||||
|
* @param int $data
|
||||||
|
* @param bool $bigEndian
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
private static function combineBytes($data, $bigEndian)
|
||||||
|
{
|
||||||
|
$value = 0;
|
||||||
|
// Combine bytes
|
||||||
|
if ($bigEndian == 0) {
|
||||||
|
$value = (($data[3] & 0xFF) << 16) |
|
||||||
|
(($data[2] & 0xFF) << 24) |
|
||||||
|
(($data[1] & 0xFF)) |
|
||||||
|
(($data[0] & 0xFF) << 8);
|
||||||
|
} else {
|
||||||
|
$value = (($data[3] & 0xFF) << 24) |
|
||||||
|
(($data[2] & 0xFF) << 16) |
|
||||||
|
(($data[1] & 0xFF) << 8) |
|
||||||
|
(($data[0] & 0xFF));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
@echo off
|
|
||||||
call ../config.bat
|
|
||||||
|
|
||||||
for %%f in (test.*.php) do %php% -q "%%f" > "output/%%f.html"
|
|
||||||
|
|
||||||
cd output
|
|
||||||
for %%f in (*.html) do %diff% "%%f" ../ref/"%%f"
|
|
||||||
cd ..
|
|
||||||
pause
|
|
||||||
|
|
||||||
@echo on
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
125<br>98<br>0<br>0<br>0<br>0<br>0<br>0<br>0<br>0<br>0<br>0<br>255<br>255<br>255<br>255<br>158<br>88<br>97<br>168<br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
Endianing off <hr>0 --> Packet: 0000_0000_</br>1 --> Packet: 0001_0000_</br>-1 --> Packet: ffff_ffff_</br>2147483647 --> Packet: ffff_7fff_</br>-2147483648 --> Packet: 0000_8000_</br>Endianing on <hr>0 --> Packet: 0000_0000_</br>1 --> Packet: 0000_0001_</br>-1 --> Packet: ffff_ffff_</br>2147483647 --> Packet: 7fff_ffff_</br>-2147483648 --> Packet: 8000_0000_</br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
Endianing off <hr>0 --> Packet: 0000_</br>1 --> Packet: 0001_</br>-1 --> Packet: ffff_</br>32767 --> Packet: 7fff_</br>-32768 --> Packet: 8000_</br>Endianing on <hr>0 --> Packet: 0000_</br>1 --> Packet: 0001_</br>-1 --> Packet: ffff_</br>32767 --> Packet: 7fff_</br>-32768 --> Packet: 8000_</br>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
Endianing off <hr>
|
|
||||||
0 --> Packet: 0000_0000_<br>
|
|
||||||
1 --> Packet: 0000_3f80_<br>
|
|
||||||
-2 --> Packet: 0000_c000_<br>
|
|
||||||
0.333333333333 --> Packet: aaaa_3eaa_<br>
|
|
||||||
25 --> Packet: 0000_41c8_<br>
|
|
||||||
Endianing on <hr>
|
|
||||||
0 --> Packet: 0000_0000_<br>
|
|
||||||
1 --> Packet: 3f80_0000_<br>
|
|
||||||
-2 --> Packet: c000_0000_<br>
|
|
||||||
0.333333333333 --> Packet: 3eaa_aaaa_<br>
|
|
||||||
25 --> Packet: 41c8_0000_<br>
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting Mixed values
|
|
||||||
$data = Array (
|
|
||||||
"0" => 125, // 32098 (DINT)
|
|
||||||
"1" => 98,
|
|
||||||
"2" => 0,
|
|
||||||
"3" => 0,
|
|
||||||
"4" => 0, // 0 (DINT)
|
|
||||||
"5" => 0,
|
|
||||||
"6" => 0,
|
|
||||||
"7" => 0,
|
|
||||||
"8" => 0, // 0 (DINT)
|
|
||||||
"9" => 0,
|
|
||||||
"10" => 0,
|
|
||||||
"11" => 0,
|
|
||||||
"12" => 255, // -1 (DINT)
|
|
||||||
"13" => 255,
|
|
||||||
"14" => 255,
|
|
||||||
"15" => 255,
|
|
||||||
"16" => 158, // -25000 (INT)
|
|
||||||
"17" => 88,
|
|
||||||
"18" => 97, // 25000 (INT)
|
|
||||||
"19" => 168
|
|
||||||
);
|
|
||||||
|
|
||||||
// Print mixed values
|
|
||||||
foreach($data as $d)
|
|
||||||
echo ord(IecType::iecBYTE($d)) . "<br>";
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting Mixed values
|
|
||||||
$data = Array (
|
|
||||||
"0" => 0,
|
|
||||||
"1" => 1,
|
|
||||||
"2" => -1,
|
|
||||||
"3" => pow(2,31)-1,
|
|
||||||
"4" => -pow(2,31)
|
|
||||||
);
|
|
||||||
|
|
||||||
function byte2hex($value){
|
|
||||||
$h = dechex(($value >> 4) & 0x0F);
|
|
||||||
$l = dechex($value & 0x0F);
|
|
||||||
return "$h$l";
|
|
||||||
}
|
|
||||||
|
|
||||||
function printPacket($packet){
|
|
||||||
$str = "";
|
|
||||||
$str .= "Packet: ";
|
|
||||||
for($i=0;$i<strlen($packet);$i++){
|
|
||||||
$str .= byte2hex(ord($packet[$i]));
|
|
||||||
if($i % 2)
|
|
||||||
$str .= "_";
|
|
||||||
}
|
|
||||||
$str .= "</br>";
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Endianing off <hr>";
|
|
||||||
// Print mixed values
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
echo $data[$i] . " --> ";
|
|
||||||
$v = IecType::iecDINT($data[$i], 0);
|
|
||||||
echo printPacket($v);
|
|
||||||
"<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Endianing on <hr>";
|
|
||||||
// Print mixed values
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
echo $data[$i] . " --> ";
|
|
||||||
$v = IecType::iecDINT($data[$i], 1);
|
|
||||||
echo printPacket($v);
|
|
||||||
"<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting Mixed values
|
|
||||||
$data = Array (
|
|
||||||
"0" => 0,
|
|
||||||
"1" => 1,
|
|
||||||
"2" => -1,
|
|
||||||
"3" => pow(2,15)-1,
|
|
||||||
"4" => -pow(2,15)
|
|
||||||
);
|
|
||||||
|
|
||||||
function byte2hex($value){
|
|
||||||
$h = dechex(($value >> 4) & 0x0F);
|
|
||||||
$l = dechex($value & 0x0F);
|
|
||||||
return "$h$l";
|
|
||||||
}
|
|
||||||
|
|
||||||
function printPacket($packet){
|
|
||||||
$str = "";
|
|
||||||
$str .= "Packet: ";
|
|
||||||
for($i=0;$i<strlen($packet);$i++){
|
|
||||||
$str .= byte2hex(ord($packet[$i]));
|
|
||||||
if($i % 2)
|
|
||||||
$str .= "_";
|
|
||||||
}
|
|
||||||
$str .= "</br>";
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Endianing off <hr>";
|
|
||||||
// Print mixed values
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
echo $data[$i] . " --> ";
|
|
||||||
$v = IecType::iecINT($data[$i], 0);
|
|
||||||
echo printPacket($v);
|
|
||||||
"<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Endianing on <hr>";
|
|
||||||
// Print mixed values
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
echo $data[$i] . " --> ";
|
|
||||||
$v = IecType::iecINT($data[$i], 1);
|
|
||||||
echo printPacket($v);
|
|
||||||
"<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// http://en.wikipedia.org/wiki/Single_precision
|
|
||||||
$data = Array (
|
|
||||||
"0" => 0, // -> 0000 0000
|
|
||||||
"1" => 1, // -> 3f80 0000
|
|
||||||
"2" => -2, // -> c000 0000
|
|
||||||
"3" => 1/3, // -> 3eaa aaab
|
|
||||||
"4" => 25 // -> 41c8 0000
|
|
||||||
);
|
|
||||||
|
|
||||||
function byte2hex($value){
|
|
||||||
$h = dechex(($value >> 4) & 0x0F);
|
|
||||||
$l = dechex($value & 0x0F);
|
|
||||||
return "$h$l";
|
|
||||||
}
|
|
||||||
|
|
||||||
function printPacket($packet){
|
|
||||||
$str = "";
|
|
||||||
$str .= "Packet: ";
|
|
||||||
for($i=0;$i<strlen($packet);$i++){
|
|
||||||
$str .= byte2hex(ord($packet[$i]));
|
|
||||||
if($i % 2)
|
|
||||||
$str .= "_";
|
|
||||||
}
|
|
||||||
$str .= "<br>\n";
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Endianing off <hr>\n";
|
|
||||||
// Print mixed values
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
echo $data[$i] . " --> ";
|
|
||||||
$v = IecType::iecREAL($data[$i], 0);
|
|
||||||
echo printPacket($v);
|
|
||||||
"<br>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Endianing on <hr>\n";
|
|
||||||
// Print mixed values
|
|
||||||
for($i=0;$i<count($data);$i++) {
|
|
||||||
echo $data[$i] . " --> ";
|
|
||||||
$v = IecType::iecREAL($data[$i], 1);
|
|
||||||
echo printPacket($v);
|
|
||||||
"<br>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
@echo off
|
|
||||||
call ../config.bat
|
|
||||||
|
|
||||||
for %%f in (test.*.php) do %php% -q "%%f" > "output/%%f.html"
|
|
||||||
|
|
||||||
cd output
|
|
||||||
for %%f in (*.html) do %diff% "%%f" ../ref/"%%f"
|
|
||||||
cd ..
|
|
||||||
pause
|
|
||||||
|
|
||||||
@echo on
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 255
|
|
||||||
[8] => 0
|
|
||||||
[9] => 255
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 255
|
|
||||||
[5] => 255
|
|
||||||
[6] => 127
|
|
||||||
[7] => 255
|
|
||||||
[8] => 128
|
|
||||||
[9] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 0
|
|
||||||
[8] => 255
|
|
||||||
[9] => 255
|
|
||||||
[10] => 255
|
|
||||||
[11] => 255
|
|
||||||
[12] => 255
|
|
||||||
[13] => 255
|
|
||||||
[14] => 127
|
|
||||||
[15] => 255
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 128
|
|
||||||
[19] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 0
|
|
||||||
[6] => 63
|
|
||||||
[7] => 128
|
|
||||||
[8] => 0
|
|
||||||
[9] => 0
|
|
||||||
[10] => 192
|
|
||||||
[11] => 0
|
|
||||||
[12] => 170
|
|
||||||
[13] => 171
|
|
||||||
[14] => 62
|
|
||||||
[15] => 170
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 65
|
|
||||||
[19] => 200
|
|
||||||
)
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
writeMultipleRegister (FC26): DONE
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
Fatal error: Uncaught exception 'Exception' with message 'Unknown socket protocol, should be 'TCP' or 'UDP'' in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMaster.php:87
|
|
||||||
Stack trace:
|
|
||||||
#0 D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMaster.php(654): ModbusMaster->connect()
|
|
||||||
#1 D:\Projects\20081010_phpmodbus\src\trunk\tests\ModbusMaster\test.tcp.socket_protocol_mismatch.php(13): ModbusMaster->readWriteRegisters(0, 12288, 6, 12288, Array, Array)
|
|
||||||
#2 {main}
|
|
||||||
thrown in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMaster.php on line 87
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 255
|
|
||||||
[8] => 0
|
|
||||||
[9] => 255
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 255
|
|
||||||
[5] => 255
|
|
||||||
[6] => 127
|
|
||||||
[7] => 255
|
|
||||||
[8] => 128
|
|
||||||
[9] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 0
|
|
||||||
[8] => 255
|
|
||||||
[9] => 255
|
|
||||||
[10] => 255
|
|
||||||
[11] => 255
|
|
||||||
[12] => 255
|
|
||||||
[13] => 255
|
|
||||||
[14] => 127
|
|
||||||
[15] => 255
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 128
|
|
||||||
[19] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 0
|
|
||||||
[6] => 63
|
|
||||||
[7] => 128
|
|
||||||
[8] => 0
|
|
||||||
[9] => 0
|
|
||||||
[10] => 192
|
|
||||||
[11] => 0
|
|
||||||
[12] => 170
|
|
||||||
[13] => 171
|
|
||||||
[14] => 62
|
|
||||||
[15] => 170
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 65
|
|
||||||
[19] => 200
|
|
||||||
)
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
writeMultipleRegister (FC26): DONE
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMaster.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster($test_host_ip, "TCP");
|
|
||||||
|
|
||||||
// Data to be writen - BYTE
|
|
||||||
$data = array(0, 1, 1, pow(2,8)-1, pow(2,8)-1);
|
|
||||||
$dataTypes = array("BYTE", "BYTE", "BYTE", "BYTE", "BYTE");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - INT
|
|
||||||
$data = array(0, 1, -1, pow(2,15)-1, -pow(2,15));
|
|
||||||
$dataTypes = array("INT", "INT", "INT", "INT", "INT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - DINT
|
|
||||||
$data = array(0, 1, -1, pow(2,31)-1, -pow(2,31));
|
|
||||||
$dataTypes = array("DINT", "DINT", "DINT", "DINT", "DINT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - REAL
|
|
||||||
$data = array(0, 1, -2, 1/3, 25);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL", "REAL");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMaster.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster($test_host_ip, "TCP");
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(1000, 2000, 1.250, 1.250);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL");
|
|
||||||
|
|
||||||
// FC23
|
|
||||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
|
|
||||||
|
|
||||||
if(!$recData) {
|
|
||||||
// Print error information if any
|
|
||||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>";
|
|
||||||
//
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "writeMultipleRegister (FC26): DONE";
|
|
||||||
?>
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMaster.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster($test_host_ip, "Mismatch");
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(1000, 2000, 1.250, 1.250);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL");
|
|
||||||
|
|
||||||
// FC23
|
|
||||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
|
|
||||||
|
|
||||||
// Should through an Exception
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "Something wrong!";
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMaster.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster($test_host_ip, "UDP");
|
|
||||||
|
|
||||||
// Data to be writen - BYTE
|
|
||||||
$data = array(0, 1, 1, pow(2,8)-1, pow(2,8)-1);
|
|
||||||
$dataTypes = array("BYTE", "BYTE", "BYTE", "BYTE", "BYTE");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - INT
|
|
||||||
$data = array(0, 1, -1, pow(2,15)-1, -pow(2,15));
|
|
||||||
$dataTypes = array("INT", "INT", "INT", "INT", "INT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - DINT
|
|
||||||
$data = array(0, 1, -1, pow(2,31)-1, -pow(2,31));
|
|
||||||
$dataTypes = array("DINT", "DINT", "DINT", "DINT", "DINT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - REAL
|
|
||||||
$data = array(0, 1, -2, 1/3, 25);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL", "REAL");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMaster.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMaster($test_host_ip, "UDP");
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(1000, 2000, 1.250, 1.250);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL");
|
|
||||||
|
|
||||||
// FC23
|
|
||||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
|
|
||||||
|
|
||||||
if(!$recData) {
|
|
||||||
// Print error information if any
|
|
||||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>";
|
|
||||||
//
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "writeMultipleRegister (FC26): DONE";
|
|
||||||
?>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
@echo off
|
|
||||||
call ../config.bat
|
|
||||||
|
|
||||||
for %%f in (test.*.php) do %php% -q "%%f" > "output/%%f.html"
|
|
||||||
|
|
||||||
cd output
|
|
||||||
for %%f in (*.html) do %diff% "%%f" ../ref/"%%f"
|
|
||||||
cd ..
|
|
||||||
pause
|
|
||||||
|
|
||||||
@echo on
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 255
|
|
||||||
[8] => 0
|
|
||||||
[9] => 255
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 255
|
|
||||||
[5] => 255
|
|
||||||
[6] => 127
|
|
||||||
[7] => 255
|
|
||||||
[8] => 128
|
|
||||||
[9] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 0
|
|
||||||
[8] => 255
|
|
||||||
[9] => 255
|
|
||||||
[10] => 255
|
|
||||||
[11] => 255
|
|
||||||
[12] => 255
|
|
||||||
[13] => 255
|
|
||||||
[14] => 127
|
|
||||||
[15] => 255
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 128
|
|
||||||
[19] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 0
|
|
||||||
[6] => 63
|
|
||||||
[7] => 128
|
|
||||||
[8] => 0
|
|
||||||
[9] => 0
|
|
||||||
[10] => 192
|
|
||||||
[11] => 0
|
|
||||||
[12] => 170
|
|
||||||
[13] => 171
|
|
||||||
[14] => 62
|
|
||||||
[15] => 170
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 65
|
|
||||||
[19] => 200
|
|
||||||
)
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
writeMultipleRegister (FC26): DONE
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterTcp.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMasterTcp($test_host_ip);
|
|
||||||
|
|
||||||
// Data to be writen - BYTE
|
|
||||||
$data = array(0, 1, 1, pow(2,8)-1, pow(2,8)-1);
|
|
||||||
$dataTypes = array("BYTE", "BYTE", "BYTE", "BYTE", "BYTE");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - INT
|
|
||||||
$data = array(0, 1, -1, pow(2,15)-1, -pow(2,15));
|
|
||||||
$dataTypes = array("INT", "INT", "INT", "INT", "INT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - DINT
|
|
||||||
$data = array(0, 1, -1, pow(2,31)-1, -pow(2,31));
|
|
||||||
$dataTypes = array("DINT", "DINT", "DINT", "DINT", "DINT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - REAL
|
|
||||||
$data = array(0, 1, -2, 1/3, 25);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL", "REAL");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterTcp.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMasterTcp($test_host_ip);
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(1000, 2000, 1.250, 1.250);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL");
|
|
||||||
|
|
||||||
// FC23
|
|
||||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
|
|
||||||
|
|
||||||
if(!$recData) {
|
|
||||||
// Print error information if any
|
|
||||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>";
|
|
||||||
//
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "writeMultipleRegister (FC26): DONE";
|
|
||||||
?>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
@echo off
|
|
||||||
call ../config.bat
|
|
||||||
|
|
||||||
for %%f in (test.*.php) do %php% -q "%%f" > "output/%%f.html"
|
|
||||||
|
|
||||||
cd output
|
|
||||||
for %%f in (*.html) do %diff% "%%f" ../ref/"%%f"
|
|
||||||
cd ..
|
|
||||||
pause
|
|
||||||
|
|
||||||
@echo on
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 255
|
|
||||||
[8] => 0
|
|
||||||
[9] => 255
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 255
|
|
||||||
[5] => 255
|
|
||||||
[6] => 127
|
|
||||||
[7] => 255
|
|
||||||
[8] => 128
|
|
||||||
[9] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 0
|
|
||||||
[8] => 255
|
|
||||||
[9] => 255
|
|
||||||
[10] => 255
|
|
||||||
[11] => 255
|
|
||||||
[12] => 255
|
|
||||||
[13] => 255
|
|
||||||
[14] => 127
|
|
||||||
[15] => 255
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 128
|
|
||||||
[19] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 0
|
|
||||||
[6] => 63
|
|
||||||
[7] => 128
|
|
||||||
[8] => 0
|
|
||||||
[9] => 0
|
|
||||||
[10] => 192
|
|
||||||
[11] => 0
|
|
||||||
[12] => 170
|
|
||||||
[13] => 170
|
|
||||||
[14] => 62
|
|
||||||
[15] => 170
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 65
|
|
||||||
[19] => 200
|
|
||||||
)
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 255
|
|
||||||
[8] => 0
|
|
||||||
[9] => 255
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 1
|
|
||||||
[4] => 255
|
|
||||||
[5] => 255
|
|
||||||
[6] => 127
|
|
||||||
[7] => 255
|
|
||||||
[8] => 128
|
|
||||||
[9] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 1
|
|
||||||
[6] => 0
|
|
||||||
[7] => 0
|
|
||||||
[8] => 255
|
|
||||||
[9] => 255
|
|
||||||
[10] => 255
|
|
||||||
[11] => 255
|
|
||||||
[12] => 255
|
|
||||||
[13] => 255
|
|
||||||
[14] => 127
|
|
||||||
[15] => 255
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 128
|
|
||||||
[19] => 0
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => 0
|
|
||||||
[1] => 0
|
|
||||||
[2] => 0
|
|
||||||
[3] => 0
|
|
||||||
[4] => 0
|
|
||||||
[5] => 0
|
|
||||||
[6] => 63
|
|
||||||
[7] => 128
|
|
||||||
[8] => 0
|
|
||||||
[9] => 0
|
|
||||||
[10] => 192
|
|
||||||
[11] => 0
|
|
||||||
[12] => 170
|
|
||||||
[13] => 171
|
|
||||||
[14] => 62
|
|
||||||
[15] => 170
|
|
||||||
[16] => 0
|
|
||||||
[17] => 0
|
|
||||||
[18] => 65
|
|
||||||
[19] => 200
|
|
||||||
)
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
writeMultipleRegister (FC26): DONE
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
writeMultipleRegister (FC26): DONE
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<?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 - BOOL array
|
|
||||||
$data = array(1, 0, TRUE, TRUE, 0, 1, TRUE, TRUE,
|
|
||||||
1, 1, TRUE, TRUE, 0, 0, FALSE, FALSE,
|
|
||||||
0, 0, FALSE, FALSE, 1, 1, TRUE, TRUE,
|
|
||||||
1, 1, TRUE, TRUE, 1, 1, TRUE, TRUE);
|
|
||||||
// Write data - FC 15
|
|
||||||
$modbus->writeMultipleCoils(0, 12288, $data);
|
|
||||||
echo $modbus->status;
|
|
||||||
$modbus->status = "";
|
|
||||||
echo "\n\n";
|
|
||||||
// Read data - FC 1
|
|
||||||
$recData = $modbus->readCoils(0, 12288, 32);
|
|
||||||
echo $modbus->status;
|
|
||||||
echo "\n\n";
|
|
||||||
var_dump($recData);
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<?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 - BYTE
|
|
||||||
$data = array(0, 1, 1, pow(2,8)-1, pow(2,8)-1);
|
|
||||||
$dataTypes = array("BYTE", "BYTE", "BYTE", "BYTE", "BYTE");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - INT
|
|
||||||
$data = array(0, 1, -1, pow(2,15)-1, -pow(2,15));
|
|
||||||
$dataTypes = array("INT", "INT", "INT", "INT", "INT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - DINT
|
|
||||||
$data = array(0, 1, -1, pow(2,31)-1, -pow(2,31));
|
|
||||||
$dataTypes = array("DINT", "DINT", "DINT", "DINT", "DINT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - REAL
|
|
||||||
$data = array(0, 1, -2, 1/3, 25);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL", "REAL");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMasterUdp($test_host_ip);
|
|
||||||
$modbus->client = $test_bind_client_ip;
|
|
||||||
|
|
||||||
// Data to be writen - BYTE
|
|
||||||
$data = array(0, 1, 1, pow(2,8)-1, pow(2,8)-1);
|
|
||||||
$dataTypes = array("BYTE", "BYTE", "BYTE", "BYTE", "BYTE");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - INT
|
|
||||||
$data = array(0, 1, -1, pow(2,15)-1, -pow(2,15));
|
|
||||||
$dataTypes = array("INT", "INT", "INT", "INT", "INT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 5);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - DINT
|
|
||||||
$data = array(0, 1, -1, pow(2,31)-1, -pow(2,31));
|
|
||||||
$dataTypes = array("DINT", "DINT", "DINT", "DINT", "DINT");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
// Data to be writen - REAL
|
|
||||||
$data = array(0, 1, -2, 1/3, 25);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL", "REAL");
|
|
||||||
// Write data - FC 16
|
|
||||||
$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
|
|
||||||
// Read data - FC3
|
|
||||||
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
|
|
||||||
print_r($recData);
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
<?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
|
|
||||||
$data = array(1000, 2000, 1.250, 1.250);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL");
|
|
||||||
|
|
||||||
// FC23
|
|
||||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
|
|
||||||
|
|
||||||
if(!$recData) {
|
|
||||||
// Print error information if any
|
|
||||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>";
|
|
||||||
//
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "writeMultipleRegister (FC26): DONE";
|
|
||||||
?>
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
require_once dirname(__FILE__) . '/../config.php';
|
|
||||||
|
|
||||||
// Create Modbus object
|
|
||||||
$modbus = new ModbusMasterUdp($test_host_ip);
|
|
||||||
$modbus->client = $test_bind_client_ip;
|
|
||||||
|
|
||||||
// Data to be writen
|
|
||||||
$data = array(1000, 2000, 1.250, 1.250);
|
|
||||||
$dataTypes = array("REAL", "REAL", "REAL", "REAL");
|
|
||||||
|
|
||||||
// FC23
|
|
||||||
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
|
|
||||||
|
|
||||||
if(!$recData) {
|
|
||||||
// Print error information if any
|
|
||||||
echo "</br>Error:</br>" . $modbus->errstr . "</br>";
|
|
||||||
//
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print status information
|
|
||||||
echo "writeMultipleRegister (FC26): DONE";
|
|
||||||
?>
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
@echo off
|
|
||||||
call ../config.bat
|
|
||||||
|
|
||||||
for %%f in (test.*.php) do %php% -q "%%f" > "output/%%f.html"
|
|
||||||
|
|
||||||
cd output
|
|
||||||
for %%f in (*.html) do %diff% "%%f" ../ref/"%%f"
|
|
||||||
cd ..
|
|
||||||
pause
|
|
||||||
|
|
||||||
@echo on
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
32098<br>0<br>0<br>-1<br>-25000<br>25000<br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
1000<br>2000<br>1.25<br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
-1<br>0<br>1<br>-2147483648<br>2147483647<br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
eHll oowlr!da<br>Hello world!<br>
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
float(4294967295)
|
|
||||||
<br>int(0)
|
|
||||||
<br>int(1)
|
|
||||||
<br>float(2147483648)
|
|
||||||
<br>int(2147483647)
|
|
||||||
<br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
Exception 'Data are not in array 2 or 4 bytes'<br>25602<br>Exception 'Data are not in array 2 or 4 bytes'<br>25602<br>Exception 'Data are not in array 2 or 4 bytes'<br>
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
Exception 'Data are not numeric'
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting Mixed values
|
|
||||||
$data = Array (
|
|
||||||
"0" => 125, // 32098 (DINT)
|
|
||||||
"1" => 98,
|
|
||||||
"2" => 0,
|
|
||||||
"3" => 0,
|
|
||||||
"4" => 0, // 0 (DINT)
|
|
||||||
"5" => 0,
|
|
||||||
"6" => 0,
|
|
||||||
"7" => 0,
|
|
||||||
"8" => 0, // 0 (DINT)
|
|
||||||
"9" => 0,
|
|
||||||
"10" => 0,
|
|
||||||
"11" => 0,
|
|
||||||
"12" => 255, // -1 (DINT)
|
|
||||||
"13" => 255,
|
|
||||||
"14" => 255,
|
|
||||||
"15" => 255,
|
|
||||||
"16" => 158, // -25000 (INT)
|
|
||||||
"17" => 88,
|
|
||||||
"18" => 97, // 25000 (INT)
|
|
||||||
"19" => 168
|
|
||||||
);
|
|
||||||
|
|
||||||
// Print mixed values
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4)) . "<br>";
|
|
||||||
echo PhpType::bytes2signedInt(array_slice($data, 4, 4)) . "<br>";
|
|
||||||
echo PhpType::bytes2signedInt(array_slice($data, 8, 4)) . "<br>";
|
|
||||||
echo PhpType::bytes2signedInt(array_slice($data, 12, 4)) . "<br>";
|
|
||||||
echo PhpType::bytes2signedInt(array_slice($data, 16, 2)) . "<br>";
|
|
||||||
echo PhpType::bytes2signedInt(array_slice($data, 18, 2)) . "<br>";
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting 3 REAL values (6 words)
|
|
||||||
$data = array( // 1000
|
|
||||||
0 => 0,
|
|
||||||
1 => 0,
|
|
||||||
2 => 68,
|
|
||||||
3 => 122,
|
|
||||||
4 => 0,
|
|
||||||
5 => 0,
|
|
||||||
6 => 68,
|
|
||||||
7 => 250,
|
|
||||||
8 => 0,
|
|
||||||
9 => 0,
|
|
||||||
10 => 63,
|
|
||||||
11 => 160,
|
|
||||||
);
|
|
||||||
|
|
||||||
$dword = array_chunk($data, 4);
|
|
||||||
|
|
||||||
// Print float interpretation of the real value
|
|
||||||
echo PhpType::bytes2float($dword[0]) . "<br>";
|
|
||||||
echo PhpType::bytes2float($dword[1]) . "<br>";
|
|
||||||
echo PhpType::bytes2float($dword[2]) . "<br>";
|
|
||||||
?>
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting DINT values
|
|
||||||
$data = array(
|
|
||||||
0xFF, // -1
|
|
||||||
0xFF,
|
|
||||||
0xFF,
|
|
||||||
0xFF,
|
|
||||||
0, // 0
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0, // 1
|
|
||||||
0x01,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0, // minus max
|
|
||||||
0,
|
|
||||||
0x80,
|
|
||||||
0x0,
|
|
||||||
0xFF, // plus max
|
|
||||||
0xFF,
|
|
||||||
0x7F,
|
|
||||||
0xFF,
|
|
||||||
);
|
|
||||||
|
|
||||||
$dword = array_chunk($data, 4);
|
|
||||||
|
|
||||||
// Print float interpretation of the real value
|
|
||||||
foreach($dword as $value) {
|
|
||||||
echo PhpType::bytes2signedInt($value) . "<br>";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting 3 REAL values (6 words)
|
|
||||||
$data = array( // String "Hello word!"
|
|
||||||
0x48, //H
|
|
||||||
0x65, //e
|
|
||||||
0x6c, //l
|
|
||||||
0x6c, //l
|
|
||||||
0x6f, //o
|
|
||||||
0x20, //
|
|
||||||
0x77, //w
|
|
||||||
0x6f, //o
|
|
||||||
0x72, //r
|
|
||||||
0x6c, //l
|
|
||||||
0x64, //d
|
|
||||||
0x21, //!
|
|
||||||
0x00, //\0
|
|
||||||
0x61, //a
|
|
||||||
0x61 //a
|
|
||||||
);
|
|
||||||
|
|
||||||
// Print string interpretation of the values
|
|
||||||
echo PhpType::bytes2string($data) . "<br>";
|
|
||||||
echo PhpType::bytes2string($data, true) . "<br>";
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting DINT values
|
|
||||||
$data = array(
|
|
||||||
0xFF, // -1
|
|
||||||
0xFF,
|
|
||||||
0xFF,
|
|
||||||
0xFF,
|
|
||||||
0, // 0
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0, // 1
|
|
||||||
0x01,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0, // minus max
|
|
||||||
0,
|
|
||||||
0x80,
|
|
||||||
0x0,
|
|
||||||
0xFF, // plus max
|
|
||||||
0xFF,
|
|
||||||
0x7F,
|
|
||||||
0xFF,
|
|
||||||
);
|
|
||||||
|
|
||||||
$dword = array_chunk($data, 4);
|
|
||||||
|
|
||||||
// Print float interpretation of the real value
|
|
||||||
foreach($dword as $value) {
|
|
||||||
var_dump(PhpType::bytes2unsignedInt($value));
|
|
||||||
echo "<br>";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<?php
|
|
||||||
/*
|
|
||||||
* To change this template, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting Mixed values
|
|
||||||
$data = Array (
|
|
||||||
"0" => 100, // 32098 (DINT)
|
|
||||||
"1" => 2,
|
|
||||||
"2" => 0,
|
|
||||||
"3" => 0,
|
|
||||||
"4" => 100, // 32098 (DINT)
|
|
||||||
"5" => 2
|
|
||||||
);
|
|
||||||
|
|
||||||
// Print mixed values
|
|
||||||
try {
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 1)) . "<br>";
|
|
||||||
} catch(Exception $e) {
|
|
||||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 2)). "<br>";
|
|
||||||
} catch(Exception $e) {
|
|
||||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 3)). "<br>";
|
|
||||||
} catch(Exception $e) {
|
|
||||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4)). "<br>";
|
|
||||||
} catch(Exception $e) {
|
|
||||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 5)). "<br>";
|
|
||||||
} catch(Exception $e) {
|
|
||||||
echo "Exception 'Data are not in array 2 or 4 bytes'". "<br>";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
/*
|
|
||||||
* To change this template, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
|
|
||||||
|
|
||||||
// Received bytes interpreting Mixed values
|
|
||||||
$data = Array (
|
|
||||||
"0" => 100, // 32098 (DINT)
|
|
||||||
"1" => "e",
|
|
||||||
"2" => 0,
|
|
||||||
"3" => 0
|
|
||||||
);
|
|
||||||
|
|
||||||
// Print mixed values
|
|
||||||
try {
|
|
||||||
echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4));
|
|
||||||
} catch(Exception $e) {
|
|
||||||
echo "Exception 'Data are not numeric'";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
set php=c:\Program_Files\xampplite\php\php.exe
|
|
||||||
rem set php=C:\PHP\versions\php-5.2.6-Win32\php.exe -d auto_prepend_file=C:\PHP\locale.php
|
|
||||||
set diff="diff.exe"
|
|
||||||
rem set testUri=http://localHost/nette/_trunk/tests
|
|
||||||
rem set wget=wget.exe
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
<?php
|
|
||||||
$test_host_ip = "192.192.15.51";
|
|
||||||
$test_bind_client_ip = "192.192.15.113";
|
|
||||||
?>
|
|
||||||
@@ -1,173 +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 3: read multiple registers</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>
|
|
||||||
<note>
|
|
||||||
Developed with support of <graphic fileref="wago_logo.png"/> {@link http://www.wago.com}.
|
|
||||||
</note>
|
|
||||||
</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_fc3}">
|
|
||||||
<title>FC3 - read mutliple registers</title>
|
|
||||||
<para>
|
|
||||||
FC3 functionality example
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
{@example example_fc3.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