Modbus TCP and UDP library for PHP., originally from code.google.com/p/phpmodbus
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
php-modbus/Phpmodbus/IecType.php

133 lines
4.0 KiB

12 years ago
<?php
/**
12 years ago
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
12 years ago
*
12 years ago
* This source file is subject to the "PhpModbus license" that is bundled
12 years ago
* with this package in the file license.txt.
*
12 years ago
* @author Jan Krakora
12 years ago
* @copyright Copyright (c) 2004, 2013 Jan Krakora
12 years ago
* @license PhpModbus license
12 years ago
* @category Phpmodbus
* @package Phpmodbus
* @version $id$
*/
/**
* IecType
12 years ago
*
* The class includes set of IEC-1131 data type functions that converts a PHP
12 years ago
* data types to a IEC data type.
*
* @author Jan Krakora
12 years ago
* @copyright Copyright (c) 2004, 2010 Jan Krakora
12 years ago
* @package Phpmodbus
12 years ago
*/
class IecType {
12 years ago
/**
* iecBYTE
*
* Converts a value to IEC-1131 BYTE data type
*
* @param value value from 0 to 255
* @return value IEC BYTE data type
*
*/
12 years ago
public static function iecBYTE($value) {
12 years ago
return chr($value & 0xFF);
}
/**
* iecINT
*
* Converts a value to IEC-1131 INT data type
*
* @param value value to be converted
* @return value IEC-1131 INT data type
*
*/
12 years ago
public static function iecINT($value) {
12 years ago
return self::iecBYTE(($value >> 8) & 0x00FF) .
self::iecBYTE(($value & 0x00FF));
}
/**
* 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
*
*/
12 years ago
public static function iecDINT($value, $endianness = 0) {
12 years ago
// 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 value value to be converted
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
* @return value IEC-1131 REAL data type
*/
12 years ago
public static function iecREAL($value, $endianness = 0) {
12 years ago
// 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
*/
12 years ago
private static function float2iecReal($value) {
12 years ago
// 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
*/
12 years ago
private static function endianness($value, $endianness = 0) {
12 years ago
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));
}
12 years ago
}
12 years ago
12 years ago
?>