handle target being in halt state
[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:
5d401ee2 66 if errorflg :
436daacf 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
5d401ee2
JM
76if errorflg :
77 print("Target halted due to errors")
faffe1c2 78
5d401ee2
JM
79else :
80 print("Waiting for complete...")
81 while okcnt < linecnt:
82 if verbose: print(str(linecnt) + " - " + str(okcnt) )
83 time.sleep(1)
84 # Wait here until finished to close serial port and file.
85 raw_input(" Press <Enter> to exit")
faffe1c2 86
faffe1c2
JM
87
88# Close file and serial port
89f.close()
90s.close()