From f8e72c923328e9a0c9423431c2f3df7a457d7917 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 --- src/octoprint/util/comm.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index f32d8d70..ed62acab 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -243,6 +243,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 @@ -1620,10 +1621,7 @@ class MachineCom(object): checksum_enabled = self.isPrinting() or self._alwaysSendChecksum if command_requiring_checksum or (command_allowing_checksum and checksum_enabled): - linenumber = self._currentLine - self._addToLastLines(command) - self._currentLine += 1 - self._doSendWithChecksum(command, linenumber) + self._doIncrementAndSendWithChecksum(command) else: self._doSendWithoutChecksum(command) @@ -1716,6 +1714,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)) @@ -1824,11 +1829,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):