-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModbusUtils.py
45 lines (34 loc) · 844 Bytes
/
ModbusUtils.py
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
#!/usr/bin/env python
##
# @package ModbusUtils Module contains the various functions required for decoding the Modbus data
#
import logging
LOG = logging.getLogger( __name__ )
def reverseTwoElements( data ):
if len( data ) % 2 != 0:
LOG.warn( "Data is not in multiple of 2" )
return data
newdata = []
newdata.append(data[2])
newdata.append(data[3])
newdata.append(data[0])
newdata.append(data[1])
return newdata
# swap 1st and 3rd byte
temp = data[0]
data[0] = data[2]
data[2] = temp
# swap 2nd and 4th byte
temp = data[1]
data[1] = data[3]
data[3] = temp
return data
##
#
def bytesToInt( data ):
length = len( data )
intVal = 0
for i in range( 0, length ):
intVal = intVal << 8
intVal = intVal | data[i]
return intVal