Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python3 port of simple_stream.py #1167

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 26 additions & 24 deletions doc/script/simple_stream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""\
Simple g-code streaming script for grbl

Expand All @@ -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.

---------------------
Expand All @@ -35,32 +35,34 @@
THE SOFTWARE.
---------------------
"""

import serial
import time
import serial


# 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
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(b"\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(f"Sending {l}", end="")
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().decode('utf-8'))

# Wait here until grbl is finished to close serial port and file.
raw_input(" Press <Enter> to exit and disable grbl.")
# Wait here until grbl is finished to close serial port and file.
input(" Press <Enter> to exit and disable grbl.")

# Close file and serial port
f.close()
s.close()
# Close serial port
s.close()