Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

libusb - Can't initialize ANT+ Node with Python OpenANT library

I've totally new in Python and also in the ANT+ technology. I wonder if that's not some basic problem, but I've been strugling with it for couple of days already browsing through forums with no luck..

So I'm trying to use the Python OpenANT library (https://github.com/Tigge/openant) to access my ANT doungle which is plugged into the USB port (WINDOWS 10 PRO). My goal is to access my Garmin through it and get some data from it. However, I'm stuck at the very beginning trying to inizialize the ANT Node. My code is this:

from ant.easy.node import Node
node=Node()

To this I get the exception:

  File "C:/Users/Edgars/Desktop/untitled-5.py", line 2, in <module>
    pass
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32Libsite-packagesanteasy
ode.py", line 56, in __init__
    self.ant = Ant()
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32Libsite-packagesantaseant.py", line 68, in __init__
    self._driver.open()
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32Libsite-packagesantasedriver.py", line 193, in open
    cfg = dev.get_active_configuration()
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32libsite-packagespyusb-1.1.0-py3.8.eggusbcore.py", line 909, in get_active_configuration
    return self._ctx.get_active_configuration(self)
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32libsite-packagespyusb-1.1.0-py3.8.eggusbcore.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32libsite-packagespyusb-1.1.0-py3.8.eggusbcore.py", line 250, in get_active_configuration
    bConfigurationValue=self.backend.get_configuration(self.handle)
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32libsite-packagespyusb-1.1.0-py3.8.eggusbackendlibusb0.py", line 519, in get_configuration
    ret = self.ctrl_transfer(
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32libsite-packagespyusb-1.1.0-py3.8.eggusbackendlibusb0.py", line 601, in ctrl_transfer
    return _check(_lib.usb_control_msg(
  File "C:UsersEdgarsAppDataLocalProgramsPythonPython38-32libsite-packagespyusb-1.1.0-py3.8.eggusbackendlibusb0.py", line 447, in _check
    raise USBError(errmsg, ret)
usb.core.USBError: [Errno None] b'libusb0-dll:err [control_msg] sending control message failed, win error: A device which does not exist was specified.

'

I have closed the Garmin Agent, so no other programs are using my ANT dongle at the same time. When I run my code, the specific sound occurs every time - the one that we hear when we detach a USB device by selecting "Eject" from the drop-down menu (the sound happens simultaneously with the exception message), so I guess the USB gets accessed at some moment.

Before the exception I get such a printout:

Driver available: [<class 'ant.base.driver.SerialDriver'>, <class 'ant.base.driver.USB2Driver'>, <class 'ant.base.driver.USB3Driver'>]
 - Using: <class 'ant.base.driver.USB3Driver'>
Could not check if kernel driver was active, not implemented in usb backend

I have seen other users' threads where the printout says Using ... USB1Driver or Using ... USB2Driver, and they don't get this message. I've installed various python libraries trying to get even this far, and now I've worried that maybe they get in each other's way.. Can anybody help me with this? It's really frustrating that a program of only two code lines can get so complicated.. :D

!!!EDIT!!!

OK, I found the problem - in the "driver.py" file there's a line dev.reset() which disconnects my USB dongle before trying to access it. I have no idea why such a line should exist there. I tried to comment this line out, and now I'm not getting the abovementioned error anymore. However, what happens now is there are continuos timeouts..

So my code has evolved to this (although actually the same timeouts happen also with my initial 2-lines-long program):

from ant.easy.node import Node
from ant.easy.channel import Channel
from ant.base.message import Message

import threading

NETWORK_KEY=[0xb9,0xa5,0x21,0xfb,0xbd,0x72,0xc3,0x45]

def on_data(data):
    print("Data received")
    print(data)
    
def back_thread(node):
    node.set_network_key(0x00,NETWORK_KEY)
    channel=node.new_channel(Channel.Type.BIDIRECTIONAL_RECEIVE)
    channel.on_broadcast_data=on_data
    channel.on_burst_data=on_data
    channel.set_period(16070)
    channel.set_search_timeout(20)
    channel.set_rf_freq(57)
    channel.set_id(0,120,0)
    
    try:
        channel.open()
        node.start()
    finally:
        node.stop()
        print("ANT Node Shutdown Complete")

node=Node()
x=threading.Thread(target=back_thread,args=(node,))
x.start()

Now I get this error line printed out for ever:

<class 'usb.core.USBError'>, (None, b'libusb0-dll:err [_usb_reap_async] timeout error
')

When my Garmin Agent is active, I get the error "ANT resource already in use" instead of the timeout, so I'm certain that my code is accessing the ANT dongle.. However, now (having closed the Garmin Agent) I have no idea about how to get rid of the timeout and how to establish a simple handshake with my Garmin device..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

OK, now I've figured out that my Garmin Forerunner 310XT can't act as a data source and thus cannot be accessed using the ANT+ protokol. Instead, I should use the ANT-FS protocol of File Sharing. Keeping my head down and trying it out...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.5k users

...