|
|
@ -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 |
|
|
@ -8,12 +9,12 @@ namespace PHPModbus; |
|
|
|
* 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, 2012 Jan Krakora |
|
|
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora |
|
|
|
* @license PhpModbus license |
|
|
|
* @license PhpModbus license |
|
|
|
* @category Phpmodbus |
|
|
|
* @category Phpmodbus |
|
|
|
* @package Phpmodbus |
|
|
|
* @package Phpmodbus |
|
|
|
* @version $id$ |
|
|
|
* @version $id$ |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
@ -23,230 +24,243 @@ namespace PHPModbus; |
|
|
|
* The class includes set of methods that convert the received Modbus data |
|
|
|
* The class includes set of methods that convert the received Modbus data |
|
|
|
* (array of bytes) to the PHP data type, i.e. signed int, unsigned int and float. |
|
|
|
* (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, 2012 Jan Krakora |
|
|
|
* @copyright Copyright (c) 2004, 2012 Jan Krakora |
|
|
|
* @package Phpmodbus |
|
|
|
* @package Phpmodbus |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class PhpType { |
|
|
|
class PhpType |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* bytes2float |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* The function converts array of 4 bytes to float. The return value |
|
|
|
|
|
|
|
* depends on order of the input bytes (endianning). |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param array $values |
|
|
|
|
|
|
|
* @param bool $bigEndian |
|
|
|
|
|
|
|
* @return float |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function bytes2float($values, $bigEndian = 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Set the array to correct form |
|
|
|
|
|
|
|
$data = self::checkData($values); |
|
|
|
|
|
|
|
// Combine bytes |
|
|
|
|
|
|
|
$real = self::combineBytes($data, $bigEndian); |
|
|
|
|
|
|
|
// Convert the real value to float |
|
|
|
|
|
|
|
return (float)self::real2float($real); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* bytes2float |
|
|
|
* bytes2signedInt |
|
|
|
* |
|
|
|
* |
|
|
|
* The function converts array of 4 bytes to float. The return value |
|
|
|
* The function converts array of 2 or 4 bytes to signed integer. |
|
|
|
* 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 float |
|
|
|
* @return int |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function bytes2float($values, $endianness = 0) { |
|
|
|
public static function bytes2signedInt($values, $bigEndian = 0) |
|
|
|
$data = array(); |
|
|
|
{ |
|
|
|
$real = 0; |
|
|
|
$data = array(); |
|
|
|
|
|
|
|
$int = 0; |
|
|
|
|
|
|
|
// Set the array to correct form |
|
|
|
|
|
|
|
$data = self::checkData($values); |
|
|
|
|
|
|
|
// Combine bytes |
|
|
|
|
|
|
|
$int = self::combineBytes($data, $bigEndian); |
|
|
|
|
|
|
|
// In the case of signed 2 byte value convert it to 4 byte one |
|
|
|
|
|
|
|
if ((count($values) == 2) && ((0x8000 & $int) > 0)) { |
|
|
|
|
|
|
|
$int = 0xFFFF8000 | $int; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Convert the value |
|
|
|
|
|
|
|
return (int)self::dword2signedInt($int); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Set the array to correct form |
|
|
|
/** |
|
|
|
$data = self::checkData($values); |
|
|
|
* bytes2unsignedInt |
|
|
|
// Combine bytes |
|
|
|
* |
|
|
|
$real = self::combineBytes($data, $endianness); |
|
|
|
* The function converts array of 2 or 4 bytes to unsigned integer. |
|
|
|
// Convert the real value to float |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
return (float) self::real2float($real); |
|
|
|
* |
|
|
|
} |
|
|
|
* @param array $values |
|
|
|
|
|
|
|
* @param bool $bigEndian |
|
|
|
|
|
|
|
* @return int|float |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function bytes2unsignedInt($values, $bigEndian = 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$data = array(); |
|
|
|
|
|
|
|
$int = 0; |
|
|
|
|
|
|
|
// Set the array to correct form |
|
|
|
|
|
|
|
$data = self::checkData($values); |
|
|
|
|
|
|
|
// Combine bytes |
|
|
|
|
|
|
|
$int = self::combineBytes($data, $bigEndian); |
|
|
|
|
|
|
|
// Convert the value |
|
|
|
|
|
|
|
return self::dword2unsignedInt($int); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* bytes2signedInt |
|
|
|
* bytes2string |
|
|
|
* |
|
|
|
* |
|
|
|
* The function converts array of 2 or 4 bytes to signed integer. |
|
|
|
* The function converts an values array to the string. The function detects |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
* 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 int |
|
|
|
* @return string |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function bytes2signedInt($values, $endianness = 0) { |
|
|
|
public static function bytes2string($values, $bigEndian = 0) |
|
|
|
$data = array(); |
|
|
|
{ |
|
|
|
$int = 0; |
|
|
|
// Prepare string variable |
|
|
|
// Set the array to correct form |
|
|
|
$str = ""; |
|
|
|
$data = self::checkData($values); |
|
|
|
// Parse the received data word array |
|
|
|
// Combine bytes |
|
|
|
for ($i = 0; $i < count($values); $i += 2) { |
|
|
|
$int = self::combineBytes($data, $endianness); |
|
|
|
if ($bigEndian) { |
|
|
|
// In the case of signed 2 byte value convert it to 4 byte one |
|
|
|
if ($values[$i] != 0) { |
|
|
|
if ((count($values) == 2) && ((0x8000 & $int) > 0)) { |
|
|
|
$str .= chr($values[$i]); |
|
|
|
$int = 0xFFFF8000 | $int; |
|
|
|
} else { |
|
|
|
} |
|
|
|
break; |
|
|
|
// Convert the value |
|
|
|
} |
|
|
|
return (int) self::dword2signedInt($int); |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* bytes2unsignedInt |
|
|
|
* real2float |
|
|
|
* |
|
|
|
* |
|
|
|
* The function converts array of 2 or 4 bytes to unsigned integer. |
|
|
|
* This function converts a value in IEC-1131 REAL single precision form to float. |
|
|
|
* The return value depends on order of the input bytes (endianning). |
|
|
|
* |
|
|
|
* |
|
|
|
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or |
|
|
|
* @param array $values |
|
|
|
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes |
|
|
|
* @param bool $endianness |
|
|
|
* @ Georgia Tech 21-Nov-2007] or |
|
|
|
* @return int|float |
|
|
|
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}] |
|
|
|
*/ |
|
|
|
* |
|
|
|
public static function bytes2unsignedInt($values, $endianness = 0) { |
|
|
|
* @param int $value in IEC REAL data type to be converted |
|
|
|
$data = array(); |
|
|
|
* @return float float value |
|
|
|
$int = 0; |
|
|
|
*/ |
|
|
|
// Set the array to correct form |
|
|
|
private static function real2float($value) |
|
|
|
$data = self::checkData($values); |
|
|
|
{ |
|
|
|
// Combine bytes |
|
|
|
// get unsigned long |
|
|
|
$int = self::combineBytes($data, $endianness); |
|
|
|
$ulong = pack("L", $value); |
|
|
|
// Convert the value |
|
|
|
// set float |
|
|
|
return self::dword2unsignedInt($int); |
|
|
|
$float = unpack("f", $ulong); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
return $float[1]; |
|
|
|
* bytes2string |
|
|
|
} |
|
|
|
* |
|
|
|
|
|
|
|
* The function converts an values array to the string. The function detects |
|
|
|
|
|
|
|
* the end of the string by 0x00 character as defined by string standards. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param array $values |
|
|
|
|
|
|
|
* @param bool $endianness |
|
|
|
|
|
|
|
* @return string |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static function bytes2string($values, $endianness = 0) { |
|
|
|
|
|
|
|
// Prepare string variable |
|
|
|
|
|
|
|
$str = ""; |
|
|
|
|
|
|
|
// Parse the received data word array |
|
|
|
|
|
|
|
for($i=0;$i<count($values);$i+=2) { |
|
|
|
|
|
|
|
if ($endianness) { |
|
|
|
|
|
|
|
if($values[$i] != 0) |
|
|
|
|
|
|
|
$str .= chr($values[$i]); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
if($values[$i+1] != 0) |
|
|
|
|
|
|
|
$str .= chr($values[$i+1]); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
if($values[$i+1] != 0) |
|
|
|
|
|
|
|
$str .= chr($values[$i+1]); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
if($values[$i] != 0) |
|
|
|
|
|
|
|
$str .= chr($values[$i]); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// return string |
|
|
|
|
|
|
|
return $str; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* real2float |
|
|
|
* dword2signedInt |
|
|
|
* |
|
|
|
* |
|
|
|
* This function converts a value in IEC-1131 REAL single precision form to float. |
|
|
|
* Switch double word to signed integer |
|
|
|
* |
|
|
|
* |
|
|
|
* For more see [{@link http://en.wikipedia.org/wiki/Single_precision Single precision on Wiki}] or |
|
|
|
* @param int $value |
|
|
|
* [{@link http://de.php.net/manual/en/function.base-convert.php PHP base_convert function commentary}, Todd Stokes @ Georgia Tech 21-Nov-2007] or |
|
|
|
* @return int |
|
|
|
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}] |
|
|
|
*/ |
|
|
|
* |
|
|
|
private static function dword2signedInt($value) |
|
|
|
* @param value value in IEC REAL data type to be converted |
|
|
|
{ |
|
|
|
* @return float float value |
|
|
|
if ((0x80000000 & $value) != 0) { |
|
|
|
*/ |
|
|
|
return -(0x7FFFFFFF & ~$value) - 1; |
|
|
|
private static function real2float($value) { |
|
|
|
} else { |
|
|
|
// get unsigned long |
|
|
|
return (0x7FFFFFFF & $value); |
|
|
|
$ulong = pack("L", $value); |
|
|
|
} |
|
|
|
// set float |
|
|
|
} |
|
|
|
$float = unpack("f", $ulong); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $float[1]; |
|
|
|
/** |
|
|
|
} |
|
|
|
* 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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* dword2signedInt |
|
|
|
* checkData |
|
|
|
* |
|
|
|
* |
|
|
|
* Switch double word to signed integer |
|
|
|
* Check if the data variable is array, and check if the values are numeric |
|
|
|
* |
|
|
|
* |
|
|
|
* @param int $value |
|
|
|
* @param int[] $data |
|
|
|
* @return int |
|
|
|
* @return int |
|
|
|
*/ |
|
|
|
* @throws Exception |
|
|
|
private static function dword2signedInt($value) { |
|
|
|
*/ |
|
|
|
if ((0x80000000 & $value) != 0) { |
|
|
|
private static function checkData($data) |
|
|
|
return -(0x7FFFFFFF & ~$value)-1; |
|
|
|
{ |
|
|
|
} else { |
|
|
|
// Check the data |
|
|
|
return (0x7FFFFFFF & $value); |
|
|
|
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; |
|
|
|
* 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 |
|
|
|
* combineBytes |
|
|
|
* |
|
|
|
* |
|
|
|
* Check if the data variable is array, and check if the values are numeric |
|
|
|
* Combine bytes together |
|
|
|
* |
|
|
|
* |
|
|
|
* @param int $data |
|
|
|
* @param int $data |
|
|
|
* @return int |
|
|
|
* @param bool $bigEndian |
|
|
|
*/ |
|
|
|
* @return int |
|
|
|
private static function checkData($data) { |
|
|
|
*/ |
|
|
|
// Check the data |
|
|
|
private static function combineBytes($data, $bigEndian) |
|
|
|
if (!is_array($data) || |
|
|
|
{ |
|
|
|
count($data)<2 || |
|
|
|
$value = 0; |
|
|
|
count($data)>4 || |
|
|
|
// Combine bytes |
|
|
|
count($data)==3) { |
|
|
|
if ($bigEndian == 0) { |
|
|
|
throw new Exception('The input data should be an array of 2 or 4 bytes.'); |
|
|
|
$value = (($data[3] & 0xFF) << 16) | |
|
|
|
} |
|
|
|
(($data[2] & 0xFF) << 24) | |
|
|
|
// Fill the rest of array by zeroes |
|
|
|
(($data[1] & 0xFF)) | |
|
|
|
if (count($data) == 2) { |
|
|
|
(($data[0] & 0xFF) << 8); |
|
|
|
$data[2] = 0; |
|
|
|
} else { |
|
|
|
$data[3] = 0; |
|
|
|
$value = (($data[3] & 0xFF) << 24) | |
|
|
|
} |
|
|
|
(($data[2] & 0xFF) << 16) | |
|
|
|
// Check the values to be number |
|
|
|
(($data[1] & 0xFF) << 8) | |
|
|
|
if (!is_numeric($data[0]) || |
|
|
|
(($data[0] & 0xFF)); |
|
|
|
!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; |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
?> |
|
|
|
|