From fb67ec04cbfa37a2c075d5ebc372ced4a8e10cb5 Mon Sep 17 00:00:00 2001 From: tpltnt <1172976+tpltnt@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:47:40 +0200 Subject: [PATCH 1/6] minimal Python 3 port --- doc/script/simple_stream.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index 67c2a2c3..a01614fc 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """\ Simple g-code streaming script for grbl @@ -53,14 +53,14 @@ # Stream g-code to grbl for line in f: l = line.strip() # Strip all EOL characters for consistency - print 'Sending: ' + l, + print(f"Sending {l}", end="") 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.") # Close file and serial port f.close() -s.close() \ No newline at end of file +s.close() From 1d05c6cde25a9c1472a5278c8b9aa59977815f20 Mon Sep 17 00:00:00 2001 From: tpltnt <1172976+tpltnt@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:51:42 +0200 Subject: [PATCH 2/6] whitespace cleanup --- doc/script/simple_stream.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index a01614fc..01ae17cb 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -7,8 +7,8 @@ return an 'ok' or 'error' response. When the planner buffer is full, grbl will not send a response until the planner buffer clears space. -G02/03 arcs are special exceptions, where they inject short line -segments directly into the planner. So there may not be a response +G02/03 arcs are special exceptions, where they inject short line +segments directly into the planner. So there may not be a response from grbl for the duration of the arc. --------------------- @@ -43,11 +43,11 @@ 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 @@ -59,7 +59,7 @@ 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() From 7583e556db1599d10600e94d6423814c1d5b9a4c Mon Sep 17 00:00:00 2001 From: tpltnt <1172976+tpltnt@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:53:30 +0200 Subject: [PATCH 3/6] replaced raw_input() with input() --- doc/script/simple_stream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index 01ae17cb..f1ff5e98 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -59,7 +59,7 @@ print(' : ' + grbl_out.strip()) # Wait here until grbl is finished to close serial port and file. -raw_input(" Press to exit and disable grbl.") +input(" Press to exit and disable grbl.") # Close file and serial port f.close() From 33ff68cc5e2ced199dd9577e012e6d797f8c57f7 Mon Sep 17 00:00:00 2001 From: tpltnt <1172976+tpltnt@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:57:52 +0200 Subject: [PATCH 4/6] use 'with' for resource-allocating operation --- doc/script/simple_stream.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index f1ff5e98..c28932d1 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -35,32 +35,30 @@ THE SOFTWARE. --------------------- """ - -import serial import time +import serial + # Open grbl serial port s = serial.Serial('/dev/tty.usbmodem1811',115200) # 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 +with open('grbl.gcode','r') as f: + # 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(f"Sending {l}", end="") - 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(f"Sending {l}", end="") + 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. -input(" Press to exit and disable grbl.") + # Wait here until grbl is finished to close serial port and file. + input(" Press to exit and disable grbl.") -# Close file and serial port -f.close() +# Close serial port s.close() From cb61aeb87e446543f5e8644aea8879b2b3df4253 Mon Sep 17 00:00:00 2001 From: tpltnt <1172976+tpltnt@users.noreply.github.com> Date: Sun, 9 Oct 2022 12:01:26 +0200 Subject: [PATCH 5/6] less noisy failure when TTY device file not found --- doc/script/simple_stream.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index c28932d1..941ba53d 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -40,7 +40,11 @@ # Open grbl serial port -s = serial.Serial('/dev/tty.usbmodem1811',115200) +try: + s = serial.Serial('/dev/tty.usbmodem1811',115200) +except serial.serialutil.SerialException as e: + print(e) + exit(1) # Open g-code file with open('grbl.gcode','r') as f: From b2d8c45afde36581409268878fb7a0e02d00160f Mon Sep 17 00:00:00 2001 From: tpltnt <1172976+tpltnt@users.noreply.github.com> Date: Sun, 9 Oct 2022 12:08:04 +0200 Subject: [PATCH 6/6] proper encoding for serial data --- doc/script/simple_stream.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/script/simple_stream.py b/doc/script/simple_stream.py index 941ba53d..259b1efd 100755 --- a/doc/script/simple_stream.py +++ b/doc/script/simple_stream.py @@ -49,7 +49,7 @@ # Open g-code file with open('grbl.gcode','r') as f: # Wake up grbl - s.write("\r\n\r\n") + s.write(b"\r\n\r\n") time.sleep(2) # Wait for grbl to initialize s.flushInput() # Flush startup text in serial input @@ -57,9 +57,9 @@ for line in f: l = line.strip() # Strip all EOL characters for consistency print(f"Sending {l}", end="") - s.write(l + '\n') # Send g-code block to grbl + s.write(l.encode(encoding = 'ASCII') + b'\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().decode('utf-8')) # Wait here until grbl is finished to close serial port and file. input(" Press to exit and disable grbl.")