Improve synchronization and timeout handling

Make sure we can safely sync to the status packet in case
garbage is received before.
This commit is contained in:
Grigori Goronzy 2014-01-21 16:03:38 +01:00
parent 5bcad70d77
commit 87a257e63e

View File

@ -1059,7 +1059,7 @@ class Stc12Protocol:
data = self.ser.read(num) data = self.ser.read(num)
if len(data) != num: if len(data) != num:
raise serial.SerialException("read timeout") raise serial.SerialTimeoutException("read timeout")
return data return data
@ -1074,9 +1074,11 @@ class Stc12Protocol:
# read and check frame start magic # read and check frame start magic
packet = bytes() packet = bytes()
packet += self.read_bytes_safe(2) packet += self.read_bytes_safe(1)
if packet[0:2] != self.PACKET_START: if packet[0] != self.PACKET_START[0]:
self.dump_packet(packet) raise StcFramingException("incorrect frame start")
packet += self.read_bytes_safe(1)
if packet[1] != self.PACKET_START[1]:
raise StcFramingException("incorrect frame start") raise StcFramingException("incorrect frame start")
# read direction and length # read direction and length
@ -1244,7 +1246,7 @@ class Stc12Protocol:
try: try:
self.pulse() self.pulse()
status_packet = self.get_status_packet() status_packet = self.get_status_packet()
except (StcFramingException, serial.SerialException): pass except (StcFramingException, serial.SerialTimeoutException): pass
print("done") print("done")
self.initialize_status(status_packet) self.initialize_status(status_packet)