serialhdl: Retry opening of serial port

Continually retry to open the serial port.  This helps with connecting
to some micro-controllers.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2016-11-29 17:44:37 -05:00
parent 0d43d269ed
commit 55fc11ff02
1 changed files with 27 additions and 10 deletions

View File

@ -59,14 +59,29 @@ class SerialReader:
def connect(self): def connect(self):
# Initial connection # Initial connection
logging.info("Starting serial connect") logging.info("Starting serial connect")
self.ser = serial.Serial(self.serialport, self.baud, timeout=0) while 1:
stk500v2_leave(self.ser, self.reactor) starttime = time.time()
self.serialqueue = self.ffi_lib.serialqueue_alloc(self.ser.fileno(), 0) try:
self.background_thread = threading.Thread(target=self._bg_thread) self.ser = serial.Serial(self.serialport, self.baud, timeout=0)
self.background_thread.start() except OSError, e:
# Obtain and load the data dictionary from the firmware logging.warn("Unable to open port: %s" % (e,))
sbs = SerialBootStrap(self) self.reactor.pause(starttime + 5.)
identify_data = sbs.get_identify_data() continue
stk500v2_leave(self.ser, self.reactor)
self.serialqueue = self.ffi_lib.serialqueue_alloc(
self.ser.fileno(), 0)
self.background_thread = threading.Thread(target=self._bg_thread)
self.background_thread.start()
# Obtain and load the data dictionary from the firmware
sbs = SerialBootStrap(self)
identify_data = sbs.get_identify_data(starttime + 5.)
if identify_data is None:
logging.warn("Timeout on serial connect")
self.disconnect()
self.ser.close()
self.ser = None
continue
break
msgparser = msgproto.MessageParser() msgparser = msgproto.MessageParser()
msgparser.process_identify(identify_data) msgparser.process_identify(identify_data)
self.msgparser = msgparser self.msgparser = msgparser
@ -250,12 +265,14 @@ class SerialBootStrap:
self.serial.register_callback(self.handle_unknown, '#unknown') self.serial.register_callback(self.handle_unknown, '#unknown')
self.send_timer = self.serial.reactor.register_timer( self.send_timer = self.serial.reactor.register_timer(
self.send_event, self.serial.reactor.NOW) self.send_event, self.serial.reactor.NOW)
def get_identify_data(self): def get_identify_data(self, timeout):
eventtime = time.time() eventtime = time.time()
while not self.is_done: while not self.is_done and eventtime <= timeout:
eventtime = self.serial.reactor.pause(eventtime + 0.05) eventtime = self.serial.reactor.pause(eventtime + 0.05)
self.serial.unregister_callback('identify_response') self.serial.unregister_callback('identify_response')
self.serial.reactor.unregister_timer(self.send_timer) self.serial.reactor.unregister_timer(self.send_timer)
if not self.is_done:
return None
return self.identify_data return self.identify_data
def handle_identify(self, params): def handle_identify(self, params):
if self.is_done or params['offset'] != len(self.identify_data): if self.is_done or params['offset'] != len(self.identify_data):