From 7b5dbef166292b4619ff4f7865df97cc57e50dd1 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Sun, 13 Jan 2019 21:53:33 +0000 Subject: [PATCH] Use non blocking I/O for writes to implement write timeout Currently write timeouts are not really implemented (as they are set to 10000 seconds). The current code also has the unwanted side effect of casuing the use of a select call (in the Linux PySerial library) even when a write does not need to block. This call to select can in some odd cases cause the write to block for the duration of the set timeout which in turn triggers a transmission error. See issue https://github.com/foosel/OctoPrint/issues/2996 This change addresses both issues by allowing the use of the existing write retry code to handle timeouts and by using non-blocking write mode to avoid the use of select. --- src/octoprint/util/comm.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index a9be8f29c..e05c781c8 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -2475,13 +2475,13 @@ class MachineCom(object): serial_obj = serial.Serial(str(port), baudrates[0], timeout=read_timeout, - write_timeout=10000, + write_timeout=0, parity=serial.PARITY_ODD) else: serial_obj = serial.Serial(str(port), baudrate, timeout=read_timeout, - write_timeout=10000, + write_timeout=0, parity=serial.PARITY_ODD) serial_obj.close() serial_obj.parity = serial.PARITY_NONE @@ -3288,6 +3288,11 @@ class MachineCom(object): self._errorValue = "Could not write to serial port" self.close(is_error=True) break + # if we have failed to write data after an initial retry then the printer/system + # may be busy, so give things a little time before we try again. Extend this + # period each time we fail until either we write the data or run out of retry attempts. + if passes > 1: + sleep((passes-1)/10.0) ##~~ command handlers