From 365da6c621fbaf348a33aa440ca912f1cf603767 Mon Sep 17 00:00:00 2001 From: Kevin R Keegan Date: Fri, 15 Dec 2017 16:22:58 -0800 Subject: [PATCH 1/2] Ensure That Machine is Ready Before Sending Command I think on startup we may be sending commands sooner in some cases than when the firmware is ready to receive. --- Connection/serialPortThread.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Connection/serialPortThread.py b/Connection/serialPortThread.py index 24e807ed..4ec4d90b 100644 --- a/Connection/serialPortThread.py +++ b/Connection/serialPortThread.py @@ -15,7 +15,7 @@ class SerialPortThread(MakesmithInitFuncs): ''' - machineIsReadyForData = False + machineIsReadyForData = False # Tracks whether last command was acked lastWriteTime = time.time() bufferSpace = 256 lengthOfLastLineStack = Queue.Queue() @@ -39,6 +39,7 @@ def _write (self, message): self.bufferSpace = self.bufferSpace - len(message) self.lengthOfLastLineStack.put(len(message)) + machineIsReadyForData = False message = message.encode() try: @@ -113,6 +114,7 @@ def getmessage (self): #Check if a line has been completed if lineFromMachine == "ok\r\n": + machineIsReadyForData = True if self.lengthOfLastLineStack.empty() != True: #if we've sent lines to the machine self.bufferSpace = self.bufferSpace + self.lengthOfLastLineStack.get_nowait() #free up that space in the buffer @@ -128,14 +130,14 @@ def getmessage (self): self._write(command) #send regular instructions to the machine if there are any - if self.bufferSpace == 256: + if self.bufferSpace == 256 and machineIsReadyForData: if self.data.gcode_queue.empty() != True: command = self.data.gcode_queue.get_nowait() + " " self._write(command) #Send the next line of gcode to the machine if we're running a program - if self.bufferSpace == 256:#> len(self.data.gcode[self.data.gcodeIndex]): + if self.bufferSpace == 256 and machineIsReadyForData:#> len(self.data.gcode[self.data.gcodeIndex]): if self.data.uploadFlag: self._write(self.data.gcode[self.data.gcodeIndex]) From 543a383149f491a7f6f8869a41b0efe16428ce23 Mon Sep 17 00:00:00 2001 From: Kevin R Keegan Date: Fri, 15 Dec 2017 19:59:47 -0800 Subject: [PATCH 2/2] Ensure Machine is Ready; Log Inc Messages Immediately There was no check for the 'ok' command from the firmware before sending the first message. This is likely why PR #405 and commit 6c934aad9b39438111242fae73e5617cc4321377 which sent a blank line on startup helped, becuase only the first command could be sent without an ok. Removed the kludge of a blank command and it seems to work well. This may also fix the issues reported in this post: https://forums.maslowcnc.com/t/unable-to-execute-command-machine-settings-not-yet-received/1517 Also added a new child class of Queue which logs the messages to the logger on put rather than waiting for the process to get them out of the queue. The get method was resulting in non- sequential incoming and outgoing messages in the log because outgoing messages were logged immediately. This makes the log much more helpful. --- Connection/serialPortThread.py | 10 ++++------ DataStructures/data.py | 3 ++- DataStructures/loggingQueue.py | 19 +++++++++++++++++++ main.py | 2 -- 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 DataStructures/loggingQueue.py diff --git a/Connection/serialPortThread.py b/Connection/serialPortThread.py index 4ec4d90b..23970b11 100644 --- a/Connection/serialPortThread.py +++ b/Connection/serialPortThread.py @@ -39,7 +39,7 @@ def _write (self, message): self.bufferSpace = self.bufferSpace - len(message) self.lengthOfLastLineStack.put(len(message)) - machineIsReadyForData = False + self.machineIsReadyForData = False message = message.encode() try: @@ -91,7 +91,6 @@ def getmessage (self): #print "port open?:" #print self.serialInstance.isOpen() self.lastMessageTime = time.time() - self.data.gcode_queue.put(' ') # send a blank line on startup self.data.connectionStatus = 1 self._getFirmwareVersion() @@ -114,7 +113,7 @@ def getmessage (self): #Check if a line has been completed if lineFromMachine == "ok\r\n": - machineIsReadyForData = True + self.machineIsReadyForData = True if self.lengthOfLastLineStack.empty() != True: #if we've sent lines to the machine self.bufferSpace = self.bufferSpace + self.lengthOfLastLineStack.get_nowait() #free up that space in the buffer @@ -130,14 +129,13 @@ def getmessage (self): self._write(command) #send regular instructions to the machine if there are any - if self.bufferSpace == 256 and machineIsReadyForData: - + if self.bufferSpace == 256 and self.machineIsReadyForData: if self.data.gcode_queue.empty() != True: command = self.data.gcode_queue.get_nowait() + " " self._write(command) #Send the next line of gcode to the machine if we're running a program - if self.bufferSpace == 256 and machineIsReadyForData:#> len(self.data.gcode[self.data.gcodeIndex]): + if self.bufferSpace == 256 and self.machineIsReadyForData: #> len(self.data.gcode[self.data.gcodeIndex]): if self.data.uploadFlag: self._write(self.data.gcode[self.data.gcodeIndex]) diff --git a/DataStructures/data.py b/DataStructures/data.py index f7aa40fe..f8f6b17c 100644 --- a/DataStructures/data.py +++ b/DataStructures/data.py @@ -6,6 +6,7 @@ from kivy.properties import NumericProperty from kivy.event import EventDispatcher from DataStructures.logger import Logger +from DataStructures.loggingQueue import LoggingQueue import Queue class Data(EventDispatcher): @@ -75,7 +76,7 @@ class Data(EventDispatcher): ''' Queues ''' - message_queue = Queue.Queue() + message_queue = LoggingQueue(logger) gcode_queue = Queue.Queue() quick_queue = Queue.Queue() diff --git a/DataStructures/loggingQueue.py b/DataStructures/loggingQueue.py new file mode 100644 index 00000000..d9edabdb --- /dev/null +++ b/DataStructures/loggingQueue.py @@ -0,0 +1,19 @@ +''' + +This module provides a simple addition to the Queue, which is that it logs +puts to the Queue immediately. + +''' + +from Queue import Queue + + +class LoggingQueue(Queue, object): + def __init__(self, logger): + self.logger = logger + super(LoggingQueue, self).__init__() + + def put(self, msg): + self.logger.writeToLog(msg) + return super(LoggingQueue, self).put(msg) + \ No newline at end of file diff --git a/main.py b/main.py index a1d6c2bf..4937902b 100755 --- a/main.py +++ b/main.py @@ -587,8 +587,6 @@ def runPeriodically(self, *args): while not self.data.message_queue.empty(): #if there is new data to be read message = self.data.message_queue.get() - self.data.logger.writeToLog(message) - if message[0] == "<": self.setPosOnScreen(message) elif message[0] == "[":