|
|
|
@ -29,32 +29,30 @@ namespace PHPModbus; |
|
|
|
|
class IecType |
|
|
|
|
{ |
|
|
|
|
/** |
|
|
|
|
* iecBYTE |
|
|
|
|
* |
|
|
|
|
* Converts a value to IEC-1131 BYTE data type |
|
|
|
|
* iecINT |
|
|
|
|
* |
|
|
|
|
* @param int $value from 0 to 255 |
|
|
|
|
* @return int IEC BYTE data type |
|
|
|
|
* Converts a value to IEC-1131 INT data type |
|
|
|
|
* |
|
|
|
|
* @param int $value to be converted |
|
|
|
|
* @return int IEC-1131 INT data type |
|
|
|
|
*/ |
|
|
|
|
public static function iecBYTE($value) |
|
|
|
|
public static function iecINT($value) |
|
|
|
|
{ |
|
|
|
|
return chr($value & 0xFF); |
|
|
|
|
return self::iecBYTE(($value >> 8) & 0x00FF) . |
|
|
|
|
self::iecBYTE($value & 0x00FF); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* iecINT |
|
|
|
|
* |
|
|
|
|
* Converts a value to IEC-1131 INT data type |
|
|
|
|
* iecBYTE |
|
|
|
|
* |
|
|
|
|
* @param int $value to be converted |
|
|
|
|
* @return int IEC-1131 INT data type |
|
|
|
|
* Converts a value to IEC-1131 BYTE data type |
|
|
|
|
* |
|
|
|
|
* @param int $value from 0 to 255 |
|
|
|
|
* @return int IEC BYTE data type |
|
|
|
|
*/ |
|
|
|
|
public static function iecINT($value) |
|
|
|
|
public static function iecBYTE($value) |
|
|
|
|
{ |
|
|
|
|
return self::iecBYTE(($value >> 8) & 0x00FF) . |
|
|
|
|
self::iecBYTE(($value & 0x00FF)); |
|
|
|
|
return chr($value & 0xFF); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -65,7 +63,6 @@ class IecType |
|
|
|
|
* @param int $value to be converted |
|
|
|
|
* @param int $bigEndian defines endian codding (little endian == 0, big endian == 1) |
|
|
|
|
* @return int IEC-1131 INT data type |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
public static function iecDINT($value, $bigEndian = 0) |
|
|
|
|
{ |
|
|
|
@ -73,6 +70,33 @@ class IecType |
|
|
|
|
return self::endianness($value, $bigEndian); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* endianness |
|
|
|
|
* |
|
|
|
|
* Make endianess as required. |
|
|
|
|
* For more see http://en.wikipedia.org/wiki/Endianness |
|
|
|
|
* |
|
|
|
|
* @param int $value |
|
|
|
|
* @param bool $bigEndian |
|
|
|
|
* @return int |
|
|
|
|
*/ |
|
|
|
|
private static function endianness($value, $bigEndian = 0) |
|
|
|
|
{ |
|
|
|
|
if ($bigEndian == 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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* iecREAL |
|
|
|
|
* |
|
|
|
@ -106,36 +130,9 @@ class IecType |
|
|
|
|
private static function float2iecReal($value) |
|
|
|
|
{ |
|
|
|
|
// get float binary string |
|
|
|
|
$float = pack("f", $value); |
|
|
|
|
$float = pack('f', $value); |
|
|
|
|
// set 32-bit unsigned integer of the float |
|
|
|
|
$w = unpack("L", $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 $bigEndian |
|
|
|
|
* @return int |
|
|
|
|
*/ |
|
|
|
|
private static function endianness($value, $bigEndian = 0) |
|
|
|
|
{ |
|
|
|
|
if ($bigEndian == 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)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|