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