-
Notifications
You must be signed in to change notification settings - Fork 2
/
PassiveModbusMonitor.py
331 lines (310 loc) · 14.8 KB
/
PassiveModbusMonitor.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Passive Modbus RTU monitor
# Programmed by Entropia
# Inspired by Dala's EV Repair
import serial
import struct
import datetime
def HexToStr(input):
return hex(ord(input))[2:]
def IntIEEE754Decode(input):
packed_v = struct.pack('>l', input)
return struct.unpack('>f', packed_v)[0]
# Combine two hex strings to one 16-bit int
def Combine(input1, input2):
temp = int(input1, base=16) << 8
temp += int(input2, base=16)
return temp
# Combine four hex strings to one 32-bit int
def Combine4(input1, input2, input3, input4):
temp = int(input1, base=16) << 24
temp += int(input2, base=16) << 16
temp += int(input3, base=16) << 8
temp += int(input4, base=16)
return temp
def tsprint(input):
timestamp = datetime.datetime.now()
print("[" + timestamp + "] " + input)
# All the magic happens here
def CheckModbusFunction(rxbuffer, nextframe, quantity):
# Uncomment for debugging
#print("Len:", len(rxbuffer), "Nextframe:", nextframe, "Quantity:", quantity, "Byte:", rxbuffer[len(rxbuffer) - 1])
try:
if(nextframe == 0 and len(rxbuffer) == 7):
# Listening for known master requests (0x0F and 0x10)
if(rxbuffer[1] == 'f'):
# Write Multiple Coils
deviceID = rxbuffer[0]
address = int(rxbuffer[2], base=16) << 8
address += int(rxbuffer[3], base=16)
quantity = int(rxbuffer[4], base=16) << 8
quantity += int(rxbuffer[5], base=16)
timestamp = datetime.datetime.now()
print(timestamp, "Write multiple coils for device ID ", deviceID, ", Start address =", address, "Quantity =", quantity)
if(quantity < 8):
quantity = 1
else:
if(quantity % 8 == 0):
quantity /= 8
else:
quantity /= 8
quantity += 1
return (True, 15, int(quantity))
elif(rxbuffer[1] == '10'):
# Write Multiple Registers
deviceID = rxbuffer[0]
address = int(rxbuffer[2], base=16) << 8
address += int(rxbuffer[3], base=16)
quantity = int(rxbuffer[4], base=16) << 8
quantity += int(rxbuffer[5], base=16)
timestamp = datetime.datetime.now()
print(timestamp, "Write multiple registers for device ID ", deviceID, ", Start address =", address, "Quantity =", quantity)
quantity *= 2
return (True, 16, int(quantity))
else:
return (False, nextframe, quantity)
elif(nextframe == 0 and len(rxbuffer) == 8):
# Listening for known master requests (0x01 thru 0x06)
if(rxbuffer[1] == '1'):
# Read Coils
deviceID = rxbuffer[0]
address = int(rxbuffer[2], base=16) << 8
address += int(rxbuffer[3], base=16)
quantity = int(rxbuffer[4], base=16) << 8
quantity += int(rxbuffer[5], base=16)
timestamp = datetime.datetime.now()
print(timestamp, "Read coils for device ID ", deviceID, ", Start address =", address, "Quantity =", quantity)
if(quantity < 8):
quantity = 1
else:
if(quantity % 8 == 0):
quantity /= 8
else:
quantity /= 8
quantity += 1
return (True, 1, int(quantity))
elif(rxbuffer[1] == '2'):
# Read Discrete Inputs
deviceID = rxbuffer[0]
address = int(rxbuffer[2], base=16) << 8
address += int(rxbuffer[3], base=16)
quantity = int(rxbuffer[4], base=16) << 8
quantity += int(rxbuffer[5], base=16)
timestamp = datetime.datetime.now()
print(timestamp, "Read discrete inputs for device ID", deviceID, ", Start address =", address, "Quantity =", quantity)
if(quantity < 8):
quantity = 1
else:
if(quantity % 8 == 0):
quantity /= 8
else:
quantity /= 8
quantity += 1
return (True, 2, int(quantity))
elif(rxbuffer[1] == '3'):
# Read Holding Register
deviceID = rxbuffer[0]
address = int(rxbuffer[2], base=16) << 8
address += int(rxbuffer[3], base=16)
quantity = int(rxbuffer[4], base=16) << 8
quantity += int(rxbuffer[5], base=16)
timestamp = datetime.datetime.now()
print(timestamp, "Read holding register for device ID", deviceID, ", Start address =", address, "Quantity =", quantity)
return (True, 3, quantity)
elif(rxbuffer[1] == '4'):
# Read Input Register
deviceID = rxbuffer[0]
address = Combine(rxbuffer[2], rxbuffer[3])
quantity = Combine(rxbuffer[4], rxbuffer[5])
timestamp = datetime.datetime.now()
print(timestamp, "Read input register for device ID", deviceID, ", Start address =", address, "Quantity =", quantity)
return (True, 4, quantity)
elif(rxbuffer[1] == '5'):
# Write Single Coil
deviceID = rxbuffer[0]
address = Combine(rxbuffer[2], rxbuffer[3])
quantity = Combine(rxbuffer[4], rxbuffer[5])
timestamp = datetime.datetime.now()
print(timestamp, "Write single coil for device ID", deviceID, ", Start address =", address, "Quantity =", quantity)
return (True, 5, quantity)
elif(rxbuffer[1] == '6'):
# Write Single Register
deviceID = rxbuffer[0]
address = Combine(rxbuffer[2], rxbuffer[3])
quantity = Combine(rxbuffer[4], rxbuffer[5])
timestamp = datetime.datetime.now()
print(timestamp, "Write single register for device ID", deviceID, ", Start address =", address, "Quantity =", quantity)
return (True, 6, quantity)
else:
return (False, nextframe, quantity)
elif(nextframe == 1 and len(rxbuffer) == 5):
# Exception for function code 1 (0x01)
if(rxbuffer[1] == '81'):
timestamp = datetime.datetime.now()
if(rxbuffer[2] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[2])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 1 and len(rxbuffer) == (5 + quantity)):
# Expecting reply from device for function code 1 (0x01)
timestamp = datetime.datetime.now()
deviceID = rxbuffer[0]
for x in range(0, quantity):
data = rxbuffer[x + 3]
print(timestamp, " * Response", (x + 1), "from device ID", deviceID, ", data =", data)
return (True, 0, 0)
elif(nextframe == 2 and len(rxbuffer) == 5):
# Exception for function code 2 (0x02)
if(rxbuffer[1] == '82'):
timestamp = datetime.datetime.now()
if(rxbuffer[2] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[2])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 2 and len(rxbuffer) == (5 + quantity)):
# Expecting reply from device for function code 2 (0x02)
timestamp = datetime.datetime.now()
deviceID = rxbuffer[0]
for x in range(0, quantity):
data = rxbuffer[x + 3]
print(timestamp, " * Response", (x + 1), "from device ID", deviceID, ", data =", data)
return (True, 0, 0)
elif(nextframe == 3 and len(rxbuffer) == 5):
# Exception for function code 3 (0x03)
if(rxbuffer[1] == '83'):
timestamp = datetime.datetime.now()
if(rxbuffer[2] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[2])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 3 and len(rxbuffer) == (5 + (quantity * 2))):
# Expecting reply from device for function code 3 (0x03)
deviceID = rxbuffer[0]
timestamp = datetime.datetime.now()
for x in range(0, quantity):
data = Combine(rxbuffer[(x * 2) + 3], rxbuffer[(x * 2) + 4])
print(timestamp, " * Response", (x + 1), "from device ID", deviceID, ", data =", data)
if(quantity == 2):
print(timestamp, " * Float:", IntIEEE754Decode(Combine4(rxbuffer[3], rxbuffer[4], rxbuffer[5], rxbuffer[6])))
return (True, 0, 0)
elif(nextframe == 4 and len(rxbuffer) == 5):
# Exception for function code 4 (0x04)
if(rxbuffer[1] == '84'):
timestamp = datetime.datetime.now()
if(rxbuffer[2] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[2])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 4 and len(rxbuffer) == (5 + (quantity * 2))):
# Expecting reply from device for function code 4 (0x04)
deviceID = rxbuffer[0]
timestamp = datetime.datetime.now()
for x in range(0, quantity):
data = Combine(rxbuffer[(x * 2) + 3], rxbuffer[(x * 2) + 4])
print(timestamp, " * Response", (x + 1), "from device ID", deviceID, ", data =", data)
return (True, 0, 0)
elif(nextframe == 5 and len(rxbuffer) == 5):
# Exception for function code 5 (0x05)
if(rxbuffer[1] == '85'):
timestamp = datetime.datetime.now()
if(rxbuffer[2] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[2])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 5 and len(rxbuffer) == 8):
# Expecting reply from device for function code 5 (0x05)
deviceID = rxbuffer[0]
address = Combine(rxbuffer[2], rxbuffer[3])
data = Combine(rxbuffer[4], rxbuffer[5])
timestamp = datetime.datetime.now()
print(timestamp, " * Response from device ID", deviceID, " Address", address, "is now", data)
elif(nextframe == 6 and len(rxbuffer) == 5):
# Exception for function code 6 (0x06)
if(rxbuffer[1] == '86'):
timestamp = datetime.datetime.now()
if(rxbuffer[2] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[2])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 6 and len(rxbuffer) == 8):
# Expecting reply from device for function code 6 (0x06)
deviceID = rxbuffer[0]
address = Combine(rxbuffer[2], rxbuffer[3])
data = Combine(rxbuffer[4], rxbuffer[5])
timestamp = datetime.datetime.now()
print(timestamp, " * Response from device ID", deviceID, " Address", address, "is now", data)
elif(nextframe == 15 and len(rxbuffer) == (quantity + 7)):
# Exception for function code 15 (0x0F)
if(rxbuffer[quantity + 3] == '8f'):
timestamp = datetime.datetime.now()
if(rxbuffer[quantity + 4] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[quantity + 4])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 15 and len(rxbuffer) == (quantity + 3 + 7)):
# Output values are still being sent, also expecting device reply for function code 15 (0x0F)
deviceID = rxbuffer[quantity + 3]
address = Combine(rxbuffer[quantity + 4], rxbuffer[quantity + 5])
timestamp = datetime.datetime.now()
for x in range (0, quantity):
print(timestamp, " * Write command", x, ":", rxbuffer[x])
return (True, 0, 0)
elif(nextframe == 16 and len(rxbuffer) == (quantity + 7)):
# Exception for function code 16 (0x10)
if(rxbuffer[quantity + 3] == '90'):
timestamp = datetime.datetime.now()
if(rxbuffer[quantity + 4] == '2'):
print(timestamp, " * Illegal Data Address exception")
else:
print(timestamp, " * Exception! Code:", rxbuffer[quantity + 4])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
elif(nextframe == 16 and len(rxbuffer) == (quantity + 3 + 7)):
# Output values are still being sent, also expecting device reply for function code 16 (0x10)
deviceID = rxbuffer[quantity + 3]
address = Combine(rxbuffer[quantity + 4], rxbuffer[quantity + 5])
timestamp = datetime.datetime.now()
for x in range (0, quantity):
print(timestamp, " * Write command", x, ":", rxbuffer[x])
return (True, 0, 0)
else:
return (False, nextframe, quantity)
except:
return (False, nextframe, quantity)
# Open serial port
ser = serial.Serial('COM3', 9600)
# Set up variables for first time use
framefinished = False
rxbuffer = list()
nextframe = 0
quantity = 0
# This main loop receives requests and responses and decodes them
while True:
# Blocking loop for receiving frames
while not framefinished:
rxbuffer.append(HexToStr(ser.read(1)))
(framefinished, nextframe, quantity) = CheckModbusFunction(rxbuffer, nextframe, quantity)
# Succesfully received one frame, clear variables for next frame
rxbuffer = list()
framefinished = False