Created
March 21, 2026 22:25
-
-
Save RobinDavid/159fc42571b5ba165db820493ff65033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Manually run a GCODE script on a Grbl CNC | |
| # inspired by https://github.com/Sam-Freitas/python_to_GRBL | |
| # | |
| RAW_SCRIPT = ''' | |
| G21 ; Set units to millimeters | |
| G90 ; Absolute positioning | |
| G0 X0 Y0 ; Move to start point | |
| M3 S1000 | |
| G1 Z-0.1 F100 ; Lower tool slightly (optional, depends on machine) | |
| G1 X1 Y0 F200 ; Draw first edge | |
| G1 X1 Y1 ; Second edge | |
| G1 X0 Y1 ; Third edge | |
| G1 X0 Y0 ; Close rectangle | |
| G0 Z5 ; Lift tool | |
| M5 ; Spindle off | |
| M30 ; End program | |
| ''' | |
| SCRIPT = ["G21", "G90", "G0 X0 Y0", "M3 S1000", "G1 Z-0.1 F100", "G1 X1 Y0 F200", "G1 X1 Y1", "G1 X0 Y1", | |
| "G1 X0 Y0", "G0 Z5", "M5", "M30"] | |
| import serial | |
| import time | |
| def wait_idle(ser): | |
| while True: | |
| ser.reset_input_buffer() | |
| command = str.encode('?' + '\n') | |
| ser.write(command) | |
| grbl_out = ser.readline() | |
| if grbl_out.startswith(b"<Idle"): | |
| break | |
| else: | |
| time.sleep(0.1) | |
| def send_command(ser, cmd: str): | |
| print(f"send: {cmd}") | |
| low_cmd = (cmd+"\n").encode() | |
| ser.write(low_cmd) | |
| wait_idle(ser) | |
| out = ser.readline() | |
| print(f"ret: {out}") | |
| s = serial.Serial("/dev/ttyUSB0", 115200, timeout=1) | |
| for cmd in SCRIPT: | |
| send_command(s, cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment