forked from piit79/python-llap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllap-tool.py
executable file
·82 lines (69 loc) · 2.26 KB
/
llap-tool.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
#!/usr/bin/env python
import argparse
import llap
import threading
import time
def monitor(transceiver, args):
"""Monitor and log LLAP meesages."""
print "Monitoring LLAP traffic on %s..." % args.device
try:
while True:
pass
except:
return
def address(transceiver, args):
"""Set address of a LLAP device."""
started = threading.Event()
addr = [None]
def handler(address, message):
addr
if message == 'STARTED':
addr[0] = address
started.set()
transceiver.handler = handler
print "Waiting for the device..."
started.wait()
dev = llap.Device(addr[0], transceiver, 'STARTED')
dev.wait_start()
print "Found device with address %s, changing to %s..." % \
(addr[0], args.address)
ret = dev.chdevid(args.address)
if ret is None:
print "Error changing device address!"
return
print "Address changed, waiting for the device to reboot..."
dev.wait_start(3)
if dev.started:
print "Device started with the new address."
else:
print "Device did not start."
def message(transceiver, args):
"""Send a message to a device."""
dev = llap.Device(args.address, transceiver)
msg = args.message.upper()
print "Sending message %s to device %s..." % (msg, args.address)
dev.send(msg, wait=True)
def main():
"""Main function."""
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--device', required=True)
parser.add_argument('-b', '--baudrate', default=9600)
subparsers = parser.add_subparsers()
parser_monitor = subparsers.add_parser('monitor')
parser_monitor.set_defaults(func=monitor)
parser_address = subparsers.add_parser('address')
parser_address.add_argument('address')
parser_address.set_defaults(func=address)
parser_message = subparsers.add_parser('message')
parser_message.add_argument('address')
parser_message.add_argument('message')
parser_message.set_defaults(func=message)
args = parser.parse_args()
try:
tcvr = llap.Transceiver(args.device, args.baudrate, debug=True)
except:
print "Cannot open serial port %s!" % args.device
return
args.func(tcvr, args)
if __name__ == '__main__':
main()