-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantlog
executable file
·34 lines (30 loc) · 1.11 KB
/
antlog
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
#!/usr/bin/env python
#
# Log data from one or more ANT+ sensors to a file
import sys, os
from antlistener import AntStick
from ant.core.message import ChannelBroadcastDataMessage
def main(serial_device, log_file, device_specs):
chan_to_devspec = dict()
ant_stick = AntStick(serial_device)
for s in device_specs:
chan_to_devspec[ ant_stick.open_channel(s) ] = s
log_fh = open(log_file, 'a')
for timestamp, msg in ant_stick.receive_messages():
if isinstance(msg, ChannelBroadcastDataMessage):
devspec = chan_to_devspec[msg.getChannelNumber()]
payload = " ".join([str(ord(b)) for b in msg.getPayload()[1:]])
log_fh.write("%f %s %s\n" % (timestamp, devspec, payload))
log_fh.flush()
if __name__ == '__main__':
args = sys.argv[1:]
if len(args) < 3:
print >> sys.stderr, "Usage: antlog <ANT_STICK_TTY_DEVICE> <LOGFILE_NAME> <DEVICE_SPEC> [...]"
sys.exit(-1)
tty = args[0]
logfile = args[1]
specs = args[2:]
if not os.path.exists(tty):
print >> sys.stderr, "First argument must be the name of a serial device connected to an ANT+ USB stick, e.g. /dev/ttyUSB0"
sys.exit(-1)
main(tty, logfile, specs)