From d7522de25abcc0e40a9cae4291f73d93a0280bc4 Mon Sep 17 00:00:00 2001 From: misdoro Date: Tue, 26 Jan 2021 21:49:41 +0100 Subject: [PATCH] Do not crash on losing USB connectivity --- instruments/px100.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/instruments/px100.py b/instruments/px100.py index c4886c8..c0c8e6c 100644 --- a/instruments/px100.py +++ b/instruments/px100.py @@ -200,13 +200,14 @@ def setVal(self, command, value): return ret == 0x6F def writeFunction(self, command, value): - frame = bytearray([0xB1, 0xB2, command, *value, 0xB6]) - self.device.write_raw(frame) if command >= 0x10: resp_len = 7 else: resp_len = 1 + + frame = bytearray([0xB1, 0xB2, command, *value, 0xB6]) try: + self.device.write_raw(frame) return self.device.read_bytes(resp_len) except Exception as inst: print(type(inst)) # the exception instance @@ -236,7 +237,15 @@ def __setup_device(self): pass def __clear_device(self): - self.device.read_bytes(self.device.bytes_in_buffer) + try: + self.device.read_bytes(self.device.bytes_in_buffer) + except Exception as inst: + print(type(inst)) # the exception instance + print(inst.args) # arguments stored in .args + print(inst) + print("error reading bytes") + self.device.close + return False def __next_aux(self): self.aux_index += 1