blob: 67c2a2c3e15b8058bbee6260f0049f684fd6376d [file] [log] [blame]
Luigi Santivetti69972f92019-11-12 22:55:40 +00001#!/usr/bin/env python
2"""\
3Simple g-code streaming script for grbl
4
5Provided as an illustration of the basic communication interface
6for grbl. When grbl has finished parsing the g-code block, it will
7return an 'ok' or 'error' response. When the planner buffer is full,
8grbl will not send a response until the planner buffer clears space.
9
10G02/03 arcs are special exceptions, where they inject short line
11segments directly into the planner. So there may not be a response
12from grbl for the duration of the arc.
13
14---------------------
15The MIT License (MIT)
16
17Copyright (c) 2012 Sungeun K. Jeon
18
19Permission is hereby granted, free of charge, to any person obtaining a copy
20of this software and associated documentation files (the "Software"), to deal
21in the Software without restriction, including without limitation the rights
22to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23copies of the Software, and to permit persons to whom the Software is
24furnished to do so, subject to the following conditions:
25
26The above copyright notice and this permission notice shall be included in
27all copies or substantial portions of the Software.
28
29THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35THE SOFTWARE.
36---------------------
37"""
38
39import serial
40import time
41
42# Open grbl serial port
43s = serial.Serial('/dev/tty.usbmodem1811',115200)
44
45# Open g-code file
46f = open('grbl.gcode','r');
47
48# Wake up grbl
49s.write("\r\n\r\n")
50time.sleep(2) # Wait for grbl to initialize
51s.flushInput() # Flush startup text in serial input
52
53# Stream g-code to grbl
54for line in f:
55 l = line.strip() # Strip all EOL characters for consistency
56 print 'Sending: ' + l,
57 s.write(l + '\n') # Send g-code block to grbl
58 grbl_out = s.readline() # Wait for grbl response with carriage return
59 print ' : ' + grbl_out.strip()
60
61# Wait here until grbl is finished to close serial port and file.
62raw_input(" Press <Enter> to exit and disable grbl.")
63
64# Close file and serial port
65f.close()
66s.close()