-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIecType.php
More file actions
133 lines (124 loc) · 3.93 KB
/
IecType.php
File metadata and controls
133 lines (124 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Phpmodbus Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
*
* This source file is subject to the "PhpModbus license" that is bundled
* with this package in the file license.txt.
*
* @author Jan Krakora
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @license PhpModbus license
* @category Phpmodbus
* @package Phpmodbus
* @version $id$
*/
/**
* IecType
*
* The class includes set of IEC-1131 data type functions that converts a PHP
* data types to a IEC data type.
*
* @author Jan Krakora
* @copyright Copyright (c) 2004, 2010 Jan Krakora, WAGO Kontakttechnik GmbH & Co. KG (http://www.wago.com)
* @package Phpmodbus
*/
class IecType {
/**
* iecBYTE
*
* Converts a value to IEC-1131 BYTE data type
*
* @param value value from 0 to 255
* @return value IEC BYTE data type
*
*/
function iecBYTE($value) {
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
*
*/
function iecINT($value) {
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
*
*/
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 value value to be converted
* @param value endianness defines endian codding (little endian == 0, big endian == 1)
* @return value IEC-1131 REAL data type
*/
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 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 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));
}
}
?>