aboutsummaryrefslogtreecommitdiffstats
path: root/klippy
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2016-11-29 17:44:37 -0500
committerKevin O'Connor <kevin@koconnor.net>2016-11-29 21:58:48 -0500
commit55fc11ff024958bb86331bfafc5abc11a82a48fe (patch)
tree7f417c51a09a220948a6edd4685c61d726306d52 /klippy
parent0d43d269ede949fac1e4b5bb125514a240ec3efe (diff)
downloadkutter-55fc11ff024958bb86331bfafc5abc11a82a48fe.tar.gz
kutter-55fc11ff024958bb86331bfafc5abc11a82a48fe.tar.xz
kutter-55fc11ff024958bb86331bfafc5abc11a82a48fe.zip
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>
Diffstat (limited to 'klippy')
-rw-r--r--klippy/serialhdl.py37
1 files changed, 27 insertions, 10 deletions
diff --git a/klippy/serialhdl.py b/klippy/serialhdl.py
index 28c83b16..32b80170 100644
--- a/klippy/serialhdl.py
+++ b/klippy/serialhdl.py
@@ -59,14 +59,29 @@ class SerialReader:
def connect(self):
# Initial connection
logging.info("Starting serial connect")
- self.ser = serial.Serial(self.serialport, self.baud, timeout=0)
- 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()
+ while 1:
+ starttime = time.time()
+ try:
+ self.ser = serial.Serial(self.serialport, self.baud, timeout=0)
+ except OSError, e:
+ logging.warn("Unable to open port: %s" % (e,))
+ self.reactor.pause(starttime + 5.)
+ 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.process_identify(identify_data)
self.msgparser = msgparser
@@ -250,12 +265,14 @@ class SerialBootStrap:
self.serial.register_callback(self.handle_unknown, '#unknown')
self.send_timer = self.serial.reactor.register_timer(
self.send_event, self.serial.reactor.NOW)
- def get_identify_data(self):
+ def get_identify_data(self, timeout):
eventtime = time.time()
- while not self.is_done:
+ while not self.is_done and eventtime <= timeout:
eventtime = self.serial.reactor.pause(eventtime + 0.05)
self.serial.unregister_callback('identify_response')
self.serial.reactor.unregister_timer(self.send_timer)
+ if not self.is_done:
+ return None
return self.identify_data
def handle_identify(self, params):
if self.is_done or params['offset'] != len(self.identify_data):