pyvisa / pyvisa-py

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

USBTMC session does not apply custom timeout #178

Closed ghost closed 5 years ago

ghost commented 5 years ago

When connecting to an instrument via USBTMC, I find that custom timeout settings are not applied.

For example:

>>> rm = pyvisa.ResourceManager('@py')
>>> instr = rm.open_resource("USB::0x1313::0x8078::P0019304::INSTR")
>>> instr.timeout   # get default timeout
2000
>>> instr.read_raw()  # this will surely time out
... (after 1 second)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
>>> instr.timeout = 5000  # set 5 seconds timeout
>>> instr.read_raw()
... (again after 1 second)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

So it looks like "instr.timeout" is being ignored and a default of 1 second applied instead.

I debugged the timeout handling in pyvisa-py and found a bug in the USBInstrSession class:

    for name in ('SEND_END_EN', 'TERMCHAR', 'TERMCHAR_EN', 'TMO_VALUE'):
        attribute = getattr(constants, 'VI_ATTR_' + name)
        self.attrs[attribute] = attributes.AttributesByID[attribute].default

Direct assignments to self.attrs[] accidentally disable the getter/setter functions that are defined for those attributes. As a consequence, timeout changes will not be applied to the USBTMC protocol instance. Instead of directly manipulating self.attrs, I think it would be better to call self.set_attribute() at this point.

When I make this change, indeed custom timeout settings are correctly applied. I will submit a pull request ...