Commit | Line | Data |
---|---|---|
faffe1c2 JM |
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 | |
436daacf | 39 | errorflg= False |
faffe1c2 JM |
40 | |
41 | def 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 | |
60 | t = threading.Thread(target=read_thread) | |
61 | t.daemon = True | |
62 | t.start() | |
63 | ||
64 | linecnt= 0 | |
65 | for 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 |
76 | if errorflg : |
77 | print("Target halted due to errors") | |
faffe1c2 | 78 | |
5d401ee2 JM |
79 | else : |
80 | print("Waiting for complete...") | |
81 | while okcnt < linecnt: | |
82 | if verbose: print(str(linecnt) + " - " + str(okcnt) ) | |
83 | time.sleep(1) | |
18ae0219 JM |
84 | |
85 | # Wait here until finished to close serial port and file. | |
86 | raw_input(" Press <Enter> to exit") | |
faffe1c2 | 87 | |
faffe1c2 JM |
88 | |
89 | # Close file and serial port | |
90 | f.close() | |
91 | s.close() |