check for halt condition
[clinton/Smoothieware.git] / fast-stream.py
CommitLineData
faffe1c2
JM
1#!/usr/bin/env python
2"""\
3Stream g-code to Smoothie USB serial connection
4
5Based on GRBL stream.py
6"""
7
8from __future__ import print_function
9import sys
10import argparse
11import serial
12import threading
13import time
14
15# Define command line argument interface
16parser = argparse.ArgumentParser(description='Stream g-code file to Smoothie over telnet.')
17parser.add_argument('gcode_file', type=argparse.FileType('r'),
18 help='g-code filename to be streamed')
19parser.add_argument('device',
20 help='Smoothie Serial Device')
21parser.add_argument('-q','--quiet',action='store_true', default=False,
22 help='suppress output text')
23args = parser.parse_args()
24
25f = args.gcode_file
26verbose = not args.quiet
27
28# Stream g-code to Smoothie
29
30dev= args.device
31
32# Open port
33s = serial.Serial(dev, 115200)
34s.flushInput() # Flush startup text in serial input
35
36print("Streaming " + args.gcode_file.name + " to " + args.device)
37
38okcnt= 0
436daacf 39errorflg= False
faffe1c2
JM
40
41def read_thread():
42 """thread worker function"""
436daacf 43 global okcnt, errorflg
faffe1c2
JM
44 flag= 1
45 while flag :
46 rep= s.readline()
47 n= rep.count("ok")
48 if n == 0 :
49 print("Incoming: " + rep)
436daacf
JM
50 if "error" in rep or "!!" in rep :
51 errorflg= True
52 break
faffe1c2
JM
53 else :
54 okcnt += n
55
56 print("Read thread exited")
57 return
58
59# start read thread
60t = threading.Thread(target=read_thread)
61t.daemon = True
62t.start()
63
64linecnt= 0
65for line in f:
436daacf
JM
66 if errorFlg :
67 break
faffe1c2
JM
68 # strip comments
69 if line.startswith(';') :
70 continue
71 l= line.strip()
72 s.write(l + '\n')
73 linecnt+=1
74 if verbose: print("SND " + str(linecnt) + ": " + line.strip() + " - " + str(okcnt))
75
76print("Waiting for complete...")
77
78while okcnt < linecnt:
79 if verbose: print(str(linecnt) + " - " + str(okcnt) )
80 time.sleep(1)
81
82# Wait here until grbl is finished to close serial port and file.
83raw_input(" Press <Enter> to exit")
84
85# Close file and serial port
86f.close()
87s.close()
88
89print("Done")