Repository navigation
Expand file tree
/
Copy pathmodbus.py
More file actions
287 lines (239 loc) · 10.1 KB
/
Copy pathmodbus.py
File metadata and controls
287 lines (239 loc) · 10.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""@package modbus
Module to work with Modbus protocol.
Includes class Address to work with modbus address.
"""
# Modbus memory types
Memory_Unknown = -1 ##< Memory type for invalid address
Memory_0x = 0 ##< Memory type for coils
Memory_1x = 1 ##< Memory type for input discretes
Memory_3x = 3 ##< Memory type for input registers
Memory_4x = 4 ##< Memory type for iholding registers
Notation_Default = 0 ##< Default notation which is equal to Modbus notation
Notation_Modbus = 1 ##< Standard Modbus address notation like `000001`, `100001`, `300001`, `400001`
Notation_IEC61131 = 2 ##< IEC-61131 address notation like `%%Q0`, `%%I0`, `%%IW0`, `%%MW0`
Notation_IEC61131Hex = 3 ##< IEC-61131 Hex address notation like `%%Q0000h`, `%%I0000h`, `%%IW0000h`, `%%MW0000h`
## @brief Python set that contains supported Modbus Address types
MemoryTypeSet = { Memory_0x, Memory_1x, Memory_3x, Memory_4x }
sIEC61131Prefix0x = "%Q" ##< IEC-61131 address notation prefix for coils
sIEC61131Prefix1x = "%I" ##< IEC-61131 address notation prefix for input discretes
sIEC61131Prefix3x = "%IW" ##< IEC-61131 address notation prefix for input registers
sIEC61131Prefix4x = "%MW" ##< IEC-61131 address notation prefix for holding registers
cIEC61131SuffixHex = 'h' ##< Suffix for IEC-61131 Hex address notation
## @brief Python set that contains supported Modbus address IEC61131 prefixes
IEC61131PrefixMap = {
Memory_0x: sIEC61131Prefix0x,
Memory_1x: sIEC61131Prefix1x,
Memory_3x: sIEC61131Prefix3x,
Memory_4x: sIEC61131Prefix4x,
}
class Address:
"""Modbus Data Address class. Represents Modbus Data Address.
`Address` class is used to represent Modbus Data Address. It contains memory type and offset.
E.g. `modbus.Address(modbus.Memory_4x, 0)` creates `400001` standard address.
E.g. `modbus.Address(400001)` creates `Address` with type `Modbus::Memory_4x` and offset `0`, and
`modbus.Address(1)` creates `modbus.Address` with type `modbus.Memory_0x` and offset `0`.
Class provides convertions from/to string methods.
Class supports next operators and standard functions:
+, -, <, <=, >, >=, ==, !=, hash(), str(), int()
"""
def __init__(self, value=None, offset=None):
"""Constructor of the class.
Can have next forms:
* `Address()` - creates invalid address class
* `Address(Memory_4x, 0)` - creates address for holding registers with `offset=0`
* `Address("%MW0")` - creates address for holding registers with `offset=0`
* `Address("%Q0000h")` - creates address for coils with `offset=0`
* `Address("100001")` - creates address for input discretes with `offset=0`
* `Address(300001)` - creates address for input registers with `offset=0`
"""
self._type = Memory_Unknown
self._offset = 0
if value is None:
pass
elif isinstance(value, int) and offset is None:
self.fromint(value)
elif isinstance(value, str) and offset is None:
self.fromstr(value)
elif isinstance(value, int) and isinstance(offset, int):
self.settype(value)
self.setoffset(offset)
else:
raise ValueError("Invalid constructor parameters")
def isvalid(self) -> bool:
"""Returns `True` if memory type is not `Modbus::Memory_Unknown`, `False` otherwise.
"""
return self._type != Memory_Unknown
def type(self) -> int:
"""Returns memory type of Modbus Data Address.
"""
return self._type
def settype(self, tp: int):
"""Sets memory type of Modbus Data Address.
"""
if tp not in MemoryTypeSet:
raise ValueError(f"Invalid memory type: {tp}. Memory type must be [0,1,3,4]")
self._type = tp
def offset(self) -> int:
"""Returns memory offset of Modbus Data Address.
"""
return self._offset
def setoffset(self, offset: int):
"""Sets memory offset of Modbus Data Address.
"""
if not (0 <= offset <= 65535):
raise ValueError(f"Invalid offset: {offset}. Offset must be in range [0:65535]")
self._offset = offset
def number(self) -> int:
""" Returns memory number (offset+1) of Modbus Data Address.
"""
return self._offset + 1
def setnumber(self, number: int):
"""Set memory number (offset+1) of Modbus Data Address.
"""
self.setoffset(number - 1)
def fromint(self, v: int):
"""Make modbus address from integer representaion
"""
number = v % 100000
if number < 1 or number > 65536:
self._type = Memory_Unknown
self._offset = 0
raise ValueError(f"Invalid integer '{v}' to convert into Address: number part '{number}' must be [1:65536]")
mem_type = v // 100000
if mem_type in MemoryTypeSet:
self._type = mem_type
self.setoffset(number - 1)
else:
raise ValueError(f"Invalid integer '{v}' to convert into Address: memory type '{mem_type}' must be [0,1,3,4]")
def toint(self) -> int:
"""Converts current Modbus Data Address to `int`,
e.g. `Address(Memory_4x, 0)` will be converted to `400001`.
"""
return (self._type * 100000) + self.number()
def fromstr(self, s: str):
"""Make modbus address from string representaion
"""
def dec_digit(c):
return int(c) if c.isdigit() else -1
def hex_digit(c):
try:
return int(c, 16)
except ValueError:
return -1
if s.startswith('%'):
i = 0
if s.startswith(sIEC61131Prefix3x):
self._type = Memory_3x
i = len(sIEC61131Prefix3x)
elif s.startswith(sIEC61131Prefix4x):
self._type = Memory_4x
i = len(sIEC61131Prefix4x)
elif s.startswith(sIEC61131Prefix0x):
self._type = Memory_0x
i = len(sIEC61131Prefix0x)
elif s.startswith(sIEC61131Prefix1x):
self._type = Memory_1x
i = len(sIEC61131Prefix1x)
else:
raise ValueError(f"Invalid str '{s}' to convert into Address")
offset = 0
suffix = s[-1]
if suffix == cIEC61131SuffixHex:
for c in s[i:-1]:
offset *= 16
d = hex_digit(c)
if d < 0:
return Address()
offset += d
else:
for c in s[i:]:
offset *= 10
d = dec_digit(c)
if d < 0:
return Address()
offset += d
self.setoffset(offset)
else:
acc = 0
for c in s:
d = dec_digit(c)
if d < 0:
return Address()
acc = acc * 10 + d
self.fromint(acc)
def tostr(self, notation: int = Notation_Default) -> str:
"""Returns string repr of Modbus Data Address with specified notation:
* `Notation_Modbus` - `Address(Memory_4x, 0)` will be converted to `"400001"`.
* `Notation_IEC61131` - `Address(Memory_4x, 0)` will be converted to `"%MW0"`.
* `Notation_IEC61131Hex` - `Address(Memory_4x, 0)` will be converted to `"%MW0000h"`.
"""
def to_dec_string(n, width=0):
return str(n).rjust(width, '0') if width else str(n)
def to_hex_string(n):
return format(n, 'X').rjust(4, '0')
if not self.isvalid():
return "Invalid address"
if notation == Notation_IEC61131:
return IEC61131PrefixMap.get(self._type, "") + to_dec_string(self._offset)
elif notation == Notation_IEC61131Hex:
return IEC61131PrefixMap.get(self._type, "") + to_hex_string(self._offset) + cIEC61131SuffixHex
else:
return to_dec_string(self.toint(), 6)
def __int__(self):
"""Return the integer representation of the object by calling the toint() method.
"""
return self.toint()
def __lt__(self, other):
"""Return self.toint() < other.toint()
"""
return self.toint() < other.toint()
def __le__(self, other):
"""Return self.toint() <= other.toint()
"""
return self.toint() <= other.toint()
def __eq__(self, other):
"""Return self.toint() == other.toint()
"""
return self.toint() == other.toint()
def __ne__(self, other):
"""Return self.toint() != other.toint()
"""
return self.toint() != other.toint()
def __gt__(self, other):
"""Return self.toint() > other.toint()
"""
return self.toint() > other.toint()
def __ge__(self, other):
"""Return self.toint() >= other.toint()
"""
return self.toint() >= other.toint()
def __hash__(self):
"""Return the hash of the object.
"""
return self.toint()
def __add__(self, other: int):
"""Return a new Address object with the offset increased by the given integer.
"""
return Address(self._type, self._offset + other)
def __sub__(self, other: int):
""" Return a new Address object with the offset decreased by the given integer.
"""
return Address(self._type, self._offset - other)
def __iadd__(self, other: int):
"""Increase the offset by the given integer.
"""
self.setoffset(self._offset + other)
return self
def __isub__(self, other: int):
"""Decrease the offset by the given integer.
"""
self.setoffset(self._offset - other)
return self
def __repr__(self):
"""Return the string representation of the object.
"""
return self.tostr(Notation_Default)
def __str__(self):
"""Return the string representation of the object.
"""
return self.tostr(Notation_Default)