|
|
@ -1,6 +1,7 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
namespace PHPModbus; |
|
|
|
namespace PHPModbus; |
|
|
|
|
|
|
|
use Exception; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora |
|
|
|
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora |
|
|
@ -28,8 +29,8 @@ namespace PHPModbus; |
|
|
|
* @package Phpmodbus |
|
|
|
* @package Phpmodbus |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class PhpType { |
|
|
|
class PhpType |
|
|
|
|
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* bytes2float |
|
|
|
* bytes2float |
|
|
|
* |
|
|
|
* |
|
|
@ -37,19 +38,17 @@ class PhpType { |
|
|
|
* 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 $bigEndian |
|
|
|
* @return float |
|
|
|
* @return float |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function bytes2float($values, $endianness = 0) { |
|
|
|
public static function bytes2float($values, $bigEndian = 0) |
|
|
|
$data = array(); |
|
|
|
{ |
|
|
|
$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, $bigEndian); |
|
|
|
// Convert the real value to float |
|
|
|
// Convert the real value to float |
|
|
|
return (float) self::real2float($real); |
|
|
|
return (float)self::real2float($real); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -59,22 +58,23 @@ class PhpType { |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
* |
|
|
|
* |
|
|
|
* @param array $values |
|
|
|
* @param array $values |
|
|
|
* @param bool $endianness |
|
|
|
* @param bool $bigEndian |
|
|
|
* @return int |
|
|
|
* @return int |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function bytes2signedInt($values, $endianness = 0) { |
|
|
|
public static function bytes2signedInt($values, $bigEndian = 0) |
|
|
|
|
|
|
|
{ |
|
|
|
$data = array(); |
|
|
|
$data = array(); |
|
|
|
$int = 0; |
|
|
|
$int = 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 |
|
|
|
$int = self::combineBytes($data, $endianness); |
|
|
|
$int = self::combineBytes($data, $bigEndian); |
|
|
|
// In the case of signed 2 byte value convert it to 4 byte one |
|
|
|
// In the case of signed 2 byte value convert it to 4 byte one |
|
|
|
if ((count($values) == 2) && ((0x8000 & $int) > 0)) { |
|
|
|
if ((count($values) == 2) && ((0x8000 & $int) > 0)) { |
|
|
|
$int = 0xFFFF8000 | $int; |
|
|
|
$int = 0xFFFF8000 | $int; |
|
|
|
} |
|
|
|
} |
|
|
|
// Convert the value |
|
|
|
// Convert the value |
|
|
|
return (int) self::dword2signedInt($int); |
|
|
|
return (int)self::dword2signedInt($int); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -84,16 +84,17 @@ class PhpType { |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
* |
|
|
|
* |
|
|
|
* @param array $values |
|
|
|
* @param array $values |
|
|
|
* @param bool $endianness |
|
|
|
* @param bool $bigEndian |
|
|
|
* @return int|float |
|
|
|
* @return int|float |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function bytes2unsignedInt($values, $endianness = 0) { |
|
|
|
public static function bytes2unsignedInt($values, $bigEndian = 0) |
|
|
|
|
|
|
|
{ |
|
|
|
$data = array(); |
|
|
|
$data = array(); |
|
|
|
$int = 0; |
|
|
|
$int = 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 |
|
|
|
$int = self::combineBytes($data, $endianness); |
|
|
|
$int = self::combineBytes($data, $bigEndian); |
|
|
|
// Convert the value |
|
|
|
// Convert the value |
|
|
|
return self::dword2unsignedInt($int); |
|
|
|
return self::dword2unsignedInt($int); |
|
|
|
} |
|
|
|
} |
|
|
@ -105,35 +106,39 @@ class PhpType { |
|
|
|
* the end of the string by 0x00 character as defined by string standards. |
|
|
|
* the end of the string by 0x00 character as defined by string standards. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param array $values |
|
|
|
* @param array $values |
|
|
|
* @param bool $endianness |
|
|
|
* @param bool $bigEndian |
|
|
|
* @return string |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function bytes2string($values, $endianness = 0) { |
|
|
|
public static function bytes2string($values, $bigEndian = 0) |
|
|
|
|
|
|
|
{ |
|
|
|
// Prepare string variable |
|
|
|
// Prepare string variable |
|
|
|
$str = ""; |
|
|
|
$str = ""; |
|
|
|
// Parse the received data word array |
|
|
|
// Parse the received data word array |
|
|
|
for($i=0;$i<count($values);$i+=2) { |
|
|
|
for ($i = 0; $i < count($values); $i += 2) { |
|
|
|
if ($endianness) { |
|
|
|
if ($bigEndian) { |
|
|
|
if($values[$i] != 0) |
|
|
|
if ($values[$i] != 0) { |
|
|
|
$str .= chr($values[$i]); |
|
|
|
$str .= chr($values[$i]); |
|
|
|
else |
|
|
|
} else { |
|
|
|
break; |
|
|
|
break; |
|
|
|
if($values[$i+1] != 0) |
|
|
|
} |
|
|
|
$str .= chr($values[$i+1]); |
|
|
|
if ($values[$i + 1] != 0) { |
|
|
|
else |
|
|
|
$str .= chr($values[$i + 1]); |
|
|
|
|
|
|
|
} else { |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
} else { |
|
|
|
if($values[$i+1] != 0) |
|
|
|
if ($values[$i + 1] != 0) { |
|
|
|
$str .= chr($values[$i+1]); |
|
|
|
$str .= chr($values[$i + 1]); |
|
|
|
else |
|
|
|
} else { |
|
|
|
break; |
|
|
|
break; |
|
|
|
if($values[$i] != 0) |
|
|
|
} |
|
|
|
|
|
|
|
if ($values[$i] != 0) { |
|
|
|
$str .= chr($values[$i]); |
|
|
|
$str .= chr($values[$i]); |
|
|
|
else |
|
|
|
} else { |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// return string |
|
|
|
// return string |
|
|
|
return $str; |
|
|
|
return $str; |
|
|
|
} |
|
|
|
} |
|
|
@ -144,13 +149,15 @@ class PhpType { |
|
|
|
* This function converts a value in IEC-1131 REAL single precision form to float. |
|
|
|
* 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 |
|
|
|
* 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://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}] |
|
|
|
* [{@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 |
|
|
|
* @param int $value in IEC REAL data type to be converted |
|
|
|
* @return float float value |
|
|
|
* @return float float value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function real2float($value) { |
|
|
|
private static function real2float($value) |
|
|
|
|
|
|
|
{ |
|
|
|
// get unsigned long |
|
|
|
// get unsigned long |
|
|
|
$ulong = pack("L", $value); |
|
|
|
$ulong = pack("L", $value); |
|
|
|
// set float |
|
|
|
// set float |
|
|
@ -167,9 +174,10 @@ class PhpType { |
|
|
|
* @param int $value |
|
|
|
* @param int $value |
|
|
|
* @return int |
|
|
|
* @return int |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function dword2signedInt($value) { |
|
|
|
private static function dword2signedInt($value) |
|
|
|
|
|
|
|
{ |
|
|
|
if ((0x80000000 & $value) != 0) { |
|
|
|
if ((0x80000000 & $value) != 0) { |
|
|
|
return -(0x7FFFFFFF & ~$value)-1; |
|
|
|
return -(0x7FFFFFFF & ~$value) - 1; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return (0x7FFFFFFF & $value); |
|
|
|
return (0x7FFFFFFF & $value); |
|
|
|
} |
|
|
|
} |
|
|
@ -183,11 +191,12 @@ class PhpType { |
|
|
|
* @param int $value |
|
|
|
* @param int $value |
|
|
|
* @return int|float |
|
|
|
* @return int|float |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function dword2unsignedInt($value) { |
|
|
|
private static function dword2unsignedInt($value) |
|
|
|
|
|
|
|
{ |
|
|
|
if ((0x80000000 & $value) != 0) { |
|
|
|
if ((0x80000000 & $value) != 0) { |
|
|
|
return ((float) (0x7FFFFFFF & $value)) + 2147483648; |
|
|
|
return ((float)(0x7FFFFFFF & $value)) + 2147483648; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return (int) (0x7FFFFFFF & $value); |
|
|
|
return (int)(0x7FFFFFFF & $value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -196,15 +205,18 @@ class PhpType { |
|
|
|
* |
|
|
|
* |
|
|
|
* Check if the data variable is array, and check if the values are numeric |
|
|
|
* Check if the data variable is array, and check if the values are numeric |
|
|
|
* |
|
|
|
* |
|
|
|
* @param int $data |
|
|
|
* @param int[] $data |
|
|
|
* @return int |
|
|
|
* @return int |
|
|
|
|
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function checkData($data) { |
|
|
|
private static function checkData($data) |
|
|
|
|
|
|
|
{ |
|
|
|
// Check the data |
|
|
|
// Check the data |
|
|
|
if (!is_array($data) || |
|
|
|
if (!is_array($data) || |
|
|
|
count($data)<2 || |
|
|
|
count($data) < 2 || |
|
|
|
count($data)>4 || |
|
|
|
count($data) > 4 || |
|
|
|
count($data)==3) { |
|
|
|
count($data) == 3 |
|
|
|
|
|
|
|
) { |
|
|
|
throw new Exception('The input data should be an array of 2 or 4 bytes.'); |
|
|
|
throw new Exception('The input data should be an array of 2 or 4 bytes.'); |
|
|
|
} |
|
|
|
} |
|
|
|
// Fill the rest of array by zeroes |
|
|
|
// Fill the rest of array by zeroes |
|
|
@ -216,7 +228,8 @@ class PhpType { |
|
|
|
if (!is_numeric($data[0]) || |
|
|
|
if (!is_numeric($data[0]) || |
|
|
|
!is_numeric($data[1]) || |
|
|
|
!is_numeric($data[1]) || |
|
|
|
!is_numeric($data[2]) || |
|
|
|
!is_numeric($data[2]) || |
|
|
|
!is_numeric($data[3])) { |
|
|
|
!is_numeric($data[3]) |
|
|
|
|
|
|
|
) { |
|
|
|
throw new Exception('Data are not numeric or the array keys are not indexed by 0,1,2 and 3'); |
|
|
|
throw new Exception('Data are not numeric or the array keys are not indexed by 0,1,2 and 3'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -229,24 +242,25 @@ class PhpType { |
|
|
|
* Combine bytes together |
|
|
|
* Combine bytes together |
|
|
|
* |
|
|
|
* |
|
|
|
* @param int $data |
|
|
|
* @param int $data |
|
|
|
* @param bool $endianness |
|
|
|
* @param bool $bigEndian |
|
|
|
* @return int |
|
|
|
* @return int |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function combineBytes($data, $endianness) { |
|
|
|
private static function combineBytes($data, $bigEndian) |
|
|
|
|
|
|
|
{ |
|
|
|
$value = 0; |
|
|
|
$value = 0; |
|
|
|
// Combine bytes |
|
|
|
// Combine bytes |
|
|
|
if ($endianness == 0) |
|
|
|
if ($bigEndian == 0) { |
|
|
|
$value = (($data[3] & 0xFF)<<16) | |
|
|
|
$value = (($data[3] & 0xFF) << 16) | |
|
|
|
(($data[2] & 0xFF)<<24) | |
|
|
|
(($data[2] & 0xFF) << 24) | |
|
|
|
(($data[1] & 0xFF)) | |
|
|
|
(($data[1] & 0xFF)) | |
|
|
|
(($data[0] & 0xFF)<<8); |
|
|
|
(($data[0] & 0xFF) << 8); |
|
|
|
else |
|
|
|
} else { |
|
|
|
$value = (($data[3] & 0xFF)<<24) | |
|
|
|
$value = (($data[3] & 0xFF) << 24) | |
|
|
|
(($data[2] & 0xFF)<<16) | |
|
|
|
(($data[2] & 0xFF) << 16) | |
|
|
|
(($data[1] & 0xFF)<<8) | |
|
|
|
(($data[1] & 0xFF) << 8) | |
|
|
|
(($data[0] & 0xFF)); |
|
|
|
(($data[0] & 0xFF)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $value; |
|
|
|
return $value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
?> |
|
|
|
|