From 6be1034988528721733cf9430e609d6977e20e16 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 3 May 2018 21:31:11 -0700 Subject: [PATCH 1/5] Convert print to function (for Py3) --- doc/script/simple_stream.py | 6 +++-- doc/script/stream.py | 51 ++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index 67c2a2c..9c3520d 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function + """\ Simple g-code streaming script for grbl @@ -53,10 +55,10 @@ s.flushInput() # Flush startup text in serial input # Stream g-code to grbl for line in f: l = line.strip() # Strip all EOL characters for consistency - print 'Sending: ' + l, + print('Sending: ' + l) s.write(l + '\n') # Send g-code block to grbl grbl_out = s.readline() # Wait for grbl response with carriage return - print ' : ' + grbl_out.strip() + print(' : ' + grbl_out.strip()) # Wait here until grbl is finished to close serial port and file. raw_input(" Press to exit and disable grbl.") diff --git a/doc/script/stream.py b/doc/script/stream.py index 4a637ab..d4ca8ca 100755 --- a/doc/script/stream.py +++ b/doc/script/stream.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function + """\ Stream g-code to grbl controller @@ -95,7 +97,7 @@ check_mode = False if args.check : check_mode = True # Wake up grbl -print "Initializing Grbl..." +print("Initializing Grbl...") s.write("\r\n\r\n") # Wait for grbl to initialize and flush startup text in serial input @@ -103,16 +105,17 @@ time.sleep(2) s.flushInput() if check_mode : - print "Enabling Grbl Check-Mode: SND: [$C]", + print("Enabling Grbl Check-Mode: SND: [$C]", end=' ') s.write("$C\n") while 1: grbl_out = s.readline().strip() # Wait for grbl response with carriage return if grbl_out.find('error') >= 0 : - print "REC:",grbl_out - print " Failed to set Grbl check-mode. Aborting..." + print("REC:", grbl_out) + print(" Failed to set Grbl check-mode. Aborting...") quit() elif grbl_out.find('ok') >= 0 : - if verbose: print 'REC:',grbl_out + if verbose: + print('REC:', grbl_out) break start_time = time.time(); @@ -129,24 +132,27 @@ error_count = 0 if settings_mode: # Send settings file via simple call-response streaming method. Settings must be streamed # in this manner since the EEPROM accessing cycles shut-off the serial interrupt. - print "SETTINGS MODE: Streaming", args.gcode_file.name, " to ", args.device_file + print("SETTINGS MODE: Streaming", args.gcode_file.name, " to ", args.device_file) for line in f: l_count += 1 # Iterate line counter # l_block = re.sub('\s|\(.*?\)','',line).upper() # Strip comments/spaces/new line and capitalize l_block = line.strip() # Strip all EOL characters for consistency - if verbose: print "SND>"+str(l_count)+": \"" + l_block + "\"" + if verbose: + print("SND>" + str(l_count) + ": \"" + l_block + "\"") s.write(l_block + '\n') # Send g-code block to grbl while 1: grbl_out = s.readline().strip() # Wait for grbl response with carriage return if grbl_out.find('ok') >= 0 : - if verbose: print " REC<"+str(l_count)+": \""+grbl_out+"\"" + if verbose: + print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") break elif grbl_out.find('error') >= 0 : - if verbose: print " REC<"+str(l_count)+": \""+grbl_out+"\"" + if verbose: + print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") error_count += 1 break else: - print " MSG: \""+grbl_out+"\"" + print(" MSG: \"" + grbl_out + "\"") else: # Send g-code program via a more agressive streaming protocol that forces characters into # Grbl's serial read buffer to ensure Grbl has immediate access to the next g-code command @@ -164,38 +170,41 @@ else: while sum(c_line) >= RX_BUFFER_SIZE-1 | s.inWaiting() : out_temp = s.readline().strip() # Wait for grbl response if out_temp.find('ok') < 0 and out_temp.find('error') < 0 : - print " MSG: \""+out_temp+"\"" # Debug response + print(" MSG: \"" + out_temp + "\"") # Debug response else : if out_temp.find('error') >= 0 : error_count += 1 g_count += 1 # Iterate g-code counter - if verbose: print " REC<"+str(g_count)+": \""+out_temp+"\"" + if verbose: + print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") del c_line[0] # Delete the block character count corresponding to the last 'ok' s.write(l_block + '\n') # Send g-code block to grbl - if verbose: print "SND>"+str(l_count)+": \"" + l_block + "\"" + if verbose: + print("SND>" + str(l_count) + ": \"" + l_block + "\"") # Wait until all responses have been received. while l_count > g_count : out_temp = s.readline().strip() # Wait for grbl response if out_temp.find('ok') < 0 and out_temp.find('error') < 0 : - print " MSG: \""+out_temp+"\"" # Debug response + print(" MSG: \"" + out_temp + "\"") # Debug response else : if out_temp.find('error') >= 0 : error_count += 1 g_count += 1 # Iterate g-code counter del c_line[0] # Delete the block character count corresponding to the last 'ok' - if verbose: print " REC<"+str(g_count)+": \""+out_temp + "\"" + if verbose: + print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") # Wait for user input after streaming is completed -print "\nG-code streaming finished!" +print("\nG-code streaming finished!") end_time = time.time(); is_run = False; -print " Time elapsed: ",end_time-start_time,"\n" +print(" Time elapsed: ", end_time - start_time, "\n") if check_mode : if error_count > 0 : - print "CHECK FAILED:",error_count,"errors found! See output for details.\n" + print("CHECK FAILED:", error_count, "errors found! See output for details.\n") else : - print "CHECK PASSED: No errors found in g-code program.\n" + print("CHECK PASSED: No errors found in g-code program.\n") else : - print "WARNING: Wait until Grbl completes buffered g-code blocks before exiting." - raw_input(" Press to exit and disable Grbl.") + print("WARNING: Wait until Grbl completes buffered g-code blocks before exiting.") + raw_input(" Press to exit and disable Grbl.") # Close file and serial port f.close() From 275270d873fac4b8f8b8cccd88fd9389301d371f Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 3 May 2018 21:34:18 -0700 Subject: [PATCH 2/5] PEP8 Whitespace. Add file encoding. Remove unused import --- doc/script/stream.py | 127 +++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/doc/script/stream.py b/doc/script/stream.py index d4ca8ca..2ab574c 100755 --- a/doc/script/stream.py +++ b/doc/script/stream.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# coding=utf-8 from __future__ import print_function """\ @@ -50,51 +51,55 @@ THE SOFTWARE. import serial import re import time -import sys import argparse import threading RX_BUFFER_SIZE = 128 BAUD_RATE = 115200 ENABLE_STATUS_REPORTS = True -REPORT_INTERVAL = 1.0 # seconds +REPORT_INTERVAL = 1.0 # seconds -is_run = True # Controls query timer +is_run = True # Controls query timer # Define command line argument interface parser = argparse.ArgumentParser(description='Stream g-code file to grbl. (pySerial and argparse libraries required)') parser.add_argument('gcode_file', type=argparse.FileType('r'), - help='g-code filename to be streamed') + help='g-code filename to be streamed') parser.add_argument('device_file', - help='serial device path') -parser.add_argument('-q','--quiet',action='store_true', default=False, - help='suppress output text') -parser.add_argument('-s','--settings',action='store_true', default=False, - help='settings write mode') -parser.add_argument('-c','--check',action='store_true', default=False, - help='stream in check mode') + help='serial device path') +parser.add_argument('-q', '--quiet', action='store_true', default=False, + help='suppress output text') +parser.add_argument('-s', '--settings', action='store_true', default=False, + help='settings write mode') +parser.add_argument('-c', '--check', action='store_true', default=False, + help='stream in check mode') args = parser.parse_args() + # Periodic timer to query for status reports # TODO: Need to track down why this doesn't restart consistently before a release. def send_status_query(): s.write('?') - -def periodic_timer() : + + +def periodic_timer(): while is_run: - send_status_query() - time.sleep(REPORT_INTERVAL) - + send_status_query() + time.sleep(REPORT_INTERVAL) + # Initialize -s = serial.Serial(args.device_file,BAUD_RATE) +s = serial.Serial(args.device_file, BAUD_RATE) f = args.gcode_file verbose = True -if args.quiet : verbose = False +if args.quiet: + verbose = False settings_mode = False -if args.settings : settings_mode = True +if args.settings: + settings_mode = True check_mode = False -if args.check : check_mode = True +if args.check: + check_mode = True # Wake up grbl print("Initializing Grbl...") @@ -104,24 +109,24 @@ s.write("\r\n\r\n") time.sleep(2) s.flushInput() -if check_mode : +if check_mode: print("Enabling Grbl Check-Mode: SND: [$C]", end=' ') s.write("$C\n") while 1: - grbl_out = s.readline().strip() # Wait for grbl response with carriage return - if grbl_out.find('error') >= 0 : + grbl_out = s.readline().strip() # Wait for grbl response with carriage return + if grbl_out.find('error') >= 0: print("REC:", grbl_out) print(" Failed to set Grbl check-mode. Aborting...") quit() - elif grbl_out.find('ok') >= 0 : + elif grbl_out.find('ok') >= 0: if verbose: print('REC:', grbl_out) break -start_time = time.time(); +start_time = time.time() # Start status report periodic timer -if ENABLE_STATUS_REPORTS : +if ENABLE_STATUS_REPORTS: timerThread = threading.Thread(target=periodic_timer) timerThread.daemon = True timerThread.start() @@ -134,26 +139,26 @@ if settings_mode: # in this manner since the EEPROM accessing cycles shut-off the serial interrupt. print("SETTINGS MODE: Streaming", args.gcode_file.name, " to ", args.device_file) for line in f: - l_count += 1 # Iterate line counter + l_count += 1 # Iterate line counter # l_block = re.sub('\s|\(.*?\)','',line).upper() # Strip comments/spaces/new line and capitalize - l_block = line.strip() # Strip all EOL characters for consistency + l_block = line.strip() # Strip all EOL characters for consistency if verbose: print("SND>" + str(l_count) + ": \"" + l_block + "\"") - s.write(l_block + '\n') # Send g-code block to grbl + s.write(l_block + '\n') # Send g-code block to grbl while 1: - grbl_out = s.readline().strip() # Wait for grbl response with carriage return - if grbl_out.find('ok') >= 0 : + grbl_out = s.readline().strip() # Wait for grbl response with carriage return + if grbl_out.find('ok') >= 0: if verbose: print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") break - elif grbl_out.find('error') >= 0 : + elif grbl_out.find('error') >= 0: if verbose: print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") error_count += 1 break else: print(" MSG: \"" + grbl_out + "\"") -else: +else: # Send g-code program via a more agressive streaming protocol that forces characters into # Grbl's serial read buffer to ensure Grbl has immediate access to the next g-code command # rather than wait for the call-response serial protocol to finish. This is done by careful @@ -162,49 +167,51 @@ else: g_count = 0 c_line = [] for line in f: - l_count += 1 # Iterate line counter - l_block = re.sub('\s|\(.*?\)','',line).upper() # Strip comments/spaces/new line and capitalize + l_count += 1 # Iterate line counter + l_block = re.sub('\s|\(.*?\)', '', line).upper() # Strip comments/spaces/new line and capitalize # l_block = line.strip() - c_line.append(len(l_block)+1) # Track number of characters in grbl serial read buffer - grbl_out = '' - while sum(c_line) >= RX_BUFFER_SIZE-1 | s.inWaiting() : - out_temp = s.readline().strip() # Wait for grbl response - if out_temp.find('ok') < 0 and out_temp.find('error') < 0 : + c_line.append(len(l_block) + 1) # Track number of characters in grbl serial read buffer + grbl_out = '' + while sum(c_line) >= RX_BUFFER_SIZE - 1 | s.inWaiting(): + out_temp = s.readline().strip() # Wait for grbl response + if out_temp.find('ok') < 0 and out_temp.find('error') < 0: print(" MSG: \"" + out_temp + "\"") # Debug response - else : - if out_temp.find('error') >= 0 : error_count += 1 - g_count += 1 # Iterate g-code counter + else: + if out_temp.find('error') >= 0: + error_count += 1 + g_count += 1 # Iterate g-code counter if verbose: print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") - del c_line[0] # Delete the block character count corresponding to the last 'ok' - s.write(l_block + '\n') # Send g-code block to grbl + del c_line[0] # Delete the block character count corresponding to the last 'ok' + s.write(l_block + '\n') # Send g-code block to grbl if verbose: print("SND>" + str(l_count) + ": \"" + l_block + "\"") # Wait until all responses have been received. - while l_count > g_count : - out_temp = s.readline().strip() # Wait for grbl response - if out_temp.find('ok') < 0 and out_temp.find('error') < 0 : + while l_count > g_count: + out_temp = s.readline().strip() # Wait for grbl response + if out_temp.find('ok') < 0 and out_temp.find('error') < 0: print(" MSG: \"" + out_temp + "\"") # Debug response - else : - if out_temp.find('error') >= 0 : error_count += 1 - g_count += 1 # Iterate g-code counter - del c_line[0] # Delete the block character count corresponding to the last 'ok' + else: + if out_temp.find('error') >= 0: + error_count += 1 + g_count += 1 # Iterate g-code counter + del c_line[0] # Delete the block character count corresponding to the last 'ok' if verbose: print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") # Wait for user input after streaming is completed print("\nG-code streaming finished!") -end_time = time.time(); -is_run = False; +end_time = time.time() +is_run = False print(" Time elapsed: ", end_time - start_time, "\n") -if check_mode : - if error_count > 0 : +if check_mode: + if error_count > 0: print("CHECK FAILED:", error_count, "errors found! See output for details.\n") - else : + else: print("CHECK PASSED: No errors found in g-code program.\n") -else : - print("WARNING: Wait until Grbl completes buffered g-code blocks before exiting.") - raw_input(" Press to exit and disable Grbl.") +else: + print("WARNING: Wait until Grbl completes buffered g-code blocks before exiting.") + raw_input(" Press to exit and disable Grbl.") # Close file and serial port f.close() From f5ae45a6798c60ca7362031cf0d5a0759ded495d Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 3 May 2018 21:34:55 -0700 Subject: [PATCH 3/5] PEP8 Whitespace. Add file encoding. --- doc/script/simple_stream.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index 9c3520d..000866c 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# coding=utf-8 from __future__ import print_function """\ @@ -42,27 +43,27 @@ import serial import time # Open grbl serial port -s = serial.Serial('/dev/tty.usbmodem1811',115200) +s = serial.Serial('/dev/tty.usbmodem1811', 115200) # Open g-code file -f = open('grbl.gcode','r'); +f = open('grbl.gcode', 'r') # Wake up grbl s.write("\r\n\r\n") -time.sleep(2) # Wait for grbl to initialize +time.sleep(2) # Wait for grbl to initialize s.flushInput() # Flush startup text in serial input # Stream g-code to grbl for line in f: - l = line.strip() # Strip all EOL characters for consistency + l = line.strip() # Strip all EOL characters for consistency print('Sending: ' + l) - s.write(l + '\n') # Send g-code block to grbl - grbl_out = s.readline() # Wait for grbl response with carriage return + s.write(l + '\n') # Send g-code block to grbl + grbl_out = s.readline() # Wait for grbl response with carriage return print(' : ' + grbl_out.strip()) # Wait here until grbl is finished to close serial port and file. -raw_input(" Press to exit and disable grbl.") +raw_input(" Press to exit and disable grbl.") # Close file and serial port f.close() -s.close() \ No newline at end of file +s.close() From a9555d2e8416f39ed01a70ef9690c9ff1b6761e3 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 3 May 2018 21:38:03 -0700 Subject: [PATCH 4/5] Switch to context manager --- doc/script/simple_stream.py | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index 000866c..bb957aa 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -42,28 +42,21 @@ THE SOFTWARE. import serial import time -# Open grbl serial port -s = serial.Serial('/dev/tty.usbmodem1811', 115200) +# Open grbl serial port & Open g-code file +with serial.Serial('/dev/tty.usbmodem1811', 115200) as s, open('grbl.gcode', 'r') as f: -# Open g-code file -f = open('grbl.gcode', 'r') + # Wake up grbl + s.write("\r\n\r\n") + time.sleep(2) # Wait for grbl to initialize + s.flushInput() # Flush startup text in serial input -# Wake up grbl -s.write("\r\n\r\n") -time.sleep(2) # Wait for grbl to initialize -s.flushInput() # Flush startup text in serial input + # Stream g-code to grbl + for line in f: + l = line.strip() # Strip all EOL characters for consistency + print('Sending: ' + l) + s.write(l + '\n') # Send g-code block to grbl + grbl_out = s.readline() # Wait for grbl response with carriage return + print(' : ' + grbl_out.strip()) -# Stream g-code to grbl -for line in f: - l = line.strip() # Strip all EOL characters for consistency - print('Sending: ' + l) - s.write(l + '\n') # Send g-code block to grbl - grbl_out = s.readline() # Wait for grbl response with carriage return - print(' : ' + grbl_out.strip()) - -# Wait here until grbl is finished to close serial port and file. -raw_input(" Press to exit and disable grbl.") - -# Close file and serial port -f.close() -s.close() + # Wait here until grbl is finished to close serial port and file. + raw_input(" Press to exit and disable grbl.") From 7bdabc9728c89587b1c089e5176435bd1e35e651 Mon Sep 17 00:00:00 2001 From: Ryan Jarvis Date: Thu, 3 May 2018 21:46:25 -0700 Subject: [PATCH 5/5] Switch to context manager --- doc/script/stream.py | 211 ++++++++++++++++++++++--------------------- 1 file changed, 106 insertions(+), 105 deletions(-) diff --git a/doc/script/stream.py b/doc/script/stream.py index 2ab574c..52033d7 100755 --- a/doc/script/stream.py +++ b/doc/script/stream.py @@ -89,90 +89,107 @@ def periodic_timer(): # Initialize -s = serial.Serial(args.device_file, BAUD_RATE) f = args.gcode_file -verbose = True -if args.quiet: - verbose = False -settings_mode = False -if args.settings: - settings_mode = True -check_mode = False -if args.check: - check_mode = True -# Wake up grbl -print("Initializing Grbl...") -s.write("\r\n\r\n") +with serial.Serial(args.device_file, BAUD_RATE) as s: -# Wait for grbl to initialize and flush startup text in serial input -time.sleep(2) -s.flushInput() + verbose = True + if args.quiet: + verbose = False + settings_mode = False + if args.settings: + settings_mode = True + check_mode = False + if args.check: + check_mode = True -if check_mode: - print("Enabling Grbl Check-Mode: SND: [$C]", end=' ') - s.write("$C\n") - while 1: - grbl_out = s.readline().strip() # Wait for grbl response with carriage return - if grbl_out.find('error') >= 0: - print("REC:", grbl_out) - print(" Failed to set Grbl check-mode. Aborting...") - quit() - elif grbl_out.find('ok') >= 0: - if verbose: - print('REC:', grbl_out) - break + # Wake up grbl + print("Initializing Grbl...") + s.write("\r\n\r\n") -start_time = time.time() + # Wait for grbl to initialize and flush startup text in serial input + time.sleep(2) + s.flushInput() -# Start status report periodic timer -if ENABLE_STATUS_REPORTS: - timerThread = threading.Thread(target=periodic_timer) - timerThread.daemon = True - timerThread.start() - -# Stream g-code to grbl -l_count = 0 -error_count = 0 -if settings_mode: - # Send settings file via simple call-response streaming method. Settings must be streamed - # in this manner since the EEPROM accessing cycles shut-off the serial interrupt. - print("SETTINGS MODE: Streaming", args.gcode_file.name, " to ", args.device_file) - for line in f: - l_count += 1 # Iterate line counter - # l_block = re.sub('\s|\(.*?\)','',line).upper() # Strip comments/spaces/new line and capitalize - l_block = line.strip() # Strip all EOL characters for consistency - if verbose: - print("SND>" + str(l_count) + ": \"" + l_block + "\"") - s.write(l_block + '\n') # Send g-code block to grbl + if check_mode: + print("Enabling Grbl Check-Mode: SND: [$C]", end=' ') + s.write("$C\n") while 1: grbl_out = s.readline().strip() # Wait for grbl response with carriage return - if grbl_out.find('ok') >= 0: + if grbl_out.find('error') >= 0: + print("REC:", grbl_out) + print(" Failed to set Grbl check-mode. Aborting...") + quit() + elif grbl_out.find('ok') >= 0: if verbose: - print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") + print('REC:', grbl_out) break - elif grbl_out.find('error') >= 0: - if verbose: - print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") - error_count += 1 - break - else: - print(" MSG: \"" + grbl_out + "\"") -else: - # Send g-code program via a more agressive streaming protocol that forces characters into - # Grbl's serial read buffer to ensure Grbl has immediate access to the next g-code command - # rather than wait for the call-response serial protocol to finish. This is done by careful - # counting of the number of characters sent by the streamer to Grbl and tracking Grbl's - # responses, such that we never overflow Grbl's serial read buffer. - g_count = 0 - c_line = [] - for line in f: - l_count += 1 # Iterate line counter - l_block = re.sub('\s|\(.*?\)', '', line).upper() # Strip comments/spaces/new line and capitalize - # l_block = line.strip() - c_line.append(len(l_block) + 1) # Track number of characters in grbl serial read buffer - grbl_out = '' - while sum(c_line) >= RX_BUFFER_SIZE - 1 | s.inWaiting(): + + start_time = time.time() + + # Start status report periodic timer + if ENABLE_STATUS_REPORTS: + timerThread = threading.Thread(target=periodic_timer) + timerThread.daemon = True + timerThread.start() + + # Stream g-code to grbl + l_count = 0 + error_count = 0 + if settings_mode: + # Send settings file via simple call-response streaming method. Settings must be streamed + # in this manner since the EEPROM accessing cycles shut-off the serial interrupt. + print("SETTINGS MODE: Streaming", args.gcode_file.name, " to ", args.device_file) + for line in f: + l_count += 1 # Iterate line counter + # l_block = re.sub('\s|\(.*?\)','',line).upper() # Strip comments/spaces/new line and capitalize + l_block = line.strip() # Strip all EOL characters for consistency + if verbose: + print("SND>" + str(l_count) + ": \"" + l_block + "\"") + s.write(l_block + '\n') # Send g-code block to grbl + while 1: + grbl_out = s.readline().strip() # Wait for grbl response with carriage return + if grbl_out.find('ok') >= 0: + if verbose: + print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") + break + elif grbl_out.find('error') >= 0: + if verbose: + print(" REC<" + str(l_count) + ": \"" + grbl_out + "\"") + error_count += 1 + break + else: + print(" MSG: \"" + grbl_out + "\"") + else: + # Send g-code program via a more agressive streaming protocol that forces characters into + # Grbl's serial read buffer to ensure Grbl has immediate access to the next g-code command + # rather than wait for the call-response serial protocol to finish. This is done by careful + # counting of the number of characters sent by the streamer to Grbl and tracking Grbl's + # responses, such that we never overflow Grbl's serial read buffer. + g_count = 0 + c_line = [] + for line in f: + l_count += 1 # Iterate line counter + l_block = re.sub('\s|\(.*?\)', '', line).upper() # Strip comments/spaces/new line and capitalize + # l_block = line.strip() + c_line.append(len(l_block) + 1) # Track number of characters in grbl serial read buffer + grbl_out = '' + while sum(c_line) >= RX_BUFFER_SIZE - 1 | s.inWaiting(): + out_temp = s.readline().strip() # Wait for grbl response + if out_temp.find('ok') < 0 and out_temp.find('error') < 0: + print(" MSG: \"" + out_temp + "\"") # Debug response + else: + if out_temp.find('error') >= 0: + error_count += 1 + g_count += 1 # Iterate g-code counter + if verbose: + print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") + del c_line[0] # Delete the block character count corresponding to the last 'ok' + s.write(l_block + '\n') # Send g-code block to grbl + if verbose: + print("SND>" + str(l_count) + ": \"" + l_block + "\"") + # Wait until all responses have been received. + while l_count > g_count: out_temp = s.readline().strip() # Wait for grbl response if out_temp.find('ok') < 0 and out_temp.find('error') < 0: print(" MSG: \"" + out_temp + "\"") # Debug response @@ -180,39 +197,23 @@ else: if out_temp.find('error') >= 0: error_count += 1 g_count += 1 # Iterate g-code counter + del c_line[0] # Delete the block character count corresponding to the last 'ok' if verbose: print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") - del c_line[0] # Delete the block character count corresponding to the last 'ok' - s.write(l_block + '\n') # Send g-code block to grbl - if verbose: - print("SND>" + str(l_count) + ": \"" + l_block + "\"") - # Wait until all responses have been received. - while l_count > g_count: - out_temp = s.readline().strip() # Wait for grbl response - if out_temp.find('ok') < 0 and out_temp.find('error') < 0: - print(" MSG: \"" + out_temp + "\"") # Debug response + + # Wait for user input after streaming is completed + print("\nG-code streaming finished!") + end_time = time.time() + is_run = False + print(" Time elapsed: ", end_time - start_time, "\n") + if check_mode: + if error_count > 0: + print("CHECK FAILED:", error_count, "errors found! See output for details.\n") else: - if out_temp.find('error') >= 0: - error_count += 1 - g_count += 1 # Iterate g-code counter - del c_line[0] # Delete the block character count corresponding to the last 'ok' - if verbose: - print(" REC<" + str(g_count) + ": \"" + out_temp + "\"") - -# Wait for user input after streaming is completed -print("\nG-code streaming finished!") -end_time = time.time() -is_run = False -print(" Time elapsed: ", end_time - start_time, "\n") -if check_mode: - if error_count > 0: - print("CHECK FAILED:", error_count, "errors found! See output for details.\n") + print("CHECK PASSED: No errors found in g-code program.\n") else: - print("CHECK PASSED: No errors found in g-code program.\n") -else: - print("WARNING: Wait until Grbl completes buffered g-code blocks before exiting.") - raw_input(" Press to exit and disable Grbl.") + print("WARNING: Wait until Grbl completes buffered g-code blocks before exiting.") + raw_input(" Press to exit and disable Grbl.") -# Close file and serial port -f.close() -s.close() + # Close file and serial port + f.close()