6 Commits
Author SHA1 Message Date
John Long 82c6fcf1d1 0.4.1 2013-06-14 07:40:48 -05:00
John Long 94772fcd7c Added tag 0.4 for changeset bdf8b3f414c4 2013-06-14 07:39:18 -05:00
John Long 71f36c3063 0.4 Release 2013-06-14 07:39:15 -05:00
John Long 89b18aea02 Added tag 0.3 for changeset a7f90c18859d 2013-06-14 07:38:17 -05:00
John Long da9731662c 0.3 Release 2013-06-14 07:38:13 -05:00
John Long fbb70f23e5 Added tag 0.2 for changeset a88b5a87fb4c 2013-06-14 07:33:02 -05:00
46 changed files with 1295 additions and 586 deletions
+3
View File
@@ -0,0 +1,3 @@
a88b5a87fb4cd49f16ce72062cce19d55f1fbf98 0.2
a7f90c18859d31922bdd8c61299e2790f5e2eb1a 0.3
bdf8b3f414c4d08fc0edf9fd1953f55db26e4bac 0.4
+103 -148
View File
@@ -1,12 +1,12 @@
<?php <?php
/** /**
* Phpmodbus Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * 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 * This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt. * with this package in the file license.txt.
* *
* @author Jan Krakora * @author Jan Krakora
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @license PhpModbus license * @license PhpModbus license
* @category Phpmodbus * @category Phpmodbus
* @package Phpmodbus * @package Phpmodbus
@@ -20,158 +20,113 @@
* data types to a IEC data type. * data types to a IEC data type.
* *
* @author Jan Krakora * @author Jan Krakora
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @package Phpmodbus * @package Phpmodbus
*/ */
class IecType { class IecType {
/** /**
* iecBYTE * iecBYTE
* *
* Converts a value to IEC-1131 BYTE data type * Converts a value to IEC-1131 BYTE data type
* *
* @param value value from 0 to 255 * @param value value from 0 to 255
* @return value IEC BYTE data type * @return value IEC BYTE data type
* *
*/ */
function iecBYTE($value){ function iecBYTE($value) {
return chr($value & 0xFF); 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]*
*
* @param float value to be converted
* @return value IEC REAL data type
*/
private function float2iecReal($value){
$bias = 128;
$cnst = 281; // 1 (carry bit) + 127 + 1 + 126 + 24 + 2 (round bits)
$two_power_x = array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576,
2097152, 4194304);
//convert and seperate input to integer and decimal parts
$val = abs($value);
$intpart = floor($val);
$decpart = $val - $intpart;
//convert integer part
for ($i=0;$i<$cnst;$i++) $real_significand_bin[$i] = 0;
$i = $bias;
while ((($intpart / 2) != 0) && ($i >= 0))
{
$real_significand_bin[$i] = $intpart % 2;
if (($intpart % 2) == 0) $intpart = $intpart / 2;
else $intpart = $intpart / 2 - 0.5;
$i -= 1;
} }
//convert decimal part
$i = $bias+1;
while (($decpart > 0) && ($i < $cnst))
{
$decpart *= 2;
if ($decpart >= 1) {
$real_significand_bin[$i] = 1;
$decpart --;
$i++;
}
else
{
$real_significand_bin[i] = 0;
$i++;
}
}
//obtain exponent value
$i = 0;
//find most significant bit of significand
while (($i < $cnst) && ($real_significand_bin[$i] != 1)) $i++;
//
$index_exp = $i;
$real_exponent = 128 - $index_exp;
if ($real_exponent < -126) return 0;
if (($real_exponent > 127)&&($real_float>0)) return 0x7F7FFFFF;
if (($real_exponent > 127)&&($real_float<0)) return 0xFF7FFFFF;
for ($i=0; $i<23; $i++)
$real_significand = $real_significand + $real_significand_bin[$index_exp+1+$i] * $two_power_x[22-$i];
// return
if ($value<0) $w = 0x80000000 + ($real_significand & 0x7FFFFF) + ((($real_exponent+127)<<23) & 0x7F800000);
else $w = ($real_significand & 0x7FFFFF) + ((($real_exponent+127)<<23) & 0x7F800000);
return $w;
}
/** /**
* endianness * iecINT
* *
* Make endianess as required. * Converts a value to IEC-1131 INT data type
* For more see http://en.wikipedia.org/wiki/Endianness *
* * @param value value to be converted
* @param int $value * @return value IEC-1131 INT data type
* @param bool $endianness *
* @return int */
*/ function iecINT($value) {
private function endianness($value, $endianness = 0){ return self::iecBYTE(($value >> 8) & 0x00FF) .
if ($endianness == 0) self::iecBYTE(($value & 0x00FF));
return }
self::iecBYTE(($value >> 8) & 0x000000FF) .
self::iecBYTE(($value & 0x000000FF)) . /**
self::iecBYTE(($value >> 24) & 0x000000FF) . * iecDINT
self::iecBYTE(($value >> 16) & 0x000000FF); *
else * Converts a value to IEC-1131 DINT data type
return *
self::iecBYTE(($value >> 24) & 0x000000FF) . * @param value value to be converted
self::iecBYTE(($value >> 16) & 0x000000FF) . * @param value endianness defines endian codding (little endian == 0, big endian == 1)
self::iecBYTE(($value >> 8) & 0x000000FF) . * @return value IEC-1131 INT data type
self::iecBYTE(($value & 0x000000FF)); *
} */
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));
}
} }
+289 -59
View File
@@ -1,12 +1,12 @@
<?php <?php
/** /**
* Phpmodbus Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * 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 * This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt. * with this package in the file license.txt.
* *
* *
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @license PhpModbus license * @license PhpModbus license
* @category Phpmodbus * @category Phpmodbus
* @tutorial Phpmodbus.pkg * @tutorial Phpmodbus.pkg
@@ -29,7 +29,7 @@ require_once dirname(__FILE__) . '/PhpType.php';
* - FC 23: read write registers * - FC 23: read write registers
* *
* @author Jan Krakora * @author Jan Krakora
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @package Phpmodbus * @package Phpmodbus
* *
*/ */
@@ -37,7 +37,8 @@ class ModbusMasterUdp {
var $sock; var $sock;
var $port = "502"; var $port = "502";
var $host = "192.168.1.1"; var $host = "192.168.1.1";
var $errstr; var $client = "";
var $client_port = "502";
var $status; var $status;
var $timeout_sec = 5; // 5 sec var $timeout_sec = 5; // 5 sec
var $endianess = 0; // defines endian codding (little endian == 0, big endian == 1) var $endianess = 0; // defines endian codding (little endian == 0, big endian == 1)
@@ -53,6 +54,15 @@ class ModbusMasterUdp {
$this->host = $host; $this->host = $host;
} }
/**
* __toString
*
* Magic method
*/
function __toString() {
return "<pre>" . $this->status . "</pre>";
}
/** /**
* connect * connect
* *
@@ -63,14 +73,23 @@ class ModbusMasterUdp {
private function connect(){ private function connect(){
// UDP socket // UDP socket
$this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_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 // connect
$result = @socket_connect($this->sock, $this->host, $this->port); $result = @socket_connect($this->sock, $this->host, $this->port);
if ($result === false) { if ($result === false) {
$this->errstr .= "socket_connect() failed.</br>Reason: ($result) " . throw new Exception("socket_connect() failed.</br>Reason: ($result)".
socket_strerror(socket_last_error($this->sock)); socket_strerror(socket_last_error($this->sock)));
return false;
} else { } else {
$this->status .= "Connected</br>"; $this->status .= "Connected\n";
return true; return true;
} }
} }
@@ -82,7 +101,7 @@ class ModbusMasterUdp {
*/ */
private function disconnect(){ private function disconnect(){
socket_close($this->sock); socket_close($this->sock);
$this->status .= "Disconnected</br>"; $this->status .= "Disconnected\n";
} }
/** /**
@@ -94,7 +113,7 @@ class ModbusMasterUdp {
*/ */
private function send($packet){ private function send($packet){
socket_write($this->sock, $packet, strlen($packet)); socket_write($this->sock, $packet, strlen($packet));
$this->status .= "Send</br>"; $this->status .= "Send\n";
} }
/** /**
@@ -116,19 +135,18 @@ class ModbusMasterUdp {
$exceptsocks, $exceptsocks,
0, 0,
300000) !== FALSE) { 300000) !== FALSE) {
$this->status .= "Wait received data</br>"; $this->status .= "Wait data ... \n";
if (in_array($this->sock, $readsocks)) { if (in_array($this->sock, $readsocks)) {
while (@socket_recv($this->sock, $rec, 2000, 0)) { while (@socket_recv($this->sock, $rec, 2000, 0)) {
$this->status .= "Received</br>"; $this->status .= "Data received\n";
return $rec; return $rec;
} }
$lastAccess = time(); $lastAccess = time();
} else { } else {
if (time()-$lastAccess >= $this->timeout_sec) { if (time()-$lastAccess >= $this->timeout_sec) {
$this->errstr .= "Watchdog time expired [ " . throw new Exception( "Watchdog time expired [ " .
$this->timeout_sec . " sec]!!! Connection to " . $this->timeout_sec . " sec]!!! Connection to " .
$this->host . " is not established."; $this->host . " is not established.");
return false;
} }
} }
$readsocks[] = $this->sock; $readsocks[] = $this->sock;
@@ -145,16 +163,128 @@ class ModbusMasterUdp {
*/ */
private function responseCode($packet){ private function responseCode($packet){
if(($packet[7] & 0x80) > 0) { if(($packet[7] & 0x80) > 0) {
$this->errstr .= "Modbus response error code:" . ord($packet[8]); throw new Exception("Modbus response error code:" . ord($packet[8]));
return false; } else {
} $this->status .= "Modbus response error code: NOERROR\n";
else
{
$this->status .= "Modbus response error code: NOERROR</br>";
return true; 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 * readMultipleRegisters
* *
@@ -171,27 +301,21 @@ class ModbusMasterUdp {
* @return false|Array Success flag or array of received data. * @return false|Array Success flag or array of received data.
*/ */
function readMultipleRegisters($unitId, $reference, $quantity){ function readMultipleRegisters($unitId, $reference, $quantity){
$this->errstr = ""; $this->status .= "readMultipleRegisters: START\n";
$this->status = "readMultipleRegisters: START</br>";
// connect // connect
if(!$this->connect()) $this->connect();
return false;
// send FC 3 // send FC 3
$packet = $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity); $packet = $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet); $this->status .= $this->printPacket($packet);
$this->send($packet); $this->send($packet);
// receive response // receive response
$rpacket = $this->rec(); $rpacket = $this->rec();
if(!$rpacket)
return false;
$this->status .= $this->printPacket($rpacket); $this->status .= $this->printPacket($rpacket);
// parse packet // parse packet
$receivedData = $this->readMultipleRegistersParser($rpacket); $receivedData = $this->readMultipleRegistersParser($rpacket);
if(!$receivedData)
return false;
// disconnect // disconnect
$this->disconnect(); $this->disconnect();
$this->status .= "readMultipleRegisters: DONE</br>"; $this->status .= "readMultipleRegisters: DONE\n";
// return // return
return $receivedData; return $receivedData;
} }
@@ -222,6 +346,8 @@ class ModbusMasterUdp {
*/ */
private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){ private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){
$dataLen = 0; $dataLen = 0;
// build data section
$buffer1 = "";
// build body // build body
$buffer2 = ""; $buffer2 = "";
$buffer2 .= iecType::iecBYTE(3); // FC 3 = 3(0x03) $buffer2 .= iecType::iecBYTE(3); // FC 3 = 3(0x03)
@@ -249,9 +375,8 @@ class ModbusMasterUdp {
*/ */
private function readMultipleRegistersParser($packet){ private function readMultipleRegistersParser($packet){
$data = array(); $data = array();
// if not exception // check Response code
if(!$this->responseCode($packet)) $this->responseCode($packet);
return false;
// get data // get data
for($i=0;$i<ord($packet[8]);$i++){ for($i=0;$i<ord($packet[8]);$i++){
$data[$i] = ord($packet[9+$i]); $data[$i] = ord($packet[9+$i]);
@@ -259,6 +384,123 @@ class ModbusMasterUdp {
return $data; 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 * writeMultipleRegister
* *
@@ -275,26 +517,21 @@ class ModbusMasterUdp {
* @return bool Success flag * @return bool Success flag
*/ */
function writeMultipleRegister($unitId, $reference, $data, $dataTypes){ function writeMultipleRegister($unitId, $reference, $data, $dataTypes){
$this->errstr = ""; $this->status .= "writeMultipleRegister: START\n";
$this->status = "writeMultipleRegister: START</br>";
// connect // connect
if(!$this->connect()) $this->connect();
return false;
// send FC16 // send FC16
$packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes); $packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes);
$this->status .= $this->printPacket($packet); $this->status .= $this->printPacket($packet);
$this->send($packet); $this->send($packet);
// receive response // receive response
$rpacket = $this->rec(); $rpacket = $this->rec();
if(!$rpacket)
return false;
$this->status .= $this->printPacket($rpacket); $this->status .= $this->printPacket($rpacket);
// parse packet // parse packet
if(!$this->writeMultipleRegisterParser($rpacket)) $this->writeMultipleRegisterParser($rpacket);
return false;
// disconnect // disconnect
$this->disconnect(); $this->disconnect();
$this->status .= "writeMultipleRegister: DONE</br>"; $this->status .= "writeMultipleRegister: DONE\n";
return true; return true;
} }
@@ -337,11 +574,11 @@ class ModbusMasterUdp {
$dataLen += 2; $dataLen += 2;
} }
elseif($dataTypes[$key]=="DINT"){ elseif($dataTypes[$key]=="DINT"){
$buffer1 .= iecType::iecDINT($dataitem, $endianess); // register values x $buffer1 .= iecType::iecDINT($dataitem, $this->endianness); // register values x
$dataLen += 4; $dataLen += 4;
} }
elseif($dataTypes[$key]=="REAL") { elseif($dataTypes[$key]=="REAL") {
$buffer1 .= iecType::iecREAL($dataitem, $endianess); // register values x $buffer1 .= iecType::iecREAL($dataitem, $this->endianness); // register values x
$dataLen += 4; $dataLen += 4;
} }
else{ else{
@@ -376,8 +613,7 @@ class ModbusMasterUdp {
* @return bool * @return bool
*/ */
private function writeMultipleRegisterParser($packet){ private function writeMultipleRegisterParser($packet){
if(!$this->responseCode($rpacket)) $this->responseCode($packet);
return false;
return true; return true;
} }
@@ -400,27 +636,21 @@ class ModbusMasterUdp {
* @return false|Array Success flag or array of data. * @return false|Array Success flag or array of data.
*/ */
function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){ function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
$this->errstr = ""; $this->status .= "readWriteRegisters: START\n";
$this->status = "readWriteRegisters: START</br>";
// connect // connect
if(!$this->connect()) $this->connect();
return false;
// send FC23 // send FC23
$packet = $this->readWriteRegistersPacketBuilder($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes); $packet = $this->readWriteRegistersPacketBuilder($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes);
$this->status .= $this->printPacket($packet); $this->status .= $this->printPacket($packet);
$this->send($packet); $this->send($packet);
// receive response // receive response
$rpacket = $this->rec(); $rpacket = $this->rec();
if(!$rpacket)
return false;
$this->status .= $this->printPacket($rpacket); $this->status .= $this->printPacket($rpacket);
// parse packet // parse packet
$receivedData = $this->readWriteRegistersParser($rpacket); $receivedData = $this->readWriteRegistersParser($rpacket);
if(!$receivedData)
return false;
// disconnect // disconnect
$this->disconnect(); $this->disconnect();
$this->status .= "writeMultipleRegister: DONE</br>"; $this->status .= "writeMultipleRegister: DONE\n";
// return // return
return $receivedData; return $receivedData;
} }
@@ -466,11 +696,11 @@ class ModbusMasterUdp {
$dataLen += 2; $dataLen += 2;
} }
elseif($dataTypes[$key]=="DINT"){ elseif($dataTypes[$key]=="DINT"){
$buffer1 .= iecType::iecDINT($dataitem, $endianess); // register values x $buffer1 .= iecType::iecDINT($dataitem, $this->endianness); // register values x
$dataLen += 4; $dataLen += 4;
} }
elseif($dataTypes[$key]=="REAL") { elseif($dataTypes[$key]=="REAL") {
$buffer1 .= iecType::iecREAL($dataitem, $endianess); // register values x $buffer1 .= iecType::iecREAL($dataitem, $this->endianness); // register values x
$dataLen += 4; $dataLen += 4;
} }
else{ else{
@@ -538,7 +768,7 @@ class ModbusMasterUdp {
/** /**
* printPacket * printPacket
* *
* Print whole packet in the hex form * Print a packet in the hex form
* *
* @param string $packet * @param string $packet
* @return string * @return string
@@ -549,7 +779,7 @@ class ModbusMasterUdp {
for($i=0;$i<strlen($packet);$i++){ for($i=0;$i<strlen($packet);$i++){
$str .= $this->byte2hex(ord($packet[$i])); $str .= $this->byte2hex(ord($packet[$i]));
} }
$str .= "</br>"; $str .= "\n";
return $str; return $str;
} }
} }
+211 -181
View File
@@ -1,12 +1,12 @@
<? <?php
/** /**
* Phpmodbus Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * 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 * This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt. * with this package in the file license.txt.
* *
* @author Jan Krakora * @author Jan Krakora
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @license PhpModbus license * @license PhpModbus license
* @category Phpmodbus * @category Phpmodbus
* @package Phpmodbus * @package Phpmodbus
@@ -21,199 +21,229 @@
* (array of bytes) to the PHP data type, i.e. signed int, unsigned int and float. * (array of bytes) to the PHP data type, i.e. signed int, unsigned int and float.
* *
* @author Jan Krakora * @author Jan Krakora
* @copyright Copyright (c) 2004, 2009 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com) * @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @package Phpmodbus * @package Phpmodbus
* *
*/ */
class PhpType { class PhpType {
/** /**
* bytes2float * bytes2float
* *
* The function converts array of 4 bytes to float. The return value * The function converts array of 4 bytes to float. The return value
* depends on order of the input bytes (endianning). * depends on order of the input bytes (endianning).
* *
* @param array $values * @param array $values
* @param bool $endianness * @param bool $endianness
* @return float * @return float
*/ */
public static function bytes2float($values, $endianness = 0){ public static function bytes2float($values, $endianness = 0) {
$data = array(); $data = array();
$real = 0; $real = 0;
// Set the array to correct form // Set the array to correct form
$data = self::checkData($values); $data = self::checkData($values);
// Combine bytes // Combine bytes
$real = self::combineBytes($data, $endianness); $real = self::combineBytes($data, $endianness);
// Convert the real value to float // Convert the real value to float
return (float) self::real2float($real); 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);
}
/**
* 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]
*
* @param value value in IEC REAL data type to be converted
* @return float float value
*/
private static function real2float($value){
$two_pow_minus_x = array(
1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625,
0.0078125, 0.00390625, 0.001953125, 0.0009765625,
0.00048828125, 0.000244140625, 0.0001220703125,
0.00006103515625, 0.000030517578125, 0.0000152587890625,
0.00000762939453125, 0.000003814697265625, 0.0000019073486328125,
0.00000095367431640625, 0.000000476837158203125,
0.0000002384185791015625, 0.00000011920928955078125);
// get sign, mantisa, exponent
$real_mantisa = $value & 0x7FFFFF | 0x800000;
$real_exponent = ($value>>23) & 0xFF;
$real_sign = ($value>>31) & 0x01;
$bin_exponent = $real_exponent - 127;
// decode value
if (( $bin_exponent >= -126) && ($bin_exponent <= 127)) {
// Mantissa decoding
for ($i=0; $i<24; $i++) {
if ($real_mantisa & 0x01)
$val += $two_pow_minus_x[23-$i];
$real_mantisa = $real_mantisa >> 1;
}
// Base
$val = $val * pow(2,$bin_exponent);
if (($real_sign == 1)) $val = -$val;
}
return (float)$val;
}
/**
* 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 * bytes2signedInt
* *
* Switch double word to unsigned integer * The function converts array of 2 or 4 bytes to signed integer.
* * The return value depends on order of the input bytes (endianning).
* @param int $value *
* @return int|float * @param array $values
*/ * @param bool $endianness
private static function dword2unsignedInt($value){ * @return int
if ((0x80000000 & $value) != 0) { */
return ((float) (0x7FFFFFFF & $value)) + 2147483648; public static function bytes2signedInt($values, $endianness = 0) {
} else { $data = array();
return (int) (0x7FFFFFFF & $value); $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);
} }
}
/** /**
* checkData * bytes2unsignedInt
* *
* Check if the data variable is array, and check if the values are numeric * The function converts array of 2 or 4 bytes to unsigned integer.
* * The return value depends on order of the input bytes (endianning).
* @param int $data *
* @return int * @param array $values
*/ * @param bool $endianness
private static function checkData($data){ * @return int|float
// Check the data */
if (!is_array($data)) { public static function bytes2unsignedInt($values, $endianness = 0) {
throw new Exception('The input data should be an array of bytes.'); $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);
} }
// Check the values to be number - must be
if (!is_numeric($data[0]) || !is_numeric($data[1])) { /**
throw new Exception('Data are not numeric.'); * 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;
} }
if (!is_numeric($data[2])) $data[2] = 0;
if (!is_numeric($data[3])) $data[3] = 0;
return $data; /**
} * 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];
* 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; /**
} * 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;
}
} }
?> ?>
+53 -59
View File
@@ -1,73 +1,67 @@
<?php <?php
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object // Create Modbus object
$ip = "192.168.1.99"; $ip = "192.192.15.51";
$modbus = new ModbusMasterUdp($ip); $modbus = new ModbusMasterUdp($ip);
// FC 3 try {
$moduleId = 0; // FC 3
$reference = 12288; $moduleId = 0;
$mw0address = 12288; $reference = 12288;
$quantity = 6; $mw0address = 12288;
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity); $quantity = 6;
$recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity);
}
catch (Exception $e) {
echo $modbus;
echo $e;
exit;
}
?> ?>
<html> <html>
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com"> <meta name="generator" content="PSPad editor, www.pspad.com">
<title>WAGO 750-841 M-memory dump</title> <title>WAGO 750-841 M-memory dump</title>
</head> </head>
<body> <body>
<h1>Dump of M-memory from WAGO 750-84x series coupler.</h1> <h1>Dump of M-memory from WAGO 750-84x series coupler.</h1>
<ul> <ul>
<li>PLC: 750-84x series</li> <li>PLC: 750-84x series</li>
<li>IP: <?=$ip?></li> <li>IP: <?php echo $ip?></li>
<li>Modbus module ID: <?=$moduleId?></li> <li>Modbus module ID: <?php echo $moduleId?></li>
<li>Modbus memory reference: <?=$reference?></li> <li>Modbus memory reference: <?php echo $reference?></li>
<li>Modbus memory quantity: <?=$quantity?></li> <li>Modbus memory quantity: <?php echo $quantity?></li>
</ul> </ul>
<h2>M-memory dump</h2> <h2>M-memory dump</h2>
<?php <table border="1px" width="400px">
if(!$recData) { <tr>
// Print error information if any <td>Modbus address</td>
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; <td>MWx</td>
} <td>value</td>
else </tr>
{ <?php
?> for($i=0;$i<count($recData);$i+=2) {
?>
<tr>
<td><?php echo $i+$reference?></td>
<td>MW<?php echo ($i + $reference - $mw0address)/2?></td>
<td><?php echo ($recData[$i] << 8)+ $recData[$i+1]?></td>
</tr>
<?php
}
?>
</table>
<table border="1px" width="400px"> <h2>Modbus class status</h2>
<tr> <?php
<td>Modbus address</td> echo $modbus;
<td>MWx</td> ?>
<td>value</td>
</tr>
<?php
for($i=0;$i<count($recData);$i+=2){
?>
<tr>
<td><?=$i+$reference?></td>
<td>MW<?=($i + $reference - $mw0address)/2?></td>
<td><?=($recData[$i] << 8)+ $recData[$i+1]?></td>
</tr>
<?php
}
?>
</table>
<?php </body>
}
?>
<h2>Modbus class status</h2>
<?php
echo $modbus->status;
?>
</body>
</html> </html>
+29 -23
View File
@@ -1,55 +1,61 @@
<?php <?php
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object // Create Modbus object
$modbus = new ModbusMasterUdp("192.168.1.99"); $modbus = new ModbusMasterUdp("192.192.15.51");
// FC 3 try {
// read 10 words (20 bytes) from device ID=0, address=12288 // FC 3
$recData = $modbus->readMultipleRegisters(0, 12288, 10); // read 10 words (20 bytes) from device ID=0, address=12288
$recData = $modbus->readMultipleRegisters(0, 12288, 10);
if(!$recData) { }
// Print error information if any catch (Exception $e) {
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; // Print error information if any
exit; echo $modbus;
echo $e;
exit;
} }
// Received data // Received data
echo "<h1>Received Data</h1>"; echo "<h1>Received Data</h1>\n";
print_r($recData); print_r($recData);
// Conversion // Conversion
echo "<h2>32 bits types</h2>"; echo "<h2>32 bits types</h2>\n";
// Chunk the data array to set of 4 bytes // Chunk the data array to set of 4 bytes
$values = array_chunk($recData, 4); $values = array_chunk($recData, 4);
// Get float from REAL interpretation // Get float from REAL interpretation
echo "<h3>REAL to Float</h3>"; echo "<h3>REAL to Float</h3>\n";
foreach($values as $bytes) foreach($values as $bytes)
echo PhpType::bytes2float($bytes) . "</br>"; echo PhpType::bytes2float($bytes) . "</br>";
// Get integer from DINT interpretation // Get integer from DINT interpretation
echo "<h3>DINT to integer </h3>"; echo "<h3>DINT to integer </h3>\n";
foreach($values as $bytes) foreach($values as $bytes)
echo PhpType::bytes2signedInt($bytes) . "</br>"; echo PhpType::bytes2signedInt($bytes) . "</br>";
// Get integer of float from DINT interpretation // Get integer of float from DINT interpretation
echo "<h3>DWORD to integer (or float) </h3>"; echo "<h3>DWORD to integer (or float) </h3>\n";
foreach($values as $bytes) foreach($values as $bytes)
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; echo PhpType::bytes2unsignedInt($bytes) . "</br>";
echo "<h2>16 bit types</h2>"; echo "<h2>16 bit types</h2>\n";
// Chunk the data array to set of 4 bytes // Chunk the data array to set of 4 bytes
$values = array_chunk($recData, 2); $values = array_chunk($recData, 2);
// Get signed integer from INT interpretation // Get signed integer from INT interpretation
echo "<h3>INT to integer </h3>"; echo "<h3>INT to integer </h3>\n";
foreach($values as $bytes) foreach($values as $bytes)
echo PhpType::bytes2signedInt($bytes) . "</br>"; echo PhpType::bytes2signedInt($bytes) . "</br>";
// Get unsigned integer from WORD interpretation // Get unsigned integer from WORD interpretation
echo "<h3>WORD to integer </h3>"; echo "<h3>WORD to integer </h3>\n";
foreach($values as $bytes) foreach($values as $bytes)
echo PhpType::bytes2unsignedInt($bytes) . "</br>"; echo PhpType::bytes2unsignedInt($bytes) . "</br>";
// Get string from STRING interpretation
echo "<h3>STRING to string </h3>\n";
echo PhpType::bytes2string($recData) . "</br>";
?> ?>
+25
View File
@@ -0,0 +1,25 @@
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object
$modbus = new ModbusMasterUdp("192.192.15.51");
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>";
+26
View File
@@ -0,0 +1,26 @@
<?php
require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object
$modbus = new ModbusMasterUdp("192.192.15.51");
// 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;
+14 -9
View File
@@ -1,21 +1,26 @@
<?php <?php
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object // Create Modbus object
$modbus = new ModbusMasterUdp("192.168.1.99"); $modbus = new ModbusMasterUdp("192.192.15.51");
// Data to be writen // Data to be writen
$data = array(1000, 2000, 3.0); $data = array(10, -1000, 2000, 3.0);
$dataTypes = array("INT", "DINT", "REAL"); $dataTypes = array("WORD", "INT", "DINT", "REAL");
// FC16 try {
if(!$modbus->writeMultipleRegister(0, 12288, $data, $dataTypes)) { // FC16
// Print error information if any $modbus->writeMultipleRegister(0, 12288, $data, $dataTypes);
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; }
catch (Exception $e) {
// Print error information if any
echo $modbus;
echo $e;
exit;
} }
// Print status information // Print status information
echo "</br>Status:</br>" . $modbus->status . "</br>"; echo $modbus;
?> ?>
+17 -12
View File
@@ -1,26 +1,31 @@
<?php <?php
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object // Create Modbus object
$modbus = new ModbusMasterUdp("192.168.1.99"); $modbus = new ModbusMasterUdp("192.192.15.51");
// Data to be writen // Data to be writen
$data = array(1000, 2000, 3.0); $data = array(10, -1000, 2000, 3.0);
$dataTypes = array("INT", "DINT", "REAL"); $dataTypes = array("WORD", "INT", "DINT", "REAL");
// FC23 try {
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes); // FC23
$recData = $modbus->readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
if(!$recData) { }
// Print error information if any catch (Exception $e) {
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; // Print error information if any
echo $modbus;
echo $e;
exit;
} }
// Print status information // Print status information
echo "</br>Status:</br>" . $modbus->status . "</br>"; echo "</br>Status:</br>" . $modbus;
// Print read data // Print read data
echo "</br>Data:</br>"; print_r($recData); echo "</br>"; echo "</br>Data:</br>";
print_r($recData);
echo "</br>";
?> ?>
+15 -10
View File
@@ -1,21 +1,26 @@
<?php <?php
require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMasterUdp.php';
// Create Modbus object // Create Modbus object
$modbus = new ModbusMasterUdp("192.168.1.99"); $modbus = new ModbusMasterUdp("192.192.15.51");
// FC 3 try {
$recData = $modbus->readMultipleRegisters(0, 12288, 6); // FC 3
$recData = $modbus->readMultipleRegisters(0, 12288, 6);
if(!$recData) { }
// Print error information if any catch (Exception $e) {
echo "</br>Error:</br>" . $modbus->errstr . "</br>"; // Print error information if any
echo $modbus;
echo $e;
exit;
} }
// Print status information // Print status information
echo "</br>Status:</br>" . $modbus->status . "</br>"; echo "</br>Status:</br>" . $modbus;
// Print read data // Print read data
echo "</br>Data:</br>"; print_r($recData); echo "</br>"; echo "</br>Data:</br>";
print_r($recData);
echo "</br>";
?> ?>
+12
View File
@@ -0,0 +1,12 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>PhpModbus</title>
</head>
<body>
<a href="examples/">Examples</a><br>
<a href="tests/">Tests</a><br>
<a href="tests/">Tutorials</a><br>
</body>
</html>
+1 -1
View File
@@ -1,7 +1,7 @@
The Phpmodbus License, Version 1 The Phpmodbus License, Version 1
============================ ============================
Copyright (c) 2004, 2008 Jan Krakora, Wago (http://www.wago.com) Copyright (c) 2004, 2011 Jan Krakora, Wago (http://www.wago.com)
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, Wago (the "Author")
-19
View File
@@ -1,19 +0,0 @@
Copyright (c) 2004, 2009 Jan Krakora, Wago (http://www.wago.com)
All rights reserved.
Phpmodbus library
####################
Phpmodbus for PHP is a small and easy-to-use Modbus UDP master library. For more
see project at http://phpmodbus.googlecode.com
Release notes
===============
0.1 -> 0.2.r20
---------------
+ Added new class for conversion from received bytes to PHP data types (PhpType class)
+ Added new data conversion using PhpType example
+ Added new alias methods fc3, fc16 and fc23 (ModbusMasterUdp class)
* Fixed problems with the endianess when data written (IecType class)
* Improved commentaries for documentation
@@ -0,0 +1 @@
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>
@@ -0,0 +1 @@
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>
@@ -0,0 +1 @@
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>
@@ -0,0 +1,12 @@
Endianing off <hr>
0 --> Packet: 0000_0000_<br>
1 --> Packet: 0000_3f80_<br>
-2 --> Packet: 0000_c000_<br>
0.33333333333333 --> Packet: aaab_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.33333333333333 --> Packet: 3eaa_aaab_<br>
25 --> Packet: 41c8_0000_<br>
+12 -1
View File
@@ -1 +1,12 @@
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> 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>
+5 -5
View File
@@ -25,26 +25,26 @@ function printPacket($packet){
if($i % 2) if($i % 2)
$str .= "_"; $str .= "_";
} }
$str .= "</br>"; $str .= "<br>\n";
return $str; return $str;
} }
echo "Endianing off <hr>"; echo "Endianing off <hr>\n";
// Print mixed values // Print mixed values
for($i=0;$i<count($data);$i++) { for($i=0;$i<count($data);$i++) {
echo $data[$i] . " --> "; echo $data[$i] . " --> ";
$v = IecType::iecREAL($data[$i], 0); $v = IecType::iecREAL($data[$i], 0);
echo printPacket($v); echo printPacket($v);
"<br>"; "<br>\n";
} }
echo "Endianing on <hr>"; echo "Endianing on <hr>\n";
// Print mixed values // Print mixed values
for($i=0;$i<count($data);$i++) { for($i=0;$i<count($data);$i++) {
echo $data[$i] . " --> "; echo $data[$i] . " --> ";
$v = IecType::iecREAL($data[$i], 1); $v = IecType::iecREAL($data[$i], 1);
echo printPacket($v); echo printPacket($v);
"<br>"; "<br>\n";
} }
?> ?>
@@ -0,0 +1,2 @@
Fatal error: Call to undefined method ModbusMasterUdp::writeMultipleCoils() in D:\Projects\20081010_phpmodbus\src\trunk\tests\ModbusMasterUdp\test.fc15fc1.php on line 11
@@ -0,0 +1,72 @@
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
)
@@ -0,0 +1,11 @@
Warning: socket_bind(): unable to bind address [0]: Požadovaná adresa není v tomto kontextu platná.
in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php on line 78
Fatal error: Uncaught exception 'Exception' with message 'socket_bind() failed.</br>Reason: ()Požadovaná adresa není v tomto kontextu platná.
' in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php:80
Stack trace:
#0 D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php(290): ModbusMasterUdp->connect()
#1 D:\Projects\20081010_phpmodbus\src\trunk\tests\ModbusMasterUdp\test.fc16fc3bind.php(13): ModbusMasterUdp->writeMultipleRegister(0, 12288, Array, Array)
#2 {main}
thrown in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php on line 80
@@ -0,0 +1 @@
writeMultipleRegister (FC26): DONE
@@ -0,0 +1,11 @@
Warning: socket_bind(): unable to bind address [0]: Požadovaná adresa není v tomto kontextu platná.
in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php on line 78
Fatal error: Uncaught exception 'Exception' with message 'socket_bind() failed.</br>Reason: ()Požadovaná adresa není v tomto kontextu platná.
' in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php:80
Stack trace:
#0 D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php(410): ModbusMasterUdp->connect()
#1 D:\Projects\20081010_phpmodbus\src\trunk\tests\ModbusMasterUdp\test.fc26bind.php(14): ModbusMasterUdp->readWriteRegisters(0, 12288, 6, 12288, Array, Array)
#2 {main}
thrown in D:\Projects\20081010_phpmodbus\src\trunk\Phpmodbus\ModbusMasterUdp.php on line 80
@@ -0,0 +1,72 @@
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
)
@@ -0,0 +1 @@
writeMultipleRegister (FC26): DONE
+22
View File
@@ -0,0 +1,22 @@
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
require_once dirname(__FILE__) . '/../config.php';
// Create Modbus object
$modbus = new ModbusMasterUdp($testip);
// 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);
@@ -0,0 +1,45 @@
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
require_once dirname(__FILE__) . '/../config.php';
// Create Modbus object
$modbus = new ModbusMasterUdp($testip);
$modbus->client = "192.192.15.133";
// 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);
?>
+25
View File
@@ -0,0 +1,25 @@
<?php
require_once dirname(__FILE__) . '/../../Phpmodbus/ModbusMasterUdp.php';
require_once dirname(__FILE__) . '/../config.php';
// Create Modbus object
$modbus = new ModbusMasterUdp($testip);
$modbus->client = "192.192.15.133";
// 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";
?>
@@ -0,0 +1 @@
32098<br>0<br>0<br>-1<br>-25000<br>25000<br>
@@ -0,0 +1 @@
1000<br>2000<br>1.25<br>
@@ -0,0 +1 @@
-1<br>0<br>1<br>-2147483648<br>2147483647<br>
@@ -0,0 +1 @@
eHll oowlr!da<br>Hello world!<br>
@@ -0,0 +1,6 @@
float(4294967295)
<br>int(0)
<br>int(1)
<br>float(2147483648)
<br>int(2147483647)
<br>
@@ -0,0 +1 @@
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>
@@ -0,0 +1 @@
Exception 'Data are not numeric'
@@ -0,0 +1 @@
eHll oowlr!da<br>Hello world!<br>
@@ -0,0 +1 @@
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>
@@ -0,0 +1 @@
Exception 'Data are not numeric'
+28
View File
@@ -0,0 +1,28 @@
<?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>";
?>
+44
View File
@@ -0,0 +1,44 @@
<?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>";
}
?>
@@ -0,0 +1,22 @@
<?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'";
}
?>
+4 -4
View File
@@ -1,5 +1,5 @@
set php=C:\Programme\xampplite\php\php.exe 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 rem set php=C:\PHP\versions\php-5.2.6-Win32\php.exe -d auto_prepend_file=C:\PHP\locale.php
set diff="C:\Program Files\Octave\msys\bin\diff.exe" set diff="diff.exe"
set testUri=http://localHost/nette/_trunk/tests rem set testUri=http://localHost/nette/_trunk/tests
set wget=wget.exe rem set wget=wget.exe
+1 -1
View File
@@ -1,3 +1,3 @@
<?php <?php
$testip = "192.168.1.15"; $testip = "192.192.15.51";
?> ?>
+46 -11
View File
@@ -21,7 +21,9 @@
<para> <para>
The library implements: The library implements:
<itemizedlist> <itemizedlist>
<listitem><para>FC 1: read multiple coils</para></listitem>
<listitem><para>FC 3: read multiple registers</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 16: write multiple registers</para></listitem>
<listitem><para>FC 23: read write registers</para></listitem> <listitem><para>FC 23: read write registers</para></listitem>
</itemizedlist> </itemizedlist>
@@ -44,25 +46,40 @@
Copy the Phpmodbus library to your PHP project folder. Copy the Phpmodbus library to your PHP project folder.
</para> </para>
<para> <para>
Create a PHP script and assign the library using Create a PHP script and assign the library using require_once() command
<programlisting role="ini"> <programlisting role="c">
<![CDATA[ require_once dirname(__FILE__) . './Phpmodbus/ModbusMasterUdp.php'; ]]> <![CDATA[require_once dirname(__FILE__) . '/Phpmodbus/ModbusMasterUdp.php'; ]]>
</programlisting> </programlisting>
</para> </para>
<para> <para>
To create the Modbus master object, that will communicate with a Modbus slave To create the Modbus master object communicating to the 192.168.1.1 modbus slave
at IP address 192.168.1.1, write at IP address 192.168.1.1 write
<programlisting role="ini"> <programlisting role="c" linenumbering="numbered" startinglinenumber="2">
<![CDATA[ $modbus = new ModbusMasterUdp("192.168.1.1"); ]]> <![CDATA[ $modbus = new ModbusMasterUdp("192.168.1.1"); ]]>
</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> </programlisting>
</para> </para>
<para> <para>
To read 5 words (10 bytes) located at the memory beginning at address = 12288 of To process the function byte stream, use conversion to PHP types in
the devide with Modbus ID=0 use
<programlisting role="ini">
<![CDATA[ $recData = $modbus->readMultipleRegisters(0, 12288, 5); ]]>
</programlisting>
</para> </para>
<programlisting role="c" linenumbering="numbered" startinglinenumber="15">
<![CDATA[echo PhpType::bytes2string($recData); ]]>
</programlisting>
<para> <para>
For other examples see sections bellow. For other examples see sections bellow.
</para> </para>
@@ -72,6 +89,15 @@
<para> <para>
This section presents library features and examples This section presents library features and examples
</para> </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}"> <refsect2 id="{@id example_fc3}">
<title>FC3 - read mutliple registers</title> <title>FC3 - read mutliple registers</title>
<para> <para>
@@ -81,6 +107,15 @@
{@example example_fc3.php} {@example example_fc3.php}
</para> </para>
</refsect2> </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}"> <refsect2 id="{@id example_fc16}">
<title>FC16 - write mutliple registers</title> <title>FC16 - write mutliple registers</title>
<para> <para>