Browse Source

fix thread exit

try-fix-comms-errors
Alex Mikhalev 6 years ago
parent
commit
95ea8a4155
  1. 11
      e32_client/ugv.py

11
e32_client/ugv.py

@ -25,8 +25,13 @@ class UGVComms(E32):
def read_message(self): def read_message(self):
len_data = self.ser.read(size=1) len_data = self.ser.read(size=1)
if len(len_data) != 1:
return None
msg_len = int.from_bytes(len_data, byteorder='big') msg_len = int.from_bytes(len_data, byteorder='big')
data = self.ser.read(size=msg_len) data = self.ser.read(size=msg_len)
if len(data) != msg_len:
self.ser.flush()
return None
msg = messages.UGV_Message() msg = messages.UGV_Message()
msg.ParseFromString(data) msg.ParseFromString(data)
return msg return msg
@ -36,7 +41,8 @@ def __rx_thread_entry(ugv: UGVComms):
while True: while True:
try: try:
msg = ugv.read_message() msg = ugv.read_message()
print("received UGV message: ", msg) if msg is not None:
print("received UGV message: ", msg)
except Exception as e: except Exception as e:
print("error reading message: ", e) print("error reading message: ", e)
continue continue
@ -47,7 +53,7 @@ if __name__ == "__main__":
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS,
timeout=2.0) timeout=2.0)
ugv = UGVComms(ser) ugv = UGVComms(ser)
rx_thread = Thread(target=__rx_thread_entry, args=(ugv, )) rx_thread = Thread(target=__rx_thread_entry, args=(ugv, ), daemon=True)
rx_thread.start() rx_thread.start()
# print("resetting") # print("resetting")
# ugv.reset() # ugv.reset()
@ -62,5 +68,6 @@ if __name__ == "__main__":
ugv.write_message(gmsg) ugv.write_message(gmsg)
time.sleep(2.) time.sleep(2.)
rx_thread.In
rx_thread.join() rx_thread.join()

Loading…
Cancel
Save