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