Home > Arduino, CNC, Machining > Grbl: A Simple Python Interface

Grbl: A Simple Python Interface

For people who are more Python inclined, here’s a stripped-down, simple streaming interface with grbl from your computer. The script will send single line g-code blocks to grbl and wait for an acknowledgement. Grbl will send an acknowledgement only when it’s done processing the block and has room in the buffer. So, when starting up, you should see it fly through the first 16 or so (328p Arduinos) commands quickly as the buffer fills, and then it should steadily stream more commands as grbl completes blocks. I can only give setup details for Macs, but this script should work on just about any operating system with an up-to-date version of Python and the same general steps are followed.

For Macs, there are only a few things you will need to do to get up and running. Python, by default, is already installed, but you will need the PySerial library to interface with the Arduino. To install, simply run the Terminal.app and type sudo easy_install pyserial at the prompt.

You will also need to find the device path for your Arduino connected to your USB port. At the Terminal.app prompt, just type ‘/dev/tty.usbmodem’ and hit tab one or two times. This should come up with short list of device ports that is your Arduino. It will likely just come up with one. Replace the complete path in the script, along with the filename of your g-code. (Note: Arduino paths can change if you plug into a different port later)

To run, you can either use ‘python stream.py’ (or whatever you will call this Python script) or set the permissions to execute the script by filename.

That’s it!

#!/usr/bin/env python
Simple g-code streaming script for grbl

import serial
import time

# Open grbl serial port
s = serial.Serial('/dev/tty.usbmodem0000',9600)

# Open g-code file
f = open('somefile.gcode','r');

# Wake up grbl
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 streaming
    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 <Enter> to exit and disable grbl.")

# Close file and serial port

NOTE: For more advanced stuff, you can take advantage of the 128 character serial buffer on the Arduino and design the script to keep careful track of how many characters you have sent and how many have been processed by each ‘ok’ response. If you ever run into any data starving problems (by using a wickedly slow computer like my resurrected 550Mhz Powerbook G4), this creates another buffer layer and provides grbl immediate access to g-code commands without having to wait for any serial processes. So far, it has worked very well for me when having to send a long series of very short line segments.

Categories: Arduino, CNC, Machining
  1. Andrew
    May 20, 2013 at 10:22 pm

    Hello, I cannot figure out why I get the “Wait for ok failed. Process interrupted” message. I am using my MAc with the Grbl 0.8 and Arduino 329 (UNO) it seems to be doing a few things ok, but not most things I want to burn.
    I am using the instructable “Pocket laser Engraver.” which was fun to build. Can you help me out??
    Thank you.


    • May 20, 2013 at 11:22 pm

      Can’t tell you for sure why, but more often than not, it has to do with your computer or with your hardware/USB cable that has electrical interference. Eliminate the computer by using something like the universal gcode sender and try moving your wires away from anything powered.

  2. February 28, 2015 at 1:33 am

    Great script! I’m triing to establish an XON/XOFF g-code stream from Rhino Grasshopper to a TinyG. I think your python code could work with some modifications as tinyg grew out of grbl shield and GH with a custom python node. Currently im using coolterm to stream gcode. Would you have any advice how to start?

    Thanks Peter

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: