chiark / gitweb /
Speed up getting operational by sending M105 as soon as Marlin is ready, not after...
[cura.git] / Cura / util / machineCom.py
index 650452dfbec1d4b43f8ba615756339870e7f016c..0282fd7c7d67c8567bcf54c249826e9aab27f564 100644 (file)
@@ -364,7 +364,7 @@ class MachineCom(object):
                        line = self._readline()
                        if line is None:
                                break
-                       
+
                        #No matter the state, if we see an fatal error, goto the error state and store the error for reference.
                        # Only goto error on known fatal errors.
                        if line.startswith('Error:'):
@@ -453,7 +453,7 @@ class MachineCom(object):
                                else:
                                        self._testingBaudrate = False
                        elif self._state == self.STATE_CONNECTING:
-                               if line == '' or 'wait' in line:        # 'wait' needed for Repetier (kind of watchdog)
+                               if line == '' or 'wait' in line or 'start' in line:        # 'wait' needed for Repetier (kind of watchdog)
                                        self._sendCommand("M105")
                                elif 'ok' in line:
                                        self._changeState(self.STATE_OPERATIONAL)
@@ -487,11 +487,26 @@ class MachineCom(object):
                                        else:
                                                self._sendNext()
                                elif "resend" in line.lower() or "rs" in line:
+                                       newPos = self._gcodePos
                                        try:
-                                               self._gcodePos = int(line.replace("N:"," ").replace("N"," ").replace(":"," ").split()[-1])
+                                               newPos = int(line.replace("N:"," ").replace("N"," ").replace(":"," ").split()[-1])
                                        except:
                                                if "rs" in line:
-                                                       self._gcodePos = int(line.split()[1])
+                                                       newPos = int(line.split()[1])
+                                       # If we need to resend more than 10 lines, we can assume that the machine
+                                       # was shut down and turned back on or something else that's weird just happened.
+                                       # In that case, it can be dangerous to restart the print, so we'd better kill it
+                                       if newPos == 1 or self._gcodePos > newPos + 100:
+                                               self._callback.mcMessage("Print canceled due to loss of communication to printer (USB unplugged or power lost)")
+                                               self.cancelPrint()
+                                       else:
+                                               self._gcodePos = newPos
+                       elif self._state == self.STATE_PAUSED:
+                               if 'ok' in line:
+                                       timeout = time.time() + 5
+                                       if not self._commandQueue.empty():
+                                               self._sendCommand(self._commandQueue.get())
+
                self._log("Connection closed, closing down monitor")
 
        def _setBaudrate(self, baudrate):
@@ -574,7 +589,7 @@ class MachineCom(object):
                        self._log("Unexpected error while writing serial port: %s" % (getExceptionString()))
                        self._errorValue = getExceptionString()
                        self.close(True)
-       
+
        def _sendNext(self):
                if self._gcodePos >= len(self._gcodeList):
                        self._changeState(self.STATE_OPERATIONAL)
@@ -587,12 +602,12 @@ class MachineCom(object):
                        line = line[0]
                try:
                        if line == 'M0' or line == 'M1':
-                               #self.setPause(True)
+                               self.setPause(True)
                                line = 'M105'   #Don't send the M0 or M1 to the machine, as M0 and M1 are handled as an LCD menu pause.
                        if self._printSection in self._feedRateModifier:
                                line = re.sub('F([0-9]*)', lambda m: 'F' + str(int(int(m.group(1)) * self._feedRateModifier[self._printSection])), line)
                        if ('G0' in line or 'G1' in line) and 'Z' in line:
-                               z = float(re.search('Z([0-9\.]*)', line).group(1))
+                               z = float(re.search('Z(-?[0-9\.]*)', line).group(1))
                                if self._currentZ != z:
                                        self._currentZ = z
                                        self._callback.mcZChange(z)
@@ -602,14 +617,14 @@ class MachineCom(object):
                self._sendCommand("N%d%s*%d" % (self._gcodePos, line, checksum))
                self._gcodePos += 1
                self._callback.mcProgress(self._gcodePos)
-       
+
        def sendCommand(self, cmd):
                cmd = cmd.encode('ascii', 'replace')
                if self.isPrinting():
                        self._commandQueue.put(cmd)
                elif self.isOperational():
                        self._sendCommand(cmd)
-       
+
        def printGCode(self, gcodeList):
                if not self.isOperational() or self.isPrinting():
                        return