-
Notifications
You must be signed in to change notification settings - Fork 0
/
bconf-tool.py
473 lines (433 loc) · 17.4 KB
/
bconf-tool.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Ugly code following!
import knownendpoints
import abusliconf
import array
import serial
import argparse
import time
from pymodbus.pdu import ModbusRequest
from pymodbus.client import ModbusSerialClient as SerialModbusClient
from pymodbus.client import ModbusTcpClient as TCPModbusClient
from pymodbus.transaction import ModbusRtuFramer
#import logging
#logging.basicConfig()
##log = logging.getLogger()
#log.setLevel(logging.DEBUG)
baudrate = 38400
parity = 'N'
port = "/dev/ttyUSB0"
slave=0 # the slave save this request is targeting
IOcffromDevice = [False]*1024
buttonConf = [False]*16
oConfFromDevice = [False]*32
timerOConfFromDevice = [False]*32
cmdRegisters = [0]*5
timeoutvalsFromDevice = [0]*16
outDefaultsFromDevice = [False]*16
patternSavingFromDeviceShortPush = [False]*16
patternSavingFromDeviceLongPush = [False]*16
fastPwmEnableFromDevice = [False]*16
ultraSlowPwmEnableFromDevice = [False]*16
description = ""
debouncetimeFromDevice = 0
global version
SwVersions = knownendpoints.SwVersions
BoardTypes = knownendpoints.BoardTypes
client = SerialModbusClient(method = "rtu", port = port, stopbits = 1, bytesize = 8, parity = parity, baudrate = baudrate, timeout=1.0)
try:
connection = client.connect()
except:
print("Cannot open serial port. Is "+port+" available?")
exit()
def checkinsanity():
if abusliconf.debouncetime<8 or abusliconf.debouncetime>25:
print("ERROR! Debounce time illegal. Must be 7 < debouncetime < 26.")
return True
if (abusliconf.debouncetime==25 and abusliconf.longPushThr!=30) or (abusliconf.debouncetime==8 and abusliconf.longPushThr!=100) or (abusliconf.debouncetime!=8 and abusliconf.debouncetime!=25):
print("WARNING: The debounce time value and the longpush threshold value must fit each other. Well tested working values are:")
print("debounce time = 8, longpush thr = 100")
print("debounce time = 25, longpush thr = 30")
print("From firmware version 22 onward 8/100 is the new standard. If you change these settings and your inputs don't work it's not my problem..")
return False
def getFeatures():
version = getFwVersion()
if(version >= 60000): #device with fw version 15+ => "extended fw/hw identifiers"
try:
result = client.read_holding_registers(10000,3,slave=save)
version=int(result.registers[1])
devType=int(result.registers[2])
print("Firmware-Version: "+str(version))
if(version>((len(SwVersions)))):
print("THIS VERSION OF BCONF-TOOL SUPPORTS DEVICES UP TO FIRMWARE VERSION "+str(len(SwVersions))+"!")
print("THIS DEVICE HAS FIRMWARE VERSION "+str(version)+"! UPDATE BCONF-TOOL!")
if(devType>(len(BoardTypes)-1)):
print("THIS VERSION OF BCONF-TOOL DOES NOT SUPPORT THIS DEVICE! UPDATE BCONF-TOOL!")
print("Hardware/Board-Type: "+BoardTypes[devType])
except:
print("Cannot read extended fw/hw identifier.")
version=0
elif(version>50000 and version<60000):
print("Device type: 1TE")
version=version-50000
elif(version>40000):
version=version-40000
print("SPECIAL device. Custom HW and/or software!")
elif(version>30000):
version=version-30000
print("Device type is: Li")
elif(version>20000):
version=version-20000
print("Device type is: Hut")
else:
if(version>10000):
version=version-10000
print("Device type is: wbcv2")
else:
print("Unknown device!")
version=0
if(version<(len(SwVersions)-1)):
print("/****************************************/")
print("LEGACY FIRMWARE! Update device's firmware!")
print("/****************************************/")
print("The following features won't be available:")
for x in range((version+1),len(SwVersions)):
print(" "+SwVersions[x])
return version
def probe():
try:
result0 = client.read_coils(2000,1,slave=save)
except Exception as e:
print(e)
print("Cannot open serial port. Is "+port+" available?")
exit()
return False
try:
print(result0.string)
except:
print("Client "+str(save)+" available.")
return True
else:
print("Client "+str(save)+" unreachable!")
exit()
return False
def getFwVersion():
try:
result = client.read_holding_registers(10000,1,slave=save)
vers=int(result.registers[0])
except:
print("Cannot read FW-Version. Maybe legacy or incompatible device?")
vers=0
finally:
return vers
def readConfs():
result0 = client.read_coils(2000,512,slave=save)
result1 = client.read_coils(2512,512,slave=save)
result2 = client.read_coils(3024,32,slave=save)
result3 = client.read_coils(3056,16,slave=save)
if version>11:
result4 = client.read_coils(3120,32,slave=save)
for x in range(0, 16):
patternSavingFromDeviceShortPush[x]=result4.bits[x]
for x in range(0, 16):
patternSavingFromDeviceLongPush[x]=result4.bits[x+16]
if version < 9:
result8 = client.read_holding_registers(2000,3,slave=save)
else:
result8 = client.read_holding_registers(2000,5,slave=save)
if(version > 2):
result5 = client.read_coils(3072,32,slave=save)
result6 = client.read_holding_registers(4000,16,slave=save)
result7 = client.read_coils(3104,16,slave=save)
for x in range(0, 32):
timerOConfFromDevice[x]=result5.bits[x]
for x in range(0, 16):
timeoutvalsFromDevice[x]=int(result6.registers[x])
for x in range(0, 16):
outDefaultsFromDevice[x]=result7.bits[x]
if version > 9:
resultDescr = client.read_holding_registers(4016,8,slave=save)
global description
description=""
for x in range(0, 8):
description=description+str(chr(resultDescr.registers[x] & 0xFF))
description=description+str(chr((resultDescr.registers[x] & 0xFF00)>>8))
description=description.rstrip(chr(0x00))
cmdRegisters[1]=int(result8.registers[1])
cmdRegisters[2]=int(result8.registers[2])
if version > 8:
cmdRegisters[3]=int(result8.registers[3])
for x in range(0, 512):
IOcffromDevice[x] = result0.bits[x]
IOcffromDevice[x+512] = result1.bits[x]
for x in range(0, 16):
buttonConf[x]=result3.bits[x]
for x in range(0, 32):
oConfFromDevice[x]=result2.bits[x]
if version > 21:
global debouncetimeFromDevice
debouncetimeFromDevice = int(client.read_holding_registers(4024,1,slave=save).registers[0])
if version > 22:
global fastPwmEnableFromDevice
global ultraSlowPwmEnableFromDevice
resPwm = client.read_coils(3152,32,slave=save)
for x in range(0,16):
fastPwmEnableFromDevice[x]=resPwm.bits[x]
for x in range(0,16):
ultraSlowPwmEnableFromDevice[x]=resPwm.bits[x+16]
def compare():
readConfs()
print("Comparing.")
abusliconf.readConfFromFile()
if len(abusliconf.description)>16:
print("ERROR! Description in config file too long! Maximum is 16 characters!")
client.close()
exit()
testResult = 0
if(IOcffromDevice==abusliconf.ioConf):
pass#print("input-output-assignments match.")
else:
testResult=1
print("input-output-assignments dont match!")
if(buttonConf==abusliconf.buttonConf):
pass#print("button configuration matches")
else:
testResult=1
print("button configuration doesn't match!")
if(oConfFromDevice==abusliconf.oConf):
pass#print("output configuration matches.")
else:
testResult=1
print("output configuration doesn't match!")
if version > 21:
if abusliconf.longPushThr==9999:
abusliconf.longPushThr=100
if abusliconf.longPushThr!=9999:
if(abusliconf.longPushThr==cmdRegisters[1]):
pass#print("longpush threshold matches. "+"("+str(cmdRegisters[1])+")")
else:
testResult=1
print("longpush threshold doesn't match!")
if(abusliconf.timeoutThr==cmdRegisters[2]):
pass#print("bus timeout threshold matches. "+"("+str(cmdRegisters[2])+")")
else:
print("bus timeout threshold doesn't match!")
testResult=1
if version > 8:
if(abusliconf.brownoutThr==cmdRegisters[3]):
pass#print("brownout threshold matches. "+"("+str(cmdRegisters[3])+")")
else:
print("brownout threshold doesn't match!")
testResult=1
if(version>2):
if(timerOConfFromDevice==abusliconf.timerOConf):
pass#print("timer output configuration matches.")
else:
testResult=1
print("timer output configuration doesn't match!")
if(abusliconf.timervals==timeoutvalsFromDevice):
pass#print("output timeout values match.")
else:
print("output timeout values doesn't match!")
testResult=1
if(outDefaultsFromDevice==abusliconf.outDefaults):
pass#print("output default states match.")
else:
print("output default states don't match!")
testResult=1
if(version>9):
if(repr(abusliconf.description)==repr(description)):
if len(description)>0:
print("Description: "+"("+description+")")
else:
print("description doesn't match!")
testResult=1
if version > 11:
if patternSavingFromDeviceShortPush==abusliconf.patternSavingShort:
pass
else:
print("pattern saving (short push) control bits don't match")
testResult=1
if patternSavingFromDeviceLongPush==abusliconf.patternSavingLong:
pass
else:
testResult=1
print("pattern saving (long push) control bits don't match")
if version > 21:
if abusliconf.debouncetime==9999:
abusliconf.debouncetime=8
if debouncetimeFromDevice==abusliconf.debouncetime:
pass
else:
testResult=1
print("debounce time does not match")
if version > 22:
if fastPwmEnableFromDevice==abusliconf.fastPwmEnable:
pass
else:
print("fast pwm settings don't match")
testResult=1
if ultraSlowPwmEnableFromDevice==abusliconf.ultraSlowPwmEnable:
pass
else:
print("ultra slow pwm settings don't match")
testResult=1
if testResult == 1:
print("ERROR: The device's configuration doesn't match the config file!")
return False
else:
return True
def store():
try:
result4 = client.write_register(2000,17239,slave=save)
print("Store: ok")
except:
print("Modbus error. Store failed.")
def loadEEPROMcontent():
print("Trying to load config from EEPROM.")
try:
result4 = client.write_register(2000,17234,slave=save)
print("Loading from EEPROM initiated.")
except:
print("Modbus error. Loading from EEPROM failed.")
def upload():
abusliconf.readConfFromFile()
if len(abusliconf.description)>16:
print("ERROR! Description in config file too long! Maximum is 16 characters!")
client.close()
exit()
if version>21:
if abusliconf.debouncetime==9999:
abusliconf.debouncetime=8
print("INFO: Setting debouncetime to default value: 8")
if abusliconf.longPushThr==9999:
abusliconf.longPushThr=100
print("INFO: Setting longpush threshold to default value: 100")
if checkinsanity():
client.close()
exit()
else:
if abusliconf.longPushThr==9999:
abusliconf.longPushThr=30
print("INFO: Setting longpush threshold to default value: 100")
try:
result0 = client.write_coils(2000,abusliconf.ioConf[0:512],slave=save)
result0 = client.write_coils(2512,abusliconf.ioConf[512:1024],slave=save)
result2 = client.write_coils(3024,abusliconf.oConf,slave=save)
if(version>2):
result5 = client.write_coils(3072,abusliconf.timerOConf,slave=save)
result7 = client.write_coils(3104,abusliconf.outDefaults, slave=save)
result6 = client.write_registers(4000,abusliconf.timervals,slave=save)
result3 = client.write_coils(3056,abusliconf.buttonConf,slave=save)
result4 = client.write_register(2001,abusliconf.longPushThr,slave=save)
result4 = client.write_register(2002,abusliconf.timeoutThr,slave=save)
if version > 8:
result4 = client.write_register(2003,abusliconf.brownoutThr,slave=save)
if version > 9:
descrAsInts = [0]*8
for x in range(0, 8):
if (x*2)<len(abusliconf.description):
descrAsInts[x]=int(ord(abusliconf.description[x*2]))
if (x*2+1)<len(abusliconf.description):
descrAsInts[x]=int(descrAsInts[x]|(ord(abusliconf.description[x*2+1])<<8))
result5 = client.write_registers(4016,descrAsInts,slave=save)
if version > 11:
result5 = client.write_coils(3120,abusliconf.patternSavingShort,slave=save)
result5 = client.write_coils(3136,abusliconf.patternSavingLong,slave=save)
if version > 21:
result5 = client.write_register(4024,abusliconf.debouncetime,slave=save)
if version > 22:
result5 = client.write_coils(3152,abusliconf.fastPwmEnable,slave=save)
result5 = client.write_coils(3168,abusliconf.ultraSlowPwmEnable,slave=save)
print("Upload done.")
except:
print("Modbus error during upload.")
parser = argparse.ArgumentParser()
parser.add_argument("command", type=str, help="command to perform. Allowed commands: upload, download, store, compare, eeprom-download")
parser.add_argument("address", type=int, help="device address")
parser.add_argument("file", type=str, help="config file source/destination")
args = parser.parse_args()
save = args.address
abusliconf.filename = args.file
if (args.command == "download"):
if probe():
version=getFeatures()
readConfs()
abusliconf.buttonConf=buttonConf
abusliconf.ioConf=IOcffromDevice
abusliconf.oConf=oConfFromDevice
abusliconf.timerOConf=timerOConfFromDevice
abusliconf.longPushThr=cmdRegisters[1]
abusliconf.timeoutThr=cmdRegisters[2]
abusliconf.patternSavingLong=patternSavingFromDeviceLongPush
abusliconf.patternSavingShort=patternSavingFromDeviceShortPush
if version > 8:
abusliconf.brownoutThr=cmdRegisters[3]
if version > 9:
abusliconf.description=description
abusliconf.timervals=timeoutvalsFromDevice
abusliconf.outDefaults=outDefaultsFromDevice
if version > 21:
abusliconf.debouncetime=debouncetimeFromDevice
if version > 22:
abusliconf.ultraSlowPwmEnable=ultraSlowPwmEnableFromDevice
abusliconf.fastPwmEnable=fastPwmEnableFromDevice
abusliconf.writeConfToFile()
print ("Written to file: "+abusliconf.filename)
if(compare()):
print("Verification: pass")
elif (args.command == "compare"):
if probe():
version=getFeatures()
if compare():
print("Verification: pass")
elif(args.command == "eeprom-download"):
if probe():
version=getFeatures()
question=str(input('Warning! Configuration stored in RAM will be overwritten with EEPROM content. Continue? (y/n)'))
if(question=='y'):
print("Okay.")
loadEEPROMcontent()
time.sleep(2)
readConfs()
abusliconf.buttonConf=buttonConf
abusliconf.ioConf=IOcffromDevice
abusliconf.oConf=oConfFromDevice
abusliconf.timerOConf=timerOConfFromDevice
abusliconf.longPushThr=cmdRegisters[1]
abusliconf.timeoutThr=cmdRegisters[2]
abusliconf.patternSavingLong=patternSavingFromDeviceLongPush
abusliconf.patternSavingShort=patternSavingFromDeviceShortPush
if version >8:
abusliconf.brownoutThr=cmdRegisters[3]
abusliconf.timervals=timeoutvalsFromDevice
abusliconf.outDefaults=outDefaultsFromDevice
if version > 9:
abusliconf.description=description
if version > 21:
abusliconf.debouncetime=debouncetimeFromDevice
if version > 22:
abusliconf.ultraSlowPwmEnable=ultraSlowPwmEnableFromDevice
abusliconf.fastPwmEnable=fastPwmEnableFromDevice
abusliconf.writeConfToFile()
print ("Writing to file: "+abusliconf.filename)
if(compare()):
print("Verification: pass")
else:
print("Aborting. Configuration in RAM unchanged. Not written to file "+abusliconf.filename+".")
elif (args.command == "upload"):
if probe():
version=getFeatures()
upload()
if(compare()):
print("Verification: pass")
elif (args.command == "store"):
if probe():
version=getFeatures()
if(compare()):
print("Verification: pass")
store()
else:
print("Cannot write to EEPROM!")
else:
print("Invalid command. Allowed commands: upload, download, store, compare, eeprom-download")
client.close()