|
|
@ -1,6 +1,7 @@ |
|
|
|
<?php |
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
namespace PHPModbus; |
|
|
|
namespace PHPModbus; |
|
|
|
|
|
|
|
|
|
|
|
use Exception; |
|
|
|
use Exception; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -15,7 +16,6 @@ use Exception; |
|
|
|
* @category Phpmodbus |
|
|
|
* @category Phpmodbus |
|
|
|
* @package Phpmodbus |
|
|
|
* @package Phpmodbus |
|
|
|
* @version $id$ |
|
|
|
* @version $id$ |
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -27,7 +27,6 @@ use Exception; |
|
|
|
* @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 |
|
|
|
{ |
|
|
|
{ |
|
|
@ -70,7 +69,7 @@ class PhpType |
|
|
|
// Combine bytes |
|
|
|
// Combine bytes |
|
|
|
$int = self::combineBytes($data, $bigEndian); |
|
|
|
$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 |
|
|
@ -159,9 +158,9 @@ class PhpType |
|
|
|
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 |
|
|
|
$float = unpack("f", $ulong); |
|
|
|
$float = unpack('f', $ulong); |
|
|
|
|
|
|
|
|
|
|
|
return $float[1]; |
|
|
|
return $float[1]; |
|
|
|
} |
|
|
|
} |
|
|
@ -176,7 +175,7 @@ class PhpType |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
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); |
|
|
@ -193,7 +192,7 @@ class PhpType |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
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); |
|
|
@ -206,29 +205,30 @@ 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 array|\int[] |
|
|
|
* @throws Exception |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static function checkData($data) |
|
|
|
private static function checkData($data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Check the data |
|
|
|
// Check the data |
|
|
|
if (!is_array($data) || |
|
|
|
$count = count($data); |
|
|
|
count($data) < 2 || |
|
|
|
if (!is_array($data) |
|
|
|
count($data) > 4 || |
|
|
|
|| $count < 2 |
|
|
|
count($data) == 3 |
|
|
|
|| $count > 4 |
|
|
|
|
|
|
|
|| $count === 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 |
|
|
|
if (count($data) == 2) { |
|
|
|
if ($count === 2) { |
|
|
|
$data[2] = 0; |
|
|
|
$data[2] = 0; |
|
|
|
$data[3] = 0; |
|
|
|
$data[3] = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
// Check the values to be number |
|
|
|
// Check the values to be number |
|
|
|
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'); |
|
|
|
} |
|
|
|
} |
|
|
@ -252,13 +252,13 @@ class PhpType |
|
|
|
if ($bigEndian == 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; |
|
|
|