diff --git a/Phpmodbus/IecType.php b/Phpmodbus/IecType.php
index cacca56..9beb696 100644
--- a/Phpmodbus/IecType.php
+++ b/Phpmodbus/IecType.php
@@ -1,13 +1,13 @@
> 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
- *
- * 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));
- }
-
+ /**
+ * 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));
+ }
+
}
-
+
?>
\ No newline at end of file
diff --git a/Phpmodbus/ModbusMasterUdp.php b/Phpmodbus/ModbusMasterUdp.php
index 3681319..8fb6635 100644
--- a/Phpmodbus/ModbusMasterUdp.php
+++ b/Phpmodbus/ModbusMasterUdp.php
@@ -1,12 +1,12 @@
host = $host;
}
+ /**
+ * __toString
+ *
+ * Magic method
+ */
+ function __toString() {
+ return "
" . $this->status . "
";
+ }
+
/**
* connect
*
@@ -66,11 +74,10 @@ class ModbusMasterUdp {
// connect
$result = @socket_connect($this->sock, $this->host, $this->port);
if ($result === false) {
- $this->errstr .= "socket_connect() failed.Reason: ($result) " .
- socket_strerror(socket_last_error($this->sock));
- return false;
+ throw new Exception("socket_connect() failed.Reason: ($result)".
+ socket_strerror(socket_last_error($this->sock)));
} else {
- $this->status .= "Connected";
+ $this->status .= "Connected\n";
return true;
}
}
@@ -82,7 +89,7 @@ class ModbusMasterUdp {
*/
private function disconnect(){
socket_close($this->sock);
- $this->status .= "Disconnected";
+ $this->status .= "Disconnected\n";
}
/**
@@ -94,7 +101,7 @@ class ModbusMasterUdp {
*/
private function send($packet){
socket_write($this->sock, $packet, strlen($packet));
- $this->status .= "Send";
+ $this->status .= "Send\n";
}
/**
@@ -116,19 +123,18 @@ class ModbusMasterUdp {
$exceptsocks,
0,
300000) !== FALSE) {
- $this->status .= "Wait received data";
+ $this->status .= "Wait data ... \n";
if (in_array($this->sock, $readsocks)) {
while (@socket_recv($this->sock, $rec, 2000, 0)) {
- $this->status .= "Received";
+ $this->status .= "Data received\n";
return $rec;
}
$lastAccess = time();
} else {
- if (time()-$lastAccess >= $this->timeout_sec) {
- $this->errstr .= "Watchdog time expired [ " .
+ if (time()-$lastAccess >= $this->timeout_sec) {
+ throw new Exception( "Watchdog time expired [ " .
$this->timeout_sec . " sec]!!! Connection to " .
- $this->host . " is not established.";
- return false;
+ $this->host . " is not established.");
}
}
$readsocks[] = $this->sock;
@@ -145,12 +151,9 @@ class ModbusMasterUdp {
*/
private function responseCode($packet){
if(($packet[7] & 0x80) > 0) {
- $this->errstr .= "Modbus response error code:" . ord($packet[8]);
- return false;
- }
- else
- {
- $this->status .= "Modbus response error code: NOERROR";
+ throw new Exception("Modbus response error code:" . ord($packet[8]));
+ } else {
+ $this->status .= "Modbus response error code: NOERROR\n";
return true;
}
}
@@ -171,27 +174,21 @@ class ModbusMasterUdp {
* @return false|Array Success flag or array of received data.
*/
function readMultipleRegisters($unitId, $reference, $quantity){
- $this->errstr = "";
- $this->status = "readMultipleRegisters: START";
+ $this->status .= "readMultipleRegisters: START\n";
// connect
- if(!$this->connect())
- return false;
+ $this->connect();
// send FC 3
$packet = $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet);
$this->send($packet);
// receive response
$rpacket = $this->rec();
- if(!$rpacket)
- return false;
- $this->status .= $this->printPacket($rpacket);
- // parse packet
+ $this->status .= $this->printPacket($rpacket);
+ // parse packet
$receivedData = $this->readMultipleRegistersParser($rpacket);
- if(!$receivedData)
- return false;
// disconnect
$this->disconnect();
- $this->status .= "readMultipleRegisters: DONE";
+ $this->status .= "readMultipleRegisters: DONE\n";
// return
return $receivedData;
}
@@ -222,6 +219,8 @@ class ModbusMasterUdp {
*/
private function readMultipleRegistersPacketBuilder($unitId, $reference, $quantity){
$dataLen = 0;
+ // build data section
+ $buffer1 = "";
// build body
$buffer2 = "";
$buffer2 .= iecType::iecBYTE(3); // FC 3 = 3(0x03)
@@ -247,11 +246,10 @@ class ModbusMasterUdp {
* @param string $packet
* @return array
*/
- private function readMultipleRegistersParser($packet){
+ private function readMultipleRegistersParser($packet){
$data = array();
- // if not exception
- if(!$this->responseCode($packet))
- return false;
+ // check Response code
+ $this->responseCode($packet);
// get data
for($i=0;$ierrstr = "";
- $this->status = "writeMultipleRegister: START";
+ $this->status .= "writeMultipleRegister: START\n";
// connect
- if(!$this->connect())
- return false;
+ $this->connect();
// send FC16
$packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes);
$this->status .= $this->printPacket($packet);
$this->send($packet);
// receive response
$rpacket = $this->rec();
- if(!$rpacket)
- return false;
$this->status .= $this->printPacket($rpacket);
// parse packet
- if(!$this->writeMultipleRegisterParser($rpacket))
- return false;
+ $this->writeMultipleRegisterParser($rpacket);
// disconnect
$this->disconnect();
- $this->status .= "writeMultipleRegister: DONE";
+ $this->status .= "writeMultipleRegister: DONE\n";
return true;
}
@@ -376,8 +369,7 @@ class ModbusMasterUdp {
* @return bool
*/
private function writeMultipleRegisterParser($packet){
- if(!$this->responseCode($rpacket))
- return false;
+ $this->responseCode($rpacket);
return true;
}
@@ -400,27 +392,21 @@ class ModbusMasterUdp {
* @return false|Array Success flag or array of data.
*/
function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes){
- $this->errstr = "";
- $this->status = "readWriteRegisters: START";
+ $this->status .= "readWriteRegisters: START\n";
// connect
- if(!$this->connect())
- return false;
+ $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();
- if(!$rpacket)
- return false;
$this->status .= $this->printPacket($rpacket);
// parse packet
- $receivedData = $this->readWriteRegistersParser($rpacket);
- if(!$receivedData)
- return false;
+ $receivedData = $this->readWriteRegistersParser($rpacket);
// disconnect
$this->disconnect();
- $this->status .= "writeMultipleRegister: DONE";
+ $this->status .= "writeMultipleRegister: DONE\n";
// return
return $receivedData;
}
@@ -538,7 +524,7 @@ class ModbusMasterUdp {
/**
* printPacket
*
- * Print whole packet in the hex form
+ * Print a packet in the hex form
*
* @param string $packet
* @return string
@@ -549,7 +535,7 @@ class ModbusMasterUdp {
for($i=0;$ibyte2hex(ord($packet[$i]));
}
- $str .= "";
+ $str .= "\n";
return $str;
}
}
diff --git a/Phpmodbus/PhpType.php b/Phpmodbus/PhpType.php
index bdf8eea..97670a9 100644
--- a/Phpmodbus/PhpType.php
+++ b/Phpmodbus/PhpType.php
@@ -1,219 +1,249 @@
-
+ 0)){
- $int = 0xFFFF8000 | $int;
+
+ /**
+ * 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);
}
- // 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);
+
+ /**
+ * 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;$i4 ||
+ 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;
}
- if (!is_numeric($data[2])) $data[2] = 0;
- if (!is_numeric($data[3])) $data[3] = 0;
-
- 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;
- }
+ /**
+ * 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;
+ }
}
?>
\ No newline at end of file
diff --git a/examples/example_750841_Mmemory.php b/examples/example_750841_Mmemory.php
index c9a418e..d6909bd 100644
--- a/examples/example_750841_Mmemory.php
+++ b/examples/example_750841_Mmemory.php
@@ -1,73 +1,69 @@
readMultipleRegisters($moduleId, $reference, $quantity);
-
+try {
+ // FC 3
+ $moduleId = 0;
+ $reference = 12288;
+ $mw0address = 12288;
+ $quantity = 6;
+ $recData = $modbus->readMultipleRegisters($moduleId, $reference, $quantity);
+ print_r($recData);
+}
+catch (Exception $e) {
+ echo $modbus;
+ echo $e;
+ exit;
+}
+
?>
-
-
-
- WAGO 750-841 M-memory dump
-
-
- Dump of M-memory from WAGO 750-84x series coupler.
-
- - PLC: 750-84x series
- - IP: =$ip?>
- - Modbus module ID: =$moduleId?>
- - Modbus memory reference: =$reference?>
- - Modbus memory quantity: =$quantity?>
-
-
- M-memory dump
-
- Error:" . $modbus->errstr . "";
- }
- else
- {
- ?>
-
-
-
- Modbus address |
- MWx |
- value |
-
-
-
- =$i+$reference?> |
- MW=($i + $reference - $mw0address)/2?> |
- =($recData[$i] << 8)+ $recData[$i+1]?> |
-
-
-
-
-
-
- Modbus class status
- status;
- ?>
-
-
+
+
+
+ WAGO 750-841 M-memory dump
+
+
+ Dump of M-memory from WAGO 750-84x series coupler.
+
+ - PLC: 750-84x series
+ - IP:
+ - Modbus module ID:
+ - Modbus memory reference:
+ - Modbus memory quantity:
+
+
+ M-memory dump
+
+
+
+ Modbus address |
+ MWx |
+ value |
+
+
+
+ |
+ MW |
+ |
+
+
+
+
+ Modbus class status
+
+
+
diff --git a/examples/example_datatype.php b/examples/example_datatype.php
index 996c518..0a7de4d 100644
--- a/examples/example_datatype.php
+++ b/examples/example_datatype.php
@@ -1,55 +1,61 @@
readMultipleRegisters(0, 12288, 10);
-
-if(!$recData) {
- // Print error information if any
- echo "Error:" . $modbus->errstr . "";
- exit;
+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 "Received Data
";
+echo "Received Data
\n";
print_r($recData);
// Conversion
-echo "32 bits types
";
+echo "32 bits types
\n";
// Chunk the data array to set of 4 bytes
$values = array_chunk($recData, 4);
// Get float from REAL interpretation
-echo "REAL to Float
";
+echo "REAL to Float
\n";
foreach($values as $bytes)
- echo PhpType::bytes2float($bytes) . "";
+ echo PhpType::bytes2float($bytes) . "";
// Get integer from DINT interpretation
-echo "DINT to integer
";
+echo "DINT to integer
\n";
foreach($values as $bytes)
- echo PhpType::bytes2signedInt($bytes) . "";
+ echo PhpType::bytes2signedInt($bytes) . "";
// Get integer of float from DINT interpretation
-echo "DWORD to integer (or float)
";
+echo "DWORD to integer (or float)
\n";
foreach($values as $bytes)
- echo PhpType::bytes2unsignedInt($bytes) . "";
+ echo PhpType::bytes2unsignedInt($bytes) . "";
-echo "16 bit types
";
+echo "16 bit types
\n";
// Chunk the data array to set of 4 bytes
$values = array_chunk($recData, 2);
// Get signed integer from INT interpretation
-echo "INT to integer
";
+echo "INT to integer
\n";
foreach($values as $bytes)
- echo PhpType::bytes2signedInt($bytes) . "";
+ echo PhpType::bytes2signedInt($bytes) . "";
// Get unsigned integer from WORD interpretation
-echo "WORD to integer
";
+echo "WORD to integer
\n";
foreach($values as $bytes)
- echo PhpType::bytes2unsignedInt($bytes) . "";
+ echo PhpType::bytes2unsignedInt($bytes) . "";
+
+// Get string from STRING interpretation
+echo "STRING to string
\n";
+echo PhpType::bytes2string($recData) . "";
?>
\ No newline at end of file
diff --git a/examples/example_fc16.php b/examples/example_fc16.php
index 3d74f0c..ddd3846 100644
--- a/examples/example_fc16.php
+++ b/examples/example_fc16.php
@@ -1,21 +1,26 @@
writeMultipleRegister(0, 12288, $data, $dataTypes)) {
- // Print error information if any
- echo "Error:" . $modbus->errstr . "";
+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 "Status:" . $modbus->status . "";
+echo $modbus;
?>
\ No newline at end of file
diff --git a/examples/example_fc23.php b/examples/example_fc23.php
index 0c37992..98cea54 100644
--- a/examples/example_fc23.php
+++ b/examples/example_fc23.php
@@ -1,26 +1,31 @@
readWriteRegisters(0, 12288, 6, 12288, $data, $dataTypes);
-
-if(!$recData) {
- // Print error information if any
- echo "Error:" . $modbus->errstr . "";
+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 "Status:" . $modbus->status . "";
+echo "Status:" . $modbus;
// Print read data
-echo "Data:"; print_r($recData); echo "";
+echo "Data:";
+print_r($recData);
+echo "";
?>
\ No newline at end of file
diff --git a/examples/example_fc3.php b/examples/example_fc3.php
index 568ded1..3b64b65 100644
--- a/examples/example_fc3.php
+++ b/examples/example_fc3.php
@@ -1,21 +1,26 @@
readMultipleRegisters(0, 12288, 6);
-
-if(!$recData) {
- // Print error information if any
- echo "Error:" . $modbus->errstr . "";
+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 "Status:" . $modbus->status . "";
+echo "Status:" . $modbus;
// Print read data
-echo "Data:"; print_r($recData); echo "";
+echo "Data:";
+print_r($recData);
+echo "";
?>
\ No newline at end of file
diff --git a/tests/PhpType/ref/test.bytes2string.php.html b/tests/PhpType/ref/test.bytes2string.php.html
new file mode 100644
index 0000000..31bbf09
--- /dev/null
+++ b/tests/PhpType/ref/test.bytes2string.php.html
@@ -0,0 +1 @@
+eHll oowlr!da
Hello world!
\ No newline at end of file
diff --git a/tests/PhpType/ref/test.strangearray.size.php.html b/tests/PhpType/ref/test.strangearray.size.php.html
new file mode 100644
index 0000000..d0e619d
--- /dev/null
+++ b/tests/PhpType/ref/test.strangearray.size.php.html
@@ -0,0 +1 @@
+Exception 'Data are not in array 2 or 4 bytes'
25602
Exception 'Data are not in array 2 or 4 bytes'
25602
Exception 'Data are not in array 2 or 4 bytes'
\ No newline at end of file
diff --git a/tests/PhpType/ref/test.strangearray.textarray.php.html b/tests/PhpType/ref/test.strangearray.textarray.php.html
new file mode 100644
index 0000000..734d099
--- /dev/null
+++ b/tests/PhpType/ref/test.strangearray.textarray.php.html
@@ -0,0 +1 @@
+Exception 'Data are not numeric'
\ No newline at end of file
diff --git a/tests/PhpType/test.bytes2string.php b/tests/PhpType/test.bytes2string.php
new file mode 100644
index 0000000..547502b
--- /dev/null
+++ b/tests/PhpType/test.bytes2string.php
@@ -0,0 +1,28 @@
+";
+echo PhpType::bytes2string($data, true) . "
";
+
+?>
diff --git a/tests/PhpType/test.strangearray.size.php b/tests/PhpType/test.strangearray.size.php
new file mode 100644
index 0000000..ba817e2
--- /dev/null
+++ b/tests/PhpType/test.strangearray.size.php
@@ -0,0 +1,44 @@
+ 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)) . "
";
+} catch(Exception $e) {
+ echo "Exception 'Data are not in array 2 or 4 bytes'". "
";
+}
+try {
+ echo PhpType::bytes2unsignedInt(array_slice($data, 0, 2)). "
";
+} catch(Exception $e) {
+ echo "Exception 'Data are not in array 2 or 4 bytes'". "
";
+}
+try {
+ echo PhpType::bytes2unsignedInt(array_slice($data, 0, 3)). "
";
+} catch(Exception $e) {
+ echo "Exception 'Data are not in array 2 or 4 bytes'". "
";
+}
+try {
+ echo PhpType::bytes2unsignedInt(array_slice($data, 0, 4)). "
";
+} catch(Exception $e) {
+ echo "Exception 'Data are not in array 2 or 4 bytes'". "
";
+}
+try {
+ echo PhpType::bytes2unsignedInt(array_slice($data, 0, 5)). "
";
+} catch(Exception $e) {
+ echo "Exception 'Data are not in array 2 or 4 bytes'". "
";
+}
+?>
\ No newline at end of file
diff --git a/tests/PhpType/test.strangearray.textarray.php b/tests/PhpType/test.strangearray.textarray.php
new file mode 100644
index 0000000..10306a0
--- /dev/null
+++ b/tests/PhpType/test.strangearray.textarray.php
@@ -0,0 +1,22 @@
+ 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'";
+}
+?>
diff --git a/tests/config.bat b/tests/config.bat
index d62ddfe..5560180 100644
--- a/tests/config.bat
+++ b/tests/config.bat
@@ -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
-set diff="C:\Program Files\Octave\msys\bin\diff.exe"
-set testUri=http://localHost/nette/_trunk/tests
-set wget=wget.exe
+set diff="diff.exe"
+rem set testUri=http://localHost/nette/_trunk/tests
+rem set wget=wget.exe
diff --git a/tests/config.php b/tests/config.php
index f94dfc6..ba00e84 100644
--- a/tests/config.php
+++ b/tests/config.php
@@ -1,3 +1,3 @@
\ No newline at end of file
diff --git a/tutorials/Phpmodbus/Phpmodbus.pkg b/tutorials/Phpmodbus/Phpmodbus.pkg
index 5ece7f3..91b4e47 100644
--- a/tutorials/Phpmodbus/Phpmodbus.pkg
+++ b/tutorials/Phpmodbus/Phpmodbus.pkg
@@ -46,7 +46,7 @@
Create a PHP script and assign the library using
-
+