Просмотр исходного кода

Merge branch 'master' of github.com:Ultimaker/cura

Aleksei S 7 лет назад
Родитель
Сommit
fc977088a8

+ 30 - 27
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
+                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")
+                serial.write(b"\n")  # Ensure we clear out previous responses
+                serial.write(b"M105\n")
 
-            timeout_time = time() + timeout
+                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
+                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")
+                    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.

+ 5 - 3
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):
@@ -267,7 +267,6 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
         if not command.endswith(b"\n"):
             command += b"\n"
         try:
-            self._serial.write(b"\n")
             self._serial.write(command)
         except SerialTimeoutException:
             Logger.log("w", "Timeout when sending command to printer via USB.")
@@ -284,7 +283,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):
@@ -302,6 +301,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())