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:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\ant\easy\node.py", line 56, in __init__
self.ant = Ant()
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\ant\base\ant.py", line 68, in __init__
self._driver.open()
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\ant\base\driver.py", line 193, in open
cfg = dev.get_active_configuration()
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyusb-1.1.0-py3.8.egg\usb\core.py", line 909, in get_active_configuration
return self._ctx.get_active_configuration(self)
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyusb-1.1.0-py3.8.egg\usb\core.py", line 113, in wrapper
return f(self, *args, **kwargs)
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyusb-1.1.0-py3.8.egg\usb\core.py", line 250, in get_active_configuration
bConfigurationValue=self.backend.get_configuration(self.handle)
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyusb-1.1.0-py3.8.egg\usb\backend\libusb0.py", line 519, in get_configuration
ret = self.ctrl_transfer(
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyusb-1.1.0-py3.8.egg\usb\backend\libusb0.py", line 601, in ctrl_transfer
return _check(_lib.usb_control_msg(
File "C:\Users\Edgars\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyusb-1.1.0-py3.8.egg\usb\backend\libusb0.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.\r\n\n'
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\n')
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..
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...
I posted a PR with some changes that I made to get Tigge’s openant library to work. Basically, I put a pause after the reset line that you mentioned above and bypassed the use of udev_rules as it doesn’t apply in Windows. You can use libusb but installation is a bit different. I’ve added Windows installation instructions to the readme in the PR with details on what worked for me.
Related
Python Version = 3.9,
Foreman Version = 2.5.4
I'm using the Python Foreman (python-foreman) module to connect to Foreman to do a bunch of automation tasks, however as soon as i'm connecting to Foreman as below:
from foreman.client import Foreman
f = Foreman('http://foreman-url.com/api/hosts', ('admin', 'password'), api_version = 2)
I'm getting a bunch of conflicting statements as below and eventually erroring out.
There is a conflict trying to redefine a method for a foreign resource (foreman_tasks_index):
resource:
apipie_resource: foreman_tasks
new_api: <resource:, name:foreman_tasks_index>
new_url: /foreman_tasks/api/tasks/:parent_task_id/sub_tasks
old_api: <resource:, name:foreman_tasks_index>
old_url: /foreman_tasks/api/tasks
There is a conflict trying to redefine a method for a foreign resource (sync_plans_rganizations):
resource:
apipie_resource: sync_plans
new_api: <resource:organizations, name:sync_plans_rganizations>
new_url: /katello/api/organizations/:organization_id/sync_plans/:id/add_products
old_api: <resource:organizations, name:sync_plans_rganizations>
old_url: /katello/api/organizations/:organization_id/sync_plans/:id
Eventually the script is erroring out at this:
Traceback (most recent call last):
File "", line 1, in
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 619, in init
self._generate_api_defs(use_cache, strict_cache)
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 820, in generate_api_defs
new_resource, extra_foreign_methods = parse_resource_definition(
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 375, in parse_resource_definition
functions[api.name] = api.generate_func()
File "/data/sfreport/python3.9/lib/python3.9/site-packages/foreman/client.py", line 282, in generate_func
six.exec(code)
File "", line 1
def lifecycle_environments_update(self, id, organization_id=None, new_name=None, description=None, registry_name_pattern=None, registry_unauthenticated_pull=None, async=None):
^
SyntaxError: invalid syntax
Can you pls help with this or let me know the best way to connect to Foreman API via Python to carry out the automations?
The error message you are getting states that (sine Python 3.5) async is a reserved keyword, and cannot be a variable name. This means that the version of Foreman you are using is incompatible with Python 3.9. Try updating Foreman, downgrading Python, or (if you are comfortable doing it) try to fix the issue yourself (changing the variable name from async to something else, and hoping it works).
I am not sure whether the offending code comes directly from Foreman. Try to search your code (and libraries) for async=None pattern, as it may also not be Foreman's fault.
I was trying to set up MicroPython on PyCharm 2021.3.3, unfortunately I was not able to successfully connect my board to IDE. I'm getting trying to use tools>>MicroPython>>MicroPython REPL:
Found the device, but could not connect via port 'COM4': could not open port 'COM4': PermissionError(13, 'Odmowa dostępu.', None, 5)
I'm not sure what to suggest. :-(
Press ENTER to continue
Then I have tried to flush simple program to control despite previous error and it spits out:
Connecting to COM4
Traceback (most recent call last):
File "C:\Users\reczul\AppData\Roaming\JetBrains\PyCharm2021.3\plugins\intellij-micropython\scripts\microupload.py", line 139, in <module>
main(sys.argv[1:])
File "C:\Users\reczul\AppData\Roaming\JetBrains\PyCharm2021.3\plugins\intellij-micropython\scripts\microupload.py", line 56, in main
board = Pyboard(port)
File "C:\Users\reczul\PycharmProjects\pythonProject1\venv\lib\site-packages\ampy\pyboard.py", line 147, in __init__
raise PyboardError('failed to access ' + device)
ampy.pyboard.PyboardError: failed to access COM4
What can I do to fix this error?
The sample code I have used:
from machine import Pin, Timer
led = Pin(25, Pin.OUT)
tim = Timer()
def tick(timer):
global led
led.toggle()
tim.init(freq=2.5, mode=Timer.PERIODIC, callback=tick)```
Assuming you are using a Pi Pico.
You are most likely getting this error because you pressed the 'BOOTSEL' button while plugging your device in.
Drag your UF2 file onto the device, it should reboot.
Don't unplug the device and hold the 'BOOTSEL' this time.
It should be good to go now.
I am using a peaktech 4046 : 160MHz Function/arbitrary Waveform Generator. I developping on pyton and I am using the pyvisa librairy.
The connection is well established and the generator applies the query. But it generates the following error and stops the program (it doesn't do anything after the error).
Here is the code :
import pyvisa
rm = pyvisa.ResourceManager()
inst = rm.open_resource('TCPIP0::130.79.192.123::5025::SOCKET')
print(inst.session)
print(inst.io_protocol)
inst.query("source1:function squ")
And here is what I have in my terminal :
2
IOProtocol.normal
Traceback (most recent call last):
File "c:\Users\Labo préclinique\Desktop\ProjetPython\importation de librairies\Forum.py", line 7, in <module>
inst.query("source1:function squ ")
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\resources\messagebased.py", line 644, in query
return self.read()
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\resources\messagebased.py", line 486, in read
message = self._read_raw().decode(enco)
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\resources\messagebased.py", line 442, in _read_raw
chunk, status = self.visalib.read(self.session, size)
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\ctwrapper\functions.py", line 2337, in read
ret = library.viRead(session, buffer, count, byref(return_count))
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
return self.handle_return_value(session, ret_value) # type: ignore
File "C:\Users\Labo préclinique\AppData\Local\Programs\Python\Python39\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
I have tried (to no avail) :
-changing SOCKET to INSTR
-using a timout much longeur ( inst.timeout = 10000)
-adding a end term (tried \n and \r) with : inst.read_termination = '\n'
So I don't know what to do anymore... I need to give more than one command, so the program must not stop so fast. I suspect that my function generator is not sending anything back, but I don't know how to make sure this is the case.
What I wish to know is : Why do I have a time out error if the connection is well established and the request is executed on the device ? How to do the request in a proper way ?
Thank you in advance !!
PS : I know how to catch the error (with try except) but I'd rather have an Ok answer thant a KO one.
Try to get a list of resources by
rm.list_resources()
and check that your resource TCPIP0::130.79.192.123::5025::SOCKET in it.
Then check the standard request to the resource from tutorial:
inst.query("*IDN?")
query is a short form for a write operation to send a message, followed by a read. So you could do this in two actions to specify the error(read or write error?):
inst.write('"source1:function squ"')
print(inst.read())
Please, check the name of query source1:function squ because I don't see it in the documentation. Maybe you should use "source1:am:interanal:function square(p. 57 of documentation) or change squ -> square?
Accordingly documentation, you could try to set infinite timeout to your request by
del inst.timeout
Also, you could add read_termination/write_termination option to specify when you'll finish your reading/writing by
inst = rm.open_resource('TCPIP0::130.79.192.123::5025::SOCKET', read_termination='\r')
And the last chance is changing the options query_delay and send_end.
So - I am trying to get a file from point A to point B. I use the following piece of code:
from scp import SCPClient
try:
scp = SCPClient(ssh_conn.get_transport())
scp.get(X, Y)
scp.close()
Everything was working well until the file grew big enough to raise timeout. Now I can not copy the file to point B anymore because it just times out with the following error:
File "/usr/local/lib/python2.7/dist-packages/scp.py", line 415, in _recv_file
raise SCPException('Error receiving, socket.timeout')
scp.SCPException: Error receiving, socket.timeout
How do I increase the time or get rid of the timeout?
This may help:
scp = SCPClient(ssh_conn.get_transport(), socket_timeout=your_timeout)
The code I write now works fine, I can even print the deserialized objects with no mistakes whatsoever, so I do know exactly what is in there.
#staticmethod
def receiveData(self):
'''
This method has to be static, as it is the argument of a Thread.
It receives Wrapperobjects from the server (as yet containing only a player)
and resets the local positions accordingly
'''
logging.getLogger(__name__).info("Serverinformationen werden nun empfangen")
from modules.logic import game
sock = self.sock
time.sleep(10)
self.myPlayer = game.get_player()
while (True):
try:
wrapPacked = sock.recv(4096)
self.myList = cPickle.loads(wrapPacked)
# self.setData(self.myList)
except Exception as eload:
print eload
However, if I try to actually use the line that is in comments here (self.setData(self.myList),
I get
unpickling stack underflow
and
invalid load key, ' '.
Just for the record, the code of setData is:
def setData(self, list):
if (list.__sizeof__()>0):
first = list [0]
self.myPlayer.setPos(first[1])
self.myPlayer.setVelocity(first[2])
I have been on this for 3 days now, and really, I have no idea what is wrong.
Can you help me?
Full Traceback:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "mypath/client.py", line 129, in receiveData
self.myList = cPickle.loads(wrapPacked)
UnpicklingError: unpickling stack underflow –
The fact that your exceptions always happen when you try to access the pickled data seem to indicate that you are hitting a bug in the cPickle library instead.
What can happen is that a C library forgets to handle an exception. The exception info is stored, not handled, and is sitting there in the interpreter until another exception happens or another piece of C code does check for an exception. At this point the old, unhandled exception is thrown instead.
Your error is clearly cPickle related, it is very unhappy about the data you feed it, but the exception itself is thrown in unrelated locations. This could be threading related, it could be a regular non-threading-related bug.
You need to see if you can load the data in a test setting. Write wrapPacked to a file for later testing. Load that file in a interpreter shell session, load it with cPickle.loads() and see what happens. Do the same with the pickle module.
If you do run into similar problems in this test session, and you can reproduce it (weird exceptions being thrown at a later point in the session) you need to file a bug with the Python project to have this looked at.