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