60 lines
1.4 KiB
Python
Executable File
60 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Simple script to stream a G-code file to a GRBL controller.
|
|
"""
|
|
import os
|
|
import time
|
|
|
|
import serial
|
|
|
|
def stream(filename):
|
|
"""Opens the given filename and streams it to GRBL."""
|
|
usbs = [dev for dev in os.listdir("/dev/") if dev.startswith("ttyUSB")]
|
|
for usb in usbs:
|
|
try:
|
|
s = serial.Serial(os.path.join("/dev/", usb), 115200)
|
|
break
|
|
except serial.serialutil.SerialException:
|
|
continue
|
|
else:
|
|
print("Could not find any USB connections. Exiting.")
|
|
return
|
|
|
|
with open(filename, 'r') as file:
|
|
data = file.read().splitlines()
|
|
len_cmds = len(data)
|
|
progress = []
|
|
|
|
# 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
|
|
|
|
for n, line in enumerate(data):
|
|
l = line.strip().encode("utf-8") # Strip all EOL characters for consistency
|
|
# print('Sending: ' + str(l))
|
|
s.write(l + b"\n") # Send g-code block to grbl
|
|
grbl_out = s.readline() # Wait for grbl response with carriage return
|
|
# print('Receive: ' + str(grbl_out).strip())
|
|
percent = int(n/len_cmds*100)
|
|
if not int(n/len_cmds) % 5:
|
|
if not percent in progress:
|
|
print(str(percent) + "%")
|
|
progress.append(percent)
|
|
|
|
s.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description="Streams the G-code file to GRBL.")
|
|
parser.add_argument(
|
|
"filename",
|
|
help="The file to stream.")
|
|
args = parser.parse_args()
|
|
|
|
stream(args.filename)
|
|
|