-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantscan
executable file
·46 lines (43 loc) · 1.73 KB
/
antscan
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
#!/usr/bin/env python
#
# Scan for ANT+ sensors and print out their types and uids
import sys, os
from antlistener import AntStick
from ant.core.message import ChannelBroadcastDataMessage, ChannelRequestMessage, ChannelIDMessage
from ant.core.constants import MESSAGE_CHANNEL_ID
def main(serial_device):
ant_stick = AntStick(serial_device)
ant_stick.open_channel('0')
device_type_uid = [None]
for timestamp, msg in ant_stick.receive_messages():
if isinstance(msg, ChannelBroadcastDataMessage):
cnum = msg.getChannelNumber()
if device_type_uid[cnum] is None:
# We don't know what this channel has paired with, ask the stick.
msg = ChannelRequestMessage(number=cnum, message_id=MESSAGE_CHANNEL_ID)
ant_stick.send_msg(msg, False)
elif isinstance(msg, ChannelIDMessage):
# Response to a message channel id request
devtype = msg.getDeviceType()
uid = msg.getDeviceNumber()
if devtype == 0 or uid == 0:
# Nope, the stick hasn't yet worked out what device is connected.
continue
cnum = msg.getChannelNumber()
if device_type_uid[cnum] is None:
# The first such response on this channel
print "Paired with device TYPE=%d UID=%d on channel %d" % (devtype, uid, cnum)
device_type_uid[cnum] = "%d.%d" % (devtype, uid)
# As this channel is now paired with a device, we need to open another to scan for more.
ant_stick.open_channel('0')
device_type_uid.append(None)
if __name__ == '__main__':
args = sys.argv[1:]
if len(args) != 1:
print >> sys.stderr, "Usage: antscan <ANT_STICK_TTY_DEVICE>"
sys.exit(-1)
tty = args[0]
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)