comm: Synchronized write access to line number

New method wraps line sending and incrementing
(cherry picked from commit f8e72c9)
This commit is contained in:
Gina Häußge 2015-08-27 16:26:51 +02:00
parent 7d922f3c77
commit b965d4cbde

View file

@ -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):