Cleaned up the code, fixed broken phpdoc tags, formatting
This commit is contained in:
+114
-109
@@ -8,12 +8,12 @@ namespace PHPModbus;
|
||||
* This source file is subject to the "PhpModbus license" that is bundled
|
||||
* with this package in the file license.txt.
|
||||
*
|
||||
* @author Jan Krakora
|
||||
* @author Jan Krakora
|
||||
* @copyright Copyright (c) 2004, 2013 Jan Krakora
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -22,115 +22,120 @@ namespace PHPModbus;
|
||||
* The class includes set of IEC-1131 data type functions that converts a PHP
|
||||
* data types to a IEC data type.
|
||||
*
|
||||
* @author Jan Krakora
|
||||
* @author Jan Krakora
|
||||
* @copyright Copyright (c) 2004, 2010 Jan Krakora
|
||||
* @package Phpmodbus
|
||||
* @package Phpmodbus
|
||||
*/
|
||||
class IecType {
|
||||
class IecType
|
||||
{
|
||||
/**
|
||||
* iecBYTE
|
||||
*
|
||||
* 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 iecBYTE($value)
|
||||
{
|
||||
return chr($value & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* iecBYTE
|
||||
*
|
||||
* Converts a value to IEC-1131 BYTE data type
|
||||
*
|
||||
* @param value value from 0 to 255
|
||||
* @return value IEC BYTE data type
|
||||
*
|
||||
*/
|
||||
public static function iecBYTE($value) {
|
||||
return chr($value & 0xFF);
|
||||
}
|
||||
/**
|
||||
* iecINT
|
||||
*
|
||||
* 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 iecINT($value)
|
||||
{
|
||||
return self::iecBYTE(($value >> 8) & 0x00FF) .
|
||||
self::iecBYTE(($value & 0x00FF));
|
||||
}
|
||||
|
||||
/**
|
||||
* iecINT
|
||||
*
|
||||
* Converts a value to IEC-1131 INT data type
|
||||
*
|
||||
* @param value value to be converted
|
||||
* @return value IEC-1131 INT data type
|
||||
*
|
||||
*/
|
||||
public static function iecINT($value) {
|
||||
return self::iecBYTE(($value >> 8) & 0x00FF) .
|
||||
self::iecBYTE(($value & 0x00FF));
|
||||
}
|
||||
/**
|
||||
* iecDINT
|
||||
*
|
||||
* Converts a value to IEC-1131 DINT data type
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
// result with right endianness
|
||||
return self::endianness($value, $bigEndian);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
*/
|
||||
public static 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 int $value to be converted
|
||||
* @param bool $bigEndian defines endian codding (little endian == 0, big endian == 1)
|
||||
* @return int IEC-1131 REAL data type
|
||||
*/
|
||||
public static function iecREAL($value, $bigEndian = 0)
|
||||
{
|
||||
// iecREAL representation
|
||||
$real = self::float2iecReal($value);
|
||||
// result with right endianness
|
||||
return self::endianness($real, $bigEndian);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static 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 static 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 static 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));
|
||||
}
|
||||
/**
|
||||
* 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 int IEC REAL data type
|
||||
*/
|
||||
private static 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 $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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
+1316
-1258
File diff suppressed because it is too large
Load Diff
+26
-25
@@ -4,47 +4,48 @@ namespace PHPModbus;
|
||||
|
||||
/**
|
||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||
*
|
||||
*
|
||||
* This source file is subject to the "PhpModbus license" that is bundled
|
||||
* with this package in the file license.txt.
|
||||
*
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @tutorial Phpmodbus.pkg
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
*
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @tutorial Phpmodbus.pkg
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* ModbusMasterTcp
|
||||
*
|
||||
* This class deals with the MODBUS master using TCP. Extends ModbusMaster class.
|
||||
*
|
||||
*
|
||||
* Implemented MODBUS functions:
|
||||
* - FC 1: read coils
|
||||
* - FC 3: read multiple registers
|
||||
* - FC 15: write multiple coils
|
||||
* - FC 15: write multiple coils
|
||||
* - FC 16: write multiple registers
|
||||
* - FC 23: read write registers
|
||||
*
|
||||
* @author Jan Krakora
|
||||
*
|
||||
* @author Jan Krakora
|
||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||
* @package Phpmodbus
|
||||
* @package Phpmodbus
|
||||
*
|
||||
*/
|
||||
class ModbusMasterTcp extends ModbusMaster {
|
||||
/**
|
||||
* ModbusMasterTcp
|
||||
*
|
||||
* This is the constructor that defines {@link $host} IP address of the object.
|
||||
*
|
||||
* @param String $host An IP address of a Modbus TCP device. E.g. "192.168.1.1".
|
||||
*/
|
||||
function ModbusMasterTcp($host){
|
||||
$this->host = $host;
|
||||
$this->socket_protocol = "TCP";
|
||||
}
|
||||
class ModbusMasterTcp extends ModbusMaster
|
||||
{
|
||||
/**
|
||||
* ModbusMasterTcp
|
||||
*
|
||||
* This is the constructor that defines {@link $host} IP address of the object.
|
||||
*
|
||||
* @param String $host An IP address of a Modbus TCP device. E.g. "192.168.1.1".
|
||||
*/
|
||||
public function __construct($host)
|
||||
{
|
||||
parent::__construct($host, "TCP");
|
||||
}
|
||||
}
|
||||
|
||||
+26
-26
@@ -4,48 +4,48 @@ namespace PHPModbus;
|
||||
|
||||
/**
|
||||
* Phpmodbus Copyright (c) 2004, 2012 Jan Krakora
|
||||
*
|
||||
*
|
||||
* This source file is subject to the "PhpModbus license" that is bundled
|
||||
* with this package in the file license.txt.
|
||||
*
|
||||
*
|
||||
*
|
||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @tutorial Phpmodbus.pkg
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
*
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @tutorial Phpmodbus.pkg
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* ModbusMasterUdp
|
||||
*
|
||||
* This class deals with the MODBUS master using UDP stack.
|
||||
*
|
||||
*
|
||||
* Implemented MODBUS master functions:
|
||||
* - FC 1: read coils
|
||||
* - FC 3: read multiple registers
|
||||
* - FC 15: write multiple coils
|
||||
* - FC 15: write multiple coils
|
||||
* - FC 16: write multiple registers
|
||||
* - FC 23: read write registers
|
||||
*
|
||||
* @author Jan Krakora
|
||||
*
|
||||
* @author Jan Krakora
|
||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||
* @package Phpmodbus
|
||||
* @package Phpmodbus
|
||||
*
|
||||
*/
|
||||
class ModbusMasterUdp extends ModbusMaster {
|
||||
|
||||
/**
|
||||
* ModbusMasterUdp
|
||||
*
|
||||
* This is the constructor that defines {@link $host} IP address of the object.
|
||||
*
|
||||
* @param String $host An IP address of a Modbus UDP device. E.g. "192.168.1.1".
|
||||
*/
|
||||
function ModbusMasterUdp($host){
|
||||
$this->host = $host;
|
||||
$this->socket_protocol = "UDP";
|
||||
}
|
||||
class ModbusMasterUdp extends ModbusMaster
|
||||
{
|
||||
/**
|
||||
* ModbusMasterUdp
|
||||
*
|
||||
* This is the constructor that defines {@link $host} IP address of the object.
|
||||
*
|
||||
* @param String $host An IP address of a Modbus UDP device. E.g. "192.168.1.1".
|
||||
*/
|
||||
public function __construct($host)
|
||||
{
|
||||
parent::__construct($host, "UDP");
|
||||
}
|
||||
}
|
||||
|
||||
+231
-217
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PHPModbus;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* with this package in the file license.txt.
|
||||
*
|
||||
* @author Jan Krakora
|
||||
* @author Jan Krakora
|
||||
* @copyright Copyright (c) 2004, 2012 Jan Krakora
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
* @license PhpModbus license
|
||||
* @category Phpmodbus
|
||||
* @package Phpmodbus
|
||||
* @version $id$
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -23,230 +24,243 @@ namespace PHPModbus;
|
||||
* 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.
|
||||
*
|
||||
* @author Jan Krakora
|
||||
* @author 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
|
||||
*
|
||||
* 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;
|
||||
/**
|
||||
* 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 $bigEndian
|
||||
* @return int
|
||||
*/
|
||||
public static function bytes2signedInt($values, $bigEndian = 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);
|
||||
// Combine bytes
|
||||
$real = self::combineBytes($data, $endianness);
|
||||
// Convert the real value to float
|
||||
return (float) self::real2float($real);
|
||||
}
|
||||
/**
|
||||
* 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 $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
|
||||
*
|
||||
* 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);
|
||||
}
|
||||
/**
|
||||
* 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 $bigEndian
|
||||
* @return string
|
||||
*/
|
||||
public static function bytes2string($values, $bigEndian = 0)
|
||||
{
|
||||
// Prepare string variable
|
||||
$str = "";
|
||||
// Parse the received data word array
|
||||
for ($i = 0; $i < count($values); $i += 2) {
|
||||
if ($bigEndian) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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] or
|
||||
* [{@link http://www.php.net/manual/en/function.pack.php PHP pack/unpack functionality}]
|
||||
*
|
||||
* @param int $value in IEC REAL data type to be converted
|
||||
* @return float float value
|
||||
*/
|
||||
private static function real2float($value)
|
||||
{
|
||||
// get unsigned long
|
||||
$ulong = pack("L", $value);
|
||||
// set float
|
||||
$float = unpack("f", $ulong);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
return $float[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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] or
|
||||
* [{@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
|
||||
* @return float float value
|
||||
*/
|
||||
private static function real2float($value) {
|
||||
// get unsigned long
|
||||
$ulong = pack("L", $value);
|
||||
// set float
|
||||
$float = unpack("f", $ulong);
|
||||
|
||||
return $float[1];
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* Check if the data variable is array, and check if the values are numeric
|
||||
*
|
||||
* @param int[] $data
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function checkData($data)
|
||||
{
|
||||
// Check the data
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* checkData
|
||||
*
|
||||
* Check if the data variable is array, and check if the values are numeric
|
||||
*
|
||||
* @param int $data
|
||||
* @return int
|
||||
*/
|
||||
private static function checkData($data) {
|
||||
// Check the data
|
||||
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;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* combineBytes
|
||||
*
|
||||
* Combine bytes together
|
||||
*
|
||||
* @param int $data
|
||||
* @param bool $bigEndian
|
||||
* @return int
|
||||
*/
|
||||
private static function combineBytes($data, $bigEndian)
|
||||
{
|
||||
$value = 0;
|
||||
// Combine bytes
|
||||
if ($bigEndian == 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user