USB resource is not closed properly
cpagravel opened this issue · comments
Issue details
I run into an error every second time I try to run a script that accesses a pyvisa device. The first time I run the script, it works; the second time I run the script, it fails with the following error:
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
I modified the script to open and close the device twice to demonstrate the issue without needing to run the script twice.
My script:
for _ in range(2):
resource_manager = pyvisa.ResourceManager()
resource_string = resource_manager.list_resources(
query=f"?*::MY56005018::?*INSTR")[0]
device = resource_manager.open_resource(resource_string)
resp = device.query("*IDN?")
device.close()
Traceback:
File "/home/me/testy/keysight_auto_script.py", line 23, in test_keysight_2
resp = device.query("*IDN?")
File "/home/me/.virtualenvs/p3/lib/python3.9/site-packages/pyvisa/resources/messagebased.py", line 648, in query
return self.read()
File "/home/me/.virtualenvs/p3/lib/python3.9/site-packages/pyvisa/resources/messagebased.py", line 486, in read
message = self._read_raw().decode(enco)
File "/home/me/.virtualenvs/p3/lib/python3.9/site-packages/pyvisa/resources/messagebased.py", line 442, in _read_raw
chunk, status = self.visalib.read(self.session, size)
File "/home/me/.virtualenvs/p3/lib/python3.9/site-packages/pyvisa_py/highlevel.py", line 519, in read
return data, self.handle_return_value(session, status_code)
File "/home/me/.virtualenvs/p3/lib/python3.9/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.
Test without using USB
I only run into the issue if I'm using communication over USB. If I use a different communication method, such as TCPIP
, then I don't have any problems.
for _ in range(2):
resource_manager = pyvisa.ResourceManager()
resource_string = resource_manager.list_resources(
query="TCPIP::100.127.36.128::INSTR")[0] # Notice that I'm using TCPIP here.
device = resource_manager.open_resource(resource_string)
resp = device.query("*IDN?")
device.close()
Environment Details
- Using a Keysight N6705b
Output of pyvisa-info
Machine Details:
Platform ID: Linux-5.10.0-21-amd64-x86_64-with-glibc2.31
Processor:
Python:
Implementation: CPython
Executable: /home/gtvchrome/.virtualenvs/p3/bin/python
Version: 3.9.2
Compiler: GCC 10.2.1 20210110
Bits: 64bit
Build: Feb 28 2021 17:03:44 (#default)
Unicode: UCS4
PyVISA Version: 1.13.0
Backends:
ivi:
Version: 1.13.0 (bundled with PyVISA)
Binary library: Not found
py:
Version: 0.6.1
ASRL INSTR: Available via PySerial (3.5)
USB INSTR: Available via PyUSB (1.2.1). Backend: libusb1
USB RAW: Available via PyUSB (1.2.1). Backend: libusb1
TCPIP INSTR: Available
Resource discovery:
- VXI-11: ok
- hislip: ok
VICP INSTR:
Please install PyVICP to use this resource type.
TCPIP SOCKET: Available
GPIB INSTR:
Please install linux-gpib (Linux) or gpib-ctypes (Windows, Linux) to use this resource type. Note that installing gpib-ctypes will give you access to a broader range of funcionality.
No module named 'gpib'
Thanks for the detailed report. This is reminiscent of pyusb/pyusb#432
Could you try fiddling with the call to set_configuration (https://github.com/pyvisa/pyvisa-py/blob/main/pyvisa_py/protocols/usbtmc.py#L216) ? Commenting it out/making it conditional on the device not being configured yet ?
Since I do not have the hardware to reproduce locally, I will need you to test possible fixes, sorry for the inconvenience.
@MatthieuDartiailh thanks for pointing me in the right direction. I have a PR to address this which works on in my environment: #352