temperaturecontrol: allow setting background tool without activating
[clinton/Smoothieware.git] / smoothie-stream.py
CommitLineData
d4ee6ee2
JM
1#!/usr/bin/env python
2"""\
3Stream g-code to Smoothie telnet connection
4
5Based on GRBL stream.py
6"""
7
8from __future__ import print_function
9import sys
10import telnetlib
be89f1bd 11import re
d4ee6ee2
JM
12import argparse
13
48a0fdb6
JM
14def write_raw_sequence(tn, seq):
15 sock = tn.get_socket()
16 if sock is not None:
17 sock.send(seq)
18
d4ee6ee2
JM
19# Define command line argument interface
20parser = argparse.ArgumentParser(description='Stream g-code file to Smoothie over telnet.')
21parser.add_argument('gcode_file', type=argparse.FileType('r'),
22 help='g-code filename to be streamed')
23parser.add_argument('ipaddr',
24 help='Smoothie IP address')
25parser.add_argument('-q','--quiet',action='store_true', default=False,
26 help='suppress output text')
be89f1bd
JL
27parser.add_argument('-l','--log',action='store_true', default=False,
28 help='suppress output text and output to file (gcode file with .log appended)')
8c3f0986
JL
29parser.add_argument('-c','--comment',action='store_true', default=False,
30 help='Send gcode comments to printer (text after ;)')
d4ee6ee2
JM
31args = parser.parse_args()
32
33f = args.gcode_file
be89f1bd 34verbose = not (args.quiet or args.log)
d4ee6ee2
JM
35
36# Stream g-code to Smoothie
37print("Streaming " + args.gcode_file.name + " to " + args.ipaddr)
be89f1bd
JL
38outlog = None
39if args.log:
40 outlog = open(args.gcode_file.name + ".log", 'w')
d4ee6ee2
JM
41
42tn = telnetlib.Telnet(args.ipaddr)
48a0fdb6
JM
43# turn on prompt
44#write_raw_sequence(tn, telnetlib.IAC + telnetlib.DO + "\x55")
45
d4ee6ee2 46# read startup prompt
48a0fdb6 47tn.read_until("Smoothie command shell")
d4ee6ee2
JM
48
49okcnt= 0
50linecnt= 0
51for line in f:
8c3f0986
JL
52 if not args.comment:
53 line = re.sub("[ ]*;.*", '', line) # remove everything after ;
54 line = line.strip() #send only the bare necessity.
55 if len(line) > 0:
721b502e 56 tn.write(line + "\n")
8c3f0986
JL
57 linecnt+=1
58 rep= tn.read_eager()
59 okcnt += rep.count("ok")
60 if verbose: print("SND " + str(linecnt) + ": " + line.strip() + " - " + str(okcnt))
61 if args.log: outlog.write("SND " + str(linecnt) + ": " + line.strip() + " - " + str(okcnt) + "\n" )
18ad8ca9
JL
62print("Waiting for complete...")
63
64while okcnt < linecnt:
65 rep= tn.read_some()
66 okcnt += rep.count("ok")
67 if verbose: print(str(linecnt) + " - " + str(okcnt) )
68 if args.log: outlog.write(str(linecnt) + " - " + str(okcnt) + "\n" )
be89f1bd 69
d4ee6ee2 70
be89f1bd 71if args.log: outlog.close()
d4ee6ee2
JM
72tn.write("exit\n")
73tn.read_all()
74
75print("Done")
76
77
78