From 112bb260acb8655e929cf377b18f505d62608eb1 Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Sun, 18 Feb 2018 11:44:04 +0100 Subject: [PATCH 1/7] Do not send new G-CODEs when the RX buffer is filling up. This is in an attempt to fix #1777 where smoothieware based printers would halt in the middle of the print. --- plugins/USBPrinting/USBPrinterOutputDevice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index b53f502d81..c177ad64d6 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -304,7 +304,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self._sendCommand(self._command_queue.get()) elif self._paused: pass # Nothing to do! - else: + elif self._serial.in_waiting < 256: # Do not send new G-CODE when the RX buffer is filling up self._sendNextGcodeLine() elif b"resend" in line.lower() or b"rs" in line: # A resend can be requested either by Resend, resend or rs. From d62dbe51876da7c8715b54ec1b30774914a782fa Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Fri, 23 Feb 2018 16:12:01 +0100 Subject: [PATCH 2/7] Stop sending empty commands as this confuses the communication with e.g. Smoothieware. See PR #3346 and bug #1777 --- plugins/USBPrinting/USBPrinterOutputDevice.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index c177ad64d6..a25bfcb5a2 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -266,7 +266,6 @@ class USBPrinterOutputDevice(PrinterOutputDevice): command = (command + "\n").encode() if not command.endswith(b"\n"): command += b"\n" - self._serial.write(b"\n") self._serial.write(command) def _update(self): From 946a09eb62e1bbf3fe623e47da2de61de3686d37 Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Fri, 23 Feb 2018 16:14:57 +0100 Subject: [PATCH 3/7] Parse heatbed temperatures even if no extruder temperature is sent. Attempts to fix bug #3332 --- plugins/USBPrinting/USBPrinterOutputDevice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index a25bfcb5a2..e34736c241 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -280,7 +280,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self.sendCommand("M105") self._last_temperature_request = time() - if b"ok T:" in line or line.startswith(b"T:"): # Temperature message + if b"ok T:" in line or line.startswith(b"T:") or b"ok B:" in line or line.startswith(b"B:"): # Temperature message. 'T:' for extruder and 'B:' for bed extruder_temperature_matches = re.findall(b"T(\d*): ?([\d\.]+) ?\/?([\d\.]+)?", line) # Update all temperature values for match, extruder in zip(extruder_temperature_matches, self._printers[0].extruders): From b1be25e8d8142d59cddb5d5ec505808810598c66 Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Fri, 23 Feb 2018 18:13:42 +0100 Subject: [PATCH 4/7] Start the device thread GCODE streaming after the main thread has sent those first few lines of codes. This fixes an issue where a race between the main thread and device thread, caused both to try to send the first lines of codes and the line numbering were then messed up. This caused the printer to send a resend request for a line whose number Cura did not recognize, and the printing would wait forever to start. --- plugins/USBPrinting/USBPrinterOutputDevice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index e34736c241..942e8d5306 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -198,7 +198,6 @@ class USBPrinterOutputDevice(PrinterOutputDevice): # Reset line number. If this is not done, first line is sometimes ignored self._gcode.insert(0, "M110") self._gcode_position = 0 - self._is_printing = True self._print_start_time = time() self._print_estimated_time = int(Application.getInstance().getPrintInformation().currentPrintTime.getDisplayString(DurationFormat.Format.Seconds)) @@ -206,6 +205,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice): for i in range(0, 4): # Push first 4 entries before accepting other inputs self._sendNextGcodeLine() + self._is_printing = True self.writeFinished.emit(self) def _autoDetectFinished(self, job: AutoDetectBaudJob): From b3f0292ce60c2920a9117de8fb102dc40f19aa51 Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Fri, 23 Feb 2018 18:19:11 +0100 Subject: [PATCH 5/7] Cancel the print when the printer sends message (b'!!') about a fatal error. --- plugins/USBPrinting/USBPrinterOutputDevice.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index 942e8d5306..8cb477d10d 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -298,6 +298,9 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self._printers[0].updateTargetBedTemperature(float(match[1])) if self._is_printing: + if line.startswith(b'!!'): + Logger.log('e', "Printer signals fatal error. Cancelling print. {}".format(line)) + self.cancelPrint() if b"ok" in line: if not self._command_queue.empty(): self._sendCommand(self._command_queue.get()) From 5214ef3bde8e632908d2b346e2a96c6f8a3df766 Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Fri, 23 Feb 2018 18:20:47 +0100 Subject: [PATCH 6/7] Add a retry when probing for printers on discovered ports. This is to accomodate for printers that needs a few seconds to initialize before they open the port. --- plugins/USBPrinting/AutoDetectBaudJob.py | 61 +++++++++++++----------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/plugins/USBPrinting/AutoDetectBaudJob.py b/plugins/USBPrinting/AutoDetectBaudJob.py index 72f4f20262..50bb831ba8 100644 --- a/plugins/USBPrinting/AutoDetectBaudJob.py +++ b/plugins/USBPrinting/AutoDetectBaudJob.py @@ -22,6 +22,7 @@ class AutoDetectBaudJob(Job): def run(self): Logger.log("d", "Auto detect baud rate started.") timeout = 3 + tries = 2 programmer = Stk500v2() serial = None @@ -31,36 +32,38 @@ class AutoDetectBaudJob(Job): except: programmer.close() - for baud_rate in self._all_baud_rates: - Logger.log("d", "Checking {serial} if baud rate {baud_rate} works".format(serial= self._serial_port, baud_rate = baud_rate)) + for retry in range(tries): + for baud_rate in self._all_baud_rates: + Logger.log("d", "Checking {serial} if baud rate {baud_rate} works".format(serial= self._serial_port, baud_rate = baud_rate)) - if serial is None: - try: - serial = Serial(str(self._serial_port), baud_rate, timeout = timeout, writeTimeout = timeout) - except SerialException as e: - Logger.logException("w", "Unable to create serial") - continue - else: - # We already have a serial connection, just change the baud rate. - try: - serial.baudrate = baud_rate - except: - continue - sleep(1.5) # Ensure that we are not talking to the boot loader. 1.5 seconds seems to be the magic number - successful_responses = 0 - - serial.write(b"\n") # Ensure we clear out previous responses - serial.write(b"M105\n") - - timeout_time = time() + timeout - - while timeout_time > time(): - line = serial.readline() - if b"ok T:" in line: - successful_responses += 1 - if successful_responses >= 3: - self.setResult(baud_rate) - return + if serial is None: + try: + serial = Serial(str(self._serial_port), baud_rate, timeout = timeout, writeTimeout = timeout) + except SerialException as e: + Logger.logException("w", "Unable to create serial") + continue + else: + # We already have a serial connection, just change the baud rate. + try: + serial.baudrate = baud_rate + except: + continue + sleep(1.5) # Ensure that we are not talking to the boot loader. 1.5 seconds seems to be the magic number + successful_responses = 0 + serial.write(b"\n") # Ensure we clear out previous responses serial.write(b"M105\n") + + timeout_time = time() + timeout + + while timeout_time > time(): + line = serial.readline() + if b"ok T:" in line: + successful_responses += 1 + if successful_responses >= 3: + self.setResult(baud_rate) + return + + serial.write(b"M105\n") + sleep(15) # Give the printer some time to init and try again. self.setResult(None) # Unable to detect the correct baudrate. From a024be78dc4a74d6ef9ae0776533b42e33d1d443 Mon Sep 17 00:00:00 2001 From: Simon Lundell Date: Fri, 23 Feb 2018 18:54:31 +0100 Subject: [PATCH 7/7] Reverting this commit as this was not the correct way to fix it. Revert "Do not send new G-CODEs when the RX buffer is filling up. This is in an attempt to fix #1777 where smoothieware based printers would halt in the middle of the print." This reverts commit 112bb260acb8655e929cf377b18f505d62608eb1. --- plugins/USBPrinting/USBPrinterOutputDevice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/USBPrinting/USBPrinterOutputDevice.py b/plugins/USBPrinting/USBPrinterOutputDevice.py index 8cb477d10d..4f5fdc340a 100644 --- a/plugins/USBPrinting/USBPrinterOutputDevice.py +++ b/plugins/USBPrinting/USBPrinterOutputDevice.py @@ -306,7 +306,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice): self._sendCommand(self._command_queue.get()) elif self._paused: pass # Nothing to do! - elif self._serial.in_waiting < 256: # Do not send new G-CODE when the RX buffer is filling up + else: self._sendNextGcodeLine() elif b"resend" in line.lower() or b"rs" in line: # A resend can be requested either by Resend, resend or rs.