Skip to content

Commit

Permalink
Do not crash on losing USB connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
misdoro committed Jan 26, 2021
1 parent 9116944 commit d7522de
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions instruments/px100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d7522de

Please sign in to comment.