/
cli.py
executable file
·157 lines (143 loc) · 4.76 KB
/
cli.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python3
"""This file contains the main program for the bioloid command line
interface.
"""
import argparse
import os
import sys
import serial
import logging
from bioloid.command_line import CommandLine
from bioloid.serial_bus import SerialPort, SerialBus
from bioloid.socket_bus import SocketBus
from bioloid.device_type_parser import DeviceTypeParser
from bioloid.device_type import DeviceTypes
from bioloid.log_setup import log_setup
from bioloid.test_bus import TestBus
def main():
"""The main program."""
default_baud = 1000000
default_port = os.getenv("BIOLOID_PORT")
parser = argparse.ArgumentParser(
prog="bioloid",
usage="%(prog)s [options] [command]",
description="Send commands to bioloid devices",
epilog=("You can specify the default serial port using the " +
"BIOLOID_PORT environment variable.")
)
parser.add_argument(
"-b", "--baud",
dest="baud",
action="store",
type=int,
help="Set the baudrate used (default = %d)" % default_baud,
default=default_baud
)
default_port_help = ""
if default_port:
default_port_help = " (default '%s')" % default_port
parser.add_argument(
"-p", "--port",
dest="port",
help="Set the serial port to use" + default_port_help,
default=default_port
)
parser.add_argument(
"-n", "--net",
dest="net",
action="store_true",
help="Connect to a test_dev_server (localhost:8888)"
)
parser.add_argument(
"-t", "--test",
dest="test",
action="store_true",
help="Uses the TestBus rather than communicating with real devices."
)
parser.add_argument(
"-f", "--file",
dest="filename",
help="Specifies a file of commands to process."
)
parser.add_argument(
"-d", "--debug",
dest="debug",
action="store_true",
help="Enable debug features",
default=False
)
parser.add_argument(
"-s", "--stats",
dest="stats",
action="store_true",
help="Print some packet stats (at the end of test run)",
default=False
)
parser.add_argument(
"-v", "--verbose",
dest="verbose",
action="store_true",
help="Turn on verbose messages",
default=False
)
parser.add_argument(
"cmd",
nargs="*",
help="Optional command to execute"
)
args = parser.parse_args(sys.argv[1:])
script_dir = os.path.dirname(os.path.realpath(__file__))
log_setup()
log = logging.getLogger()
if args.debug:
log.setLevel(logging.DEBUG)
if args.verbose:
log.info("Baud = %d", args.baud)
log.info("Port = %s", args.port)
log.info("Net = %s", args.net)
log.info("Test = %s", args.test)
log.info("Debug = %s", args.debug)
log.info("Cmd = [%s]", ', '.join(args.cmd))
log.info("script_dir = %s", script_dir)
if not args.port and not args.net and not args.test:
log.error("Must specify one of network, serial, or test")
sys.exit(1)
dev_types = DeviceTypes()
parser = DeviceTypeParser(dev_types)
parser.parse_dev_type_files(script_dir)
bus = None
if args.test:
bus = TestBus(show_packets=args.debug)
elif args.port:
try:
serial_port = SerialPort(port=args.port, baudrate=args.baud)
except serial.serialutil.SerialException:
log.error("Unable to open port '%s'", args.port)
sys.exit(1)
bus = SerialBus(serial_port, show_packets=args.debug)
elif args.net:
bus = SocketBus(show_packets=args.debug)
else:
log.error('No bus specified')
sys.exit(1)
if args.filename:
with open(args.filename) as cmd_file:
cmd_line = CommandLine(bus, dev_types, stdin=cmd_file,
filename=args.filename)
cmd_line.auto_cmdloop('')
else:
cmd_line = CommandLine(bus, dev_types)
cmd_line.auto_cmdloop(' '.join(args.cmd))
if args.test:
log.info("--------------------------")
log.info("Passed: %d Failed: %d",
bus.get_pass_count(), bus.get_fail_count())
if args.stats:
log.info('')
log.info(' Packets read: {}'.format(bus.packets_read_count))
log.info(' Packets written: {}'.format(bus.packets_written_count))
log.info(' Bytes read: {}'.format(bus.packet_bytes_read))
log.info(' Bytes written: {}'.format(bus.packet_bytes_written))
log.info(' Max Packet read: {}'.format(bus.max_packet_size_read))
log.info('Max Packet written: {}'.format(bus.max_packet_size_written))
main()