-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodbus.py
More file actions
169 lines (122 loc) · 4.88 KB
/
modbus.py
File metadata and controls
169 lines (122 loc) · 4.88 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import logging
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.client.sync import ModbusTcpClient
import pymodbus.exceptions
import buscommon
MODBUS_FUNC_READHOLDING = 3
MODBUS_SINT16 = 0
MODBUS_SINT32 = 1
MODBUS_SINT32_RWORDS = 2
MODBUS_SKIP2 = 3
MODBUS_UINT16 = 4
MODBUS_UINT32 = 5
MODBUS_UINT32_RWORDS = 6
MODBUS_FLOAT = 7
MODBUS_FLOAT_SKIP2 = 8
MODBUS_FLOAT_RBYTES = 9
MODBUS_FLOAT_RWORDS = 10
MODBUS_FLOAT_RSKIP2 = 11
MODBUS_ABB_REAL32_U = 12
MODBUS_ABB_REAL32_S = 13
FORMAT_LENGTH = {MODBUS_SINT16: 1, MODBUS_SINT32: 2, MODBUS_SINT32_RWORDS: 2, MODBUS_SKIP2: 3,
MODBUS_UINT16: 4, MODBUS_UINT32: 5, MODBUS_UINT32_RWORDS: 6, MODBUS_FLOAT: 7,
MODBUS_FLOAT_SKIP2: 8, MODBUS_FLOAT_RBYTES: 9, MODBUS_FLOAT_RWORDS: 10,
MODBUS_FLOAT_RSKIP2: 11, MODBUS_ABB_REAL32_U: 2, MODBUS_ABB_REAL32_S: 2}
"""
ModbusMixin - Mixin for Modbus specific functionality
"""
class ModbusMixin:
def read_register(self, chl):
#print(f"Reading {chl.name}")
ret = buscommon.ReadResponse()
try:
with ModbusTcpClient(host=self.host, port=self.port, timeout=self.timeout) as client:
if chl.modbus_fn_code == MODBUS_FUNC_READHOLDING:
count = FORMAT_LENGTH[chl.format]
ret.response = client.read_holding_registers(address=chl.address, count=count, unit=chl.device_id)
client.close()
else:
raise pymodbus.exceptions.ModbusException("Function code not yet implemented.")
if ret.response.isError():
ret.result = -1
except pymodbus.exceptions.ModbusException as e:
ret.exception = e
ret.result = -1
return ret
def decode_data_format(self, chl, res):
value = None
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_UINT16:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_SINT16:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_UINT32:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_UINT32_RWORDS:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_SINT32:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_SINT32_RWORDS:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_SKIP2:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_FLOAT:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_FLOAT_SKIP2:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_FLOAT_RBYTES:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_FLOAT_RWORDS:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_FLOAT_RSKIP2:
byteorder = Endian.Big
wordorder = Endian.Little
if chl.format == MODBUS_ABB_REAL32_U or chl.format == MODBUS_ABB_REAL32_S:
byteorder = Endian.Big
wordorder = Endian.Big
decoder = BinaryPayloadDecoder.fromRegisters(res.response.registers, byteorder=byteorder, wordorder=wordorder)
if chl.format == MODBUS_UINT16:
value = decoder.decode_16bit_uint()
if chl.format == MODBUS_SINT16:
value = decoder.decode_16bit_int()
if chl.format == MODBUS_SINT32:
value = decoder.decode_32bit_int()
if chl.format == MODBUS_UINT32:
value = decoder.decode_32bit_uint()
if chl.format == MODBUS_SINT32_RWORDS:
value = decoder.decode_32bit_int()
if chl.format == MODBUS_SKIP2:
pass
if chl.format == MODBUS_UINT32_RWORDS:
pass
if chl.format == MODBUS_FLOAT:
value = decoder.decode_32bit_float()
if chl.format == MODBUS_FLOAT_SKIP2:
pass
if chl.format == MODBUS_FLOAT_RBYTES:
pass
if chl.format == MODBUS_FLOAT_RWORDS:
pass
if chl.format == MODBUS_FLOAT_RSKIP2:
pass
if chl.format == MODBUS_ABB_REAL32_U:
value = decoder.decode_16bit_uint()
# print("Reg 1 : %d" % res.response.registers[0])
# print("Reg 2 : %d" % res.response.registers[1])
if chl.format == MODBUS_ABB_REAL32_S:
value = int(format(res.response.registers[1], "b").zfill(16) + format(res.response.registers[0], "b").zfill(16),2)
# decoder.decode_bits()
return value