Sergey Pastor / grbl1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers simple_stream.py Source File

simple_stream.py

00001 #!/usr/bin/env python
00002 """\
00003 Simple g-code streaming script for grbl
00004 
00005 Provided as an illustration of the basic communication interface
00006 for grbl. When grbl has finished parsing the g-code block, it will
00007 return an 'ok' or 'error' response. When the planner buffer is full,
00008 grbl will not send a response until the planner buffer clears space.
00009 
00010 G02/03 arcs are special exceptions, where they inject short line 
00011 segments directly into the planner. So there may not be a response 
00012 from grbl for the duration of the arc.
00013 
00014 ---------------------
00015 The MIT License (MIT)
00016 
00017 Copyright (c) 2012 Sungeun K. Jeon
00018 
00019 Permission is hereby granted, free of charge, to any person obtaining a copy
00020 of this software and associated documentation files (the "Software"), to deal
00021 in the Software without restriction, including without limitation the rights
00022 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00023 copies of the Software, and to permit persons to whom the Software is
00024 furnished to do so, subject to the following conditions:
00025 
00026 The above copyright notice and this permission notice shall be included in
00027 all copies or substantial portions of the Software.
00028 
00029 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00030 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00031 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00032 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00033 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00034 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00035 THE SOFTWARE.
00036 ---------------------
00037 """
00038 
00039 import serial
00040 import time
00041 
00042 # Open grbl serial port
00043 s = serial.Serial('/dev/tty.usbmodem1811',115200)
00044 
00045 # Open g-code file
00046 f = open('grbl.gcode','r');
00047 
00048 # Wake up grbl
00049 s.write("\r\n\r\n")
00050 time.sleep(2)   # Wait for grbl to initialize 
00051 s.flushInput()  # Flush startup text in serial input
00052 
00053 # Stream g-code to grbl
00054 for line in f:
00055     l = line.strip() # Strip all EOL characters for consistency
00056     print 'Sending: ' + l,
00057     s.write(l + '\n') # Send g-code block to grbl
00058     grbl_out = s.readline() # Wait for grbl response with carriage return
00059     print ' : ' + grbl_out.strip()
00060 
00061 # Wait here until grbl is finished to close serial port and file.
00062 raw_input("  Press <Enter> to exit and disable grbl.") 
00063 
00064 # Close file and serial port
00065 f.close()
00066 s.close()