-
Notifications
You must be signed in to change notification settings - Fork 0
/
nano_manual_cmd
executable file
·78 lines (57 loc) · 1.48 KB
/
nano_manual_cmd
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
#! /usr/bin/env python
#
# This uses a mux_server.py (from mux_serial) on the localhost to run a
# commmand and prints out the next line it sees, this may not be the
# output of the command you requested if the arduino gets distracted
#
# -- Daniel Frederick Crisman
# MIT license 2016
#
import os, sys
import socket
import optparse
# Option parsing, duh
parser = optparse.OptionParser()
parser.add_option('-p', '--port',
help = 'Host port',
dest = 'port',
type = 'int',
default = 23200)
parser.add_option('-c', '--command',
help = 'Command to send',
dest = 'cmd',
type = 'string')
(opts, args) = parser.parse_args()
# Setup client
server_address = ('localhost', opts.port)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(server_address)
# format command for the on wire serial format we use
if opts.cmd[0] != '(':
opts.cmd = '(' + opts.cmd
if opts.cmd[-1] != ')':
opts.cmd += ')'
opts.cmd = opts.cmd.upper()
print >>sys.stderr, 'MUX > Connected to %s:%d' % server_address
print >>sys.stderr, 'MUX > Use ctrl+c to stop...\n'
print >>sys.stderr, 'MUX > Command is "%s"\n' % opts.cmd
current_line = ''
s.send(opts.cmd)
##### MAIN
while True:
try:
# Read 1 char
x = s.recv(1)
# Ignore carriage returns
if x == '\r':
continue
current_line += x
if x == '\n':
print current_line
current_line = ''
break
except:
break
print >>sys.stderr, '\nMUX > Closing...'
s.close()
print >>sys.stderr, 'MUX > Done! =)'