-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-serial.py
executable file
·116 lines (96 loc) · 2.77 KB
/
send-serial.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import colors
import glob
import serial
import sys
import time
def main(device, speed, file=None):
print("# Connecting to {}, speed {}".format(device, speed))
ser = serial.Serial(
port=device,
baudrate=speed
)
print("# Connected to {}, speed {}".format(ser.name, ser.baudrate))
write_bytes("\r\n\r\n", ser)
time.sleep(2) # Wait for grbl to initialize
ser.reset_input_buffer()
if file:
print("# Reading from {}".format(file))
f = open(file, "r")
else:
print("# Reading from stdin")
f = sys.stdin
cancel = False
try:
for line in f:
if cancel:
break
send_line(line, ser)
except KeyboardInterrupt:
cancel = True
finally:
print("\n# Disconnecting from {}".format(ser.name))
f.close()
ser.close()
def send_line(line, ser):
command = line.strip()
if command != '':
print("> {}".format(colors.bold(command)))
write_bytes(command + "\n", ser)
response = str(ser.readline().strip(), 'utf-8')
if response == 'ok':
print('< {}'.format(colors.green(response)))
else:
print('< {}'.format(colors.red(response)))
def write_bytes(unicode_string, ser):
ser.write(unicode_string.encode("utf-8"))
def guess_device():
try:
# macOS
return glob.glob('/dev/tty.usb*')[0]
except IndexError:
try:
# Linux
return glob.glob("/dev/ttyUSB*")[0]
except IndexError:
# Give up
return glob.glob("/dev/*")[0]
description = """
Sends G-Code commands (well, any commands, really) from either
standard input (stdin) or the given file, to a connected Serial device.
"""
examples = """examples:
echo 'M03 S05' | %(prog)s
cat /some/file.gcode | %(prog)s
%(prog)s /some/file.gcode
cat /some/file.gcode | %(prog)s -d /dev/tty.whatever -s 9200
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=description,
epilog=examples,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"file",
metavar="FILE",
nargs="?",
default=None,
help="send lines from the file at the given path, rather than from stdin"
)
parser.add_argument(
"-d",
"--device",
default=guess_device(),
help="path of a serial device to write to (default: %(default)s)"
)
parser.add_argument(
"-s",
"--speed",
default=115200,
help="baud rate to write with (default: %(default)s)"
)
args = parser.parse_args()
main(device=args.device, speed=args.speed, file=args.file)