forked from Smoothieware/Smoothieware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast-stream.py
125 lines (98 loc) · 2.92 KB
/
fast-stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
"""\
Stream g-code to Smoothie USB serial connection
Based on GRBL stream.py, but completely different
"""
from __future__ import print_function
import sys
import argparse
import serial
import threading
import time
import signal
import sys
errorflg = False
intrflg = False
def signal_term_handler(signal, frame):
global intrflg
print('got SIGTERM...')
intrflg = True
signal.signal(signal.SIGTERM, signal_term_handler)
# Define command line argument interface
parser = argparse.ArgumentParser(description='Stream g-code file to Smoothie over telnet.')
parser.add_argument('gcode_file', type=argparse.FileType('r'), help='g-code filename to be streamed')
parser.add_argument('device', help='Smoothie Serial Device')
parser.add_argument('-q', '--quiet', action='store_true', default=False, help='suppress output text')
args = parser.parse_args()
f = args.gcode_file
verbose = not args.quiet
# Stream g-code to Smoothie
dev = args.device
# Open port
s = serial.Serial(dev, 115200)
s.flushInput() # Flush startup text in serial input
print("Streaming " + args.gcode_file.name + " to " + args.device)
okcnt = 0
def read_thread():
"""thread worker function"""
global okcnt, errorflg
flag = 1
while flag:
rep = s.readline().decode('latin1')
n = rep.count("ok")
if n == 0:
print("Incoming: " + rep)
if "error" in rep or "!!" in rep or "ALARM" in rep or "ERROR" in rep:
errorflg = True
break
else:
okcnt += n
print("Read thread exited")
return
# start read thread
t = threading.Thread(target=read_thread)
t.daemon = True
t.start()
linecnt = 0
try:
for line in f:
if errorflg:
break
# strip comments
if line.startswith(';'):
continue
l = line.strip()
o = "{}\n".format(l).encode('latin1')
n = s.write(o)
if n != len(o):
print("Not entire line was sent: {} - {}".format(n, len(o)))
linecnt += 1
if verbose:
print("SND " + str(linecnt) + ": " + line.strip() + " - " + str(okcnt))
except KeyboardInterrupt:
print("Interrupted...")
intrflg = True
if intrflg:
# We need to consume oks otherwise smoothie will deadlock on a full tx buffer
print("Sending Abort - this may take a while...")
s.write(b'\x18') # send halt
while(s.inWaiting()):
s.read(s.inWaiting())
linecnt = 0
if errorflg:
print("Target halted due to errors")
else:
print("Waiting for complete...")
while okcnt < linecnt:
if verbose:
print(str(linecnt) + " - " + str(okcnt))
if errorflg:
s.read(s.inWaiting()) # rad all remaining characters
break
time.sleep(1)
# Wait here until finished to close serial port and file.
print(" Press <Enter> to exit")
input()
# Close file and serial port
f.close()
s.close()