pyvisa / pyvisa-py

A pure python PyVISA backend
https://pyvisa-py.readthedocs.io
MIT License
282 stars 120 forks source link

Attempt to stabilize USB resource access #335

Closed MatthieuDartiailh closed 1 year ago

MatthieuDartiailh commented 2 years ago

This removes device resets and only configure an alternative setting if it makes sense for the interface.

ping @knuesel

MatthieuDartiailh commented 2 years ago

ping @tpchuckles @pfjarschel @mwcmwc12

knuesel commented 2 years ago

Thanks for working on this! I get the following error:

>>> device = rm.open_resource(addr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa/highlevel.py", line 3284, in open_resource
    res.open(access_mode, open_timeout)
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa/resources/resource.py", line 278, in open
    self.session, status = self._resource_manager.open_bare_resource(
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa/highlevel.py", line 3209, in open_bare_resource
    return self.visalib.open(self.session, resource_name, access_mode, open_timeout)
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa_py/highlevel.py", line 167, in open
    sess = cls(session, resource_name, parsed, open_timeout)
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa_py/sessions.py", line 325, in __init__
    self.after_parsing()
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa_py/usb.py", line 81, in after_parsing
    self.interface = self._intf_cls(
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa_py/protocols/usbtmc.py", line 303, in __init__
    super(USBTMC, self).__init__(vendor, product, serial_number, **kwargs)
  File "/home/j/.local/lib/python3.10/site-packages/pyvisa_py/protocols/usbtmc.py", line 229, in __init__
    len(
TypeError: object of type 'generator' has no len()
MatthieuDartiailh commented 2 years ago

Thanks for testing. I could not do it locally and went too quickly through pyusb docs. It should be fixed now.

knuesel commented 2 years ago

This seems to work well with my SDG1032X: no more freezes, and it answers *IDN? and C1:OUTP?. It also works with my SPD1168X, SDM3045X and SDS1104X-E as far as I can see. Thanks!

MatthieuDartiailh commented 2 years ago

Thanks for testing. I will wait for more reports from other users before merging. We have often seen such fixes backfire on other hardware hence why I want to be prudent. If you can test with hardware from different manufacturers it would be great.

knuesel commented 2 years ago

Sounds good. I don't have any other device to test with unfortunately.

I forgot to mention: with the SDG device I still get the following warning when calling dev.query('*IDN?'):

/home/j/.local/lib/python3.10/site-packages/pyvisa_py/protocols/usbtmc.py:116: UserWarning: Unexpected
MsgID format. Consider updating the device's firmware. See https://github.com/pyvisa/pyvisa-py/issues/20
  warnings.warn(

but I guess that's unrelated.

MatthieuDartiailh commented 2 years ago

It is unrelated yes. You are probably better off just silencing the warning if there is no other firmware available.

MatthieuDartiailh commented 1 year ago

Friendly ping @tpchuckles @pfjarschel @mwcmwc12