forked from Smoothieware/Smoothieware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smoothie-stream.py
78 lines (62 loc) · 2.25 KB
/
smoothie-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
#!/usr/bin/env python
"""\
Stream g-code to Smoothie telnet connection
Based on GRBL stream.py
"""
from __future__ import print_function
import sys
import telnetlib
import re
import argparse
def write_raw_sequence(tn, seq):
sock = tn.get_socket()
if sock is not None:
sock.send(seq)
# 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('ipaddr',
help='Smoothie IP address')
parser.add_argument('-q','--quiet',action='store_true', default=False,
help='suppress output text')
parser.add_argument('-l','--log',action='store_true', default=False,
help='suppress output text and output to file (gcode file with .log appended)')
parser.add_argument('-c','--comment',action='store_true', default=False,
help='Send gcode comments to printer (text after ;)')
args = parser.parse_args()
f = args.gcode_file
verbose = not (args.quiet or args.log)
# Stream g-code to Smoothie
print("Streaming " + args.gcode_file.name + " to " + args.ipaddr)
outlog = None
if args.log:
outlog = open(args.gcode_file.name + ".log", 'w')
tn = telnetlib.Telnet(args.ipaddr)
# turn on prompt
#write_raw_sequence(tn, telnetlib.IAC + telnetlib.DO + "\x55")
# read startup prompt
tn.read_until("Smoothie command shell")
okcnt= 0
linecnt= 0
for line in f:
if not args.comment:
line = re.sub("[ ]*;.*", '', line) # remove everything after ;
line = line.strip() #send only the bare necessity.
if len(line) > 0:
tn.write(line + "\n")
linecnt+=1
rep= tn.read_eager()
okcnt += rep.count("ok")
if verbose: print("SND " + str(linecnt) + ": " + line.strip() + " - " + str(okcnt))
if args.log: outlog.write("SND " + str(linecnt) + ": " + line.strip() + " - " + str(okcnt) + "\n" )
print("Waiting for complete...")
while okcnt < linecnt:
rep= tn.read_some()
okcnt += rep.count("ok")
if verbose: print(str(linecnt) + " - " + str(okcnt) )
if args.log: outlog.write(str(linecnt) + " - " + str(okcnt) + "\n" )
if args.log: outlog.close()
tn.write("exit\n")
tn.read_all()
print("Done")