Catch struct unpack error

This commit is contained in:
Odd Stråbø 2021-11-08 04:32:49 +01:00
parent 5e87e3edd6
commit 6cbf480307
1 changed files with 12 additions and 6 deletions

View File

@ -252,8 +252,8 @@ def construct_request(address, words=1, action=ACTION_READ, marker=0xFF):
return struct.pack("!BBHH", marker, action, address, words)
def log(message: object):
print(datetime.datetime.utcnow().isoformat(" "), message)
def log(*message: object):
print(datetime.datetime.utcnow().isoformat(" "), *message)
sys.stdout.flush()
@ -346,13 +346,19 @@ if __name__ == "__main__":
# CMD_GET_BATTERY_STATE + CMD_GET_PANEL_STATUS
res = readMemory(dev, 0x0100, 11)
if res:
d = parse_battery_state(res)
log(d)
try:
d = parse_battery_state(res)
log(d)
except struct.error:
log("0x0100 Unpack error:", res)
if per_current_hist(now):
res = readMemory(dev, 0x010B, 21)
if res:
d = parse_historical_entry(res)
log(d)
try:
d = parse_historical_entry(res)
log(d)
except struct.error:
log("0x010B Unpack error:", res)
# print(".")
time.sleep(1)