| 1 | #!/usr/bin/env python |
| 2 | """\ |
| 3 | Stream g-code to Smoothie telnet connection |
| 4 | |
| 5 | Based on GRBL stream.py |
| 6 | """ |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | import sys |
| 10 | import telnetlib |
| 11 | import re |
| 12 | import argparse |
| 13 | |
| 14 | def write_raw_sequence(tn, seq): |
| 15 | sock = tn.get_socket() |
| 16 | if sock is not None: |
| 17 | sock.send(seq) |
| 18 | |
| 19 | # Define command line argument interface |
| 20 | parser = argparse.ArgumentParser(description='Stream g-code file to Smoothie over telnet.') |
| 21 | parser.add_argument('gcode_file', type=argparse.FileType('r'), |
| 22 | help='g-code filename to be streamed') |
| 23 | parser.add_argument('ipaddr', |
| 24 | help='Smoothie IP address') |
| 25 | parser.add_argument('-q','--quiet',action='store_true', default=False, |
| 26 | help='suppress output text') |
| 27 | parser.add_argument('-l','--log',action='store_true', default=False, |
| 28 | help='suppress output text and output to file (gcode file with .log appended)') |
| 29 | parser.add_argument('-c','--comment',action='store_true', default=False, |
| 30 | help='Send gcode comments to printer (text after ;)') |
| 31 | args = parser.parse_args() |
| 32 | |
| 33 | f = args.gcode_file |
| 34 | verbose = not (args.quiet or args.log) |
| 35 | |
| 36 | # Stream g-code to Smoothie |
| 37 | print("Streaming " + args.gcode_file.name + " to " + args.ipaddr) |
| 38 | outlog = None |
| 39 | if args.log: |
| 40 | outlog = open(args.gcode_file.name + ".log", 'w') |
| 41 | |
| 42 | tn = telnetlib.Telnet(args.ipaddr) |
| 43 | # turn on prompt |
| 44 | #write_raw_sequence(tn, telnetlib.IAC + telnetlib.DO + "\x55") |
| 45 | |
| 46 | # read startup prompt |
| 47 | tn.read_until("Smoothie command shell") |
| 48 | |
| 49 | okcnt= 0 |
| 50 | linecnt= 0 |
| 51 | for line in f: |
| 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: |
| 56 | tn.write(line + "\n") |
| 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" ) |
| 62 | print("Waiting for complete...") |
| 63 | |
| 64 | while 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" ) |
| 69 | |
| 70 | |
| 71 | if args.log: outlog.close() |
| 72 | tn.write("exit\n") |
| 73 | tn.read_all() |
| 74 | |
| 75 | print("Done") |
| 76 | |
| 77 | |
| 78 | |