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.
This commit is contained in:
Andy Shaw committed 2019-01-15 19:33:51 +00:00
1 parent d452051c0a
commit 7b5dbef166
1 file changed
+7 -2
+7 -2
View File
@@ -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