PyVisa-USB timeout error cannot be cleared without reboot?

796 Views Asked by At

I need some advice on how to deal with USB timeout errors.

I am working on an app that is communicating with my Anritsu instrument over USB-GPIB port. The app works fine, I read out results periodically with fixed interval. However from time to time I accidentally can end up calling Read_data function while the instrument is in the process of reading. This throws Timeout error and the app halts. When I restart the app, pyvisa finds the instrument and connects to it. First command is to read the *IDN?, but on this first read I get the following error:

('USB0::1003::8293::ANRITSU_0_0000::0::INSTR',)
Found VNA USB0::1003::8293::ANRITSU_0_0000::0::INSTR
Attached
[2022-09-17 23:14:31,349] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa_py/protoco                                                                                                             ls/usbtmc.py", line 256, in write
    return self.usb_send_ep.write(data)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/usb/core.py", lin                                                                                                             e 408, in write
    return self.device.write(self, data, timeout)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/usb/core.py", lin                                                                                                             e 989, in write
    return fn(
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/usb/backend/libus                                                                                                             b1.py", line 837, in bulk_write
    return self.__write(self.lib.libusb_bulk_transfer,
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/usb/backend/libus                                                                                                             b1.py", line 938, in __write
    _check(retval)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/usb/backend/libus                                                                                                             b1.py", line 602, in _check
    raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])


    usb.core.USBTimeoutError: [Errno 110] Operation timed out
    
During handling of the above exception, another exception occurred:


Traceback (most recent call last):
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/flask/app.py", li                                                                                                             ne 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/flask/app.py", li                                                                                                             ne 1508, in full_dispatch_request
    self.try_trigger_before_first_request_functions()
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/flask/app.py", li                                                                                                             ne 1560, in try_trigger_before_first_request_functions
    self.ensure_sync(func)()
  File "/home/xilinx/Anritsu_GPIB/VNA/routes.py", line 44, in intialize_VNA
    print(anritsu.VNA.query('*IDN?'))
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa/resources/                                                                                                             messagebased.py", line 642, in query
    self.write(message)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa/resources/                                                                                                             messagebased.py", line 197, in write
    count = self.write_raw(message.encode(enco))
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa/resources/                                                                                                             messagebased.py", line 157, in write_raw
    return self.visalib.write(self.session, message)[0]
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa_py/highlev                                                                                                             el.py", line 543, in write
    written, status_code = self.sessions[session].write(data)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa_py/usb.py"                                                                                                             , line 179, in write
    count = self.interface.write(data)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa_py/protoco                                                                                                             ls/usbtmc.py", line 436, in write
    bytes_sent += raw_write(data)
  File "/usr/local/share/pynq-venv/lib/python3.8/site-packages/pyvisa_py/protoco                                                                                                             ls/usbtmc.py", line 258, in write
    raise ValueError(str(e))
ValueError: [Errno 110] Operation timed out

This persists until I do a complete reboot. I cannot find a way to clear this error. Does anyone have an advice on how to go about debugging this issue.

Thanks.

1

There are 1 best solutions below

4
grey_ranger On

When you encounter Operation timed out it is usually one of the following two cases:

  1. The instrument is busy. Perhaps your instrument is still working on something else when you restart the app? I think this one is probably not the case.
  2. The instrument is in a bad state, because it received a bad command earlier. This is the most likely problem, since a reboot solves it.

I'm not familiar with the Anritsu equipment, but I would suggest one of the following ways to avoid querying before the instrument is ready:

First, many machines support the GPIB *OPC? query, which means "Operation Complete". After you trigger the job/measurement to start, you can loop over the *OPC? query until you get a good response. Then you know the machine is ready to report, and you can read_data. This sometimes requires you to catch the GPIB timeout and try again (depending on the machine response).

Second, most machines put updates in their status bytes, which can be read without interrupting the instrument. Use the GPIB *STB? query to get the current status byte of the instrument. You'll have to read the manual for your particular instrument to understand what each bit means. Often, there is a bit for errors and another to show that the test is in-progress or complete.

Third, if you can't find any way to know in advance if the instrument is ready, you can query the instrument and hope (like you are doing now). If the machine becomes non-responsive because of an error state, you can reset it using the GPIB *RST command. It should have the same effect as a reboot, but you can program it directly into your code.