From b965d4cbde5b94e7cc1c732ff36c56ba0b9e85c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 27 Aug 2015 16:26:51 +0200 Subject: [PATCH] comm: Synchronized write access to line number New method wraps line sending and incrementing (cherry picked from commit f8e72c9) --- src/octoprint/util/comm.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 2c76de7a..c1999167 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -232,6 +232,7 @@ class MachineCom(object): self._sendChecksumWithUnknownCommands = settings().getBoolean(["feature", "sendChecksumWithUnknownCommands"]) self._unknownCommandsNeedAck = settings().getBoolean(["feature", "unknownCommandsNeedAck"]) self._currentLine = 1 + self._line_mutex = threading.RLock() self._resendDelta = None self._lastLines = deque([], 50) self._lastCommError = None @@ -1549,21 +1550,20 @@ class MachineCom(object): # now comes the part where we increase line numbers and send stuff - no turning back now if (gcode is not None or self._sendChecksumWithUnknownCommands) and (self.isPrinting() or self._alwaysSendChecksum): - linenumber = self._currentLine - self._addToLastLines(command) - self._currentLine += 1 - self._doSendWithChecksum(command, linenumber) + self._doIncrementAndSendWithChecksum(command) else: self._doSendWithoutChecksum(command) # trigger "sent" phase and use up one "ok" self._process_command_phase("sent", command, command_type, gcode=gcode) - # we only need to use up a clear if the command we just sent was either a gcode command or if we also - # require ack's for unknown commands - use_up_clear = self._unknownCommandsNeedAck - if gcode is not None: - use_up_clear = True + if command_requiring_checksum or (command_allowing_checksum and checksum_enabled): + linenumber = self._currentLine + self._addToLastLines(command) + self._currentLine += 1 + self._doSendWithChecksum(command, linenumber) + else: + self._doSendWithoutChecksum(command) # if we need to use up a clear, do that now if use_up_clear: @@ -1640,6 +1640,13 @@ class MachineCom(object): ##~~ actual sending via serial + def _doIncrementAndSendWithChecksum(self, cmd): + with self._line_mutex: + linenumber = self._currentLine + self._addToLastLines(cmd) + self._currentLine += 1 + self._doSendWithChecksum(cmd, linenumber) + def _doSendWithChecksum(self, cmd, lineNumber): commandToSend = "N%d %s" % (lineNumber, cmd) checksum = reduce(lambda x,y:x^y, map(ord, commandToSend)) @@ -1748,11 +1755,12 @@ class MachineCom(object): else: newLineNumber = 0 - # send M110 command with new line number - self._currentLine = newLineNumber + with self._line_mutex: + # send M110 command with new line number + self._currentLine = newLineNumber - # after a reset of the line number we have no way to determine what line exactly the printer now wants - self._lastLines.clear() + # after a reset of the line number we have no way to determine what line exactly the printer now wants + self._lastLines.clear() self._resendDelta = None def _gcode_M112_queuing(self, cmd, cmd_type=None):