pyvisa / pyvisa-py

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

[COM] Communication issue with HMC 8012 using Raspberry Pi 4 Model B #336

Closed XaniXxable closed 2 years ago

XaniXxable commented 2 years ago

Hi, I do have a problem with my connection to the instrument. I can establish a connection, but after closing it and reopen it again it will fail with pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.. Running the program again will work again, but after that fail again....

I already tried with different version, the version below is the installed version, I also tried it with the latest one. Got the same effect there as well.

Instrument details

Output of pyvisa-info old version

pi@raspberrypi:~ $ pyvisa-info
Machine Details:
   Platform ID:    Linux-4.19.66-v7l+-armv7l-with-debian-10.1
   Processor:

Python:
   Implementation: CPython
   Executable:     /usr/bin/python3
   Version:        3.7.3
   Compiler:       GCC 8.2.0
   Bits:           32bit
   Build:          Apr  3 2019 05:39:12 (#default)
   Unicode:        UCS4

PyVISA Version: 1.10.0

Backends:
   ni:
      Version: 1.10.0 (bundled with PyVISA)
      Binary library: Not found
   py:
      Version: 0.3.1
      ASRL INSTR: Available via PySerial (3.4)
      USB INSTR: Available via PyUSB (1.0.2). Backend: libusb1
      USB RAW: Available via PyUSB (1.0.2). Backend: libusb1
      TCPIP INSTR: Available
      TCPIP SOCKET: Available
      GPIB INSTR:
         Please install linux-gpib to use this resource type.
         No module named 'gpib'

This I installed in a venv, to not mess with my dependencies. Output of pyvisa-info latest

pi@raspberrypi:~ $ pyvisa-info
Machine Details:
   Platform ID:    Linux-4.19.66-v7l+-armv7l-with-debian-10.1
   Processor:

Python:
   Implementation: CPython
   Executable:     /home/pi/Documents/new_testTool/test_scripts/venv/bin/python3
   Version:        3.7.3
   Compiler:       GCC 8.2.0
   Bits:           32bit
   Build:          Apr  3 2019 05:39:12 (#default)
   Unicode:        UCS4

PyVISA Version: 1.12.0

Backends:
   ivi:
      Version: 1.12.0 (bundled with PyVISA)
      Binary library: Not found
   py:
      Version: 0.5.3
      ASRL INSTR:
         Please install PySerial (>=3.0) to use this resource type.
         No module named 'serial'
      USB INSTR: Available via PyUSB (1.2.1). Backend: libusb1
      USB RAW: Available via PyUSB (1.2.1). Backend: libusb1
      TCPIP INSTR: Available
      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'

Here is my code I used to recreate the problem:

import pyvisa
import time

pyvisa.log_to_screen()
rm = pyvisa.ResourceManager('@py') #Use the pyvisa-py backend
myInstrument = rm.open_resource('USB0::2733::309::037282696::0::INSTR')
myInstrument.timeout = 5000
myInstrument.read_termination = '\n'
myInstrument.write_termination = '\n'

# Enable remote commands
print('\nEnabling remote')
myInstrument.write("SYSTem:REMote")
# myInstrument.write("MEAS:VOLT:DC? AUTO")
time.sleep(1)

for i in range(1):
    fullReadingLine = myInstrument.query("READ?")
    ready = myInstrument.query("*OPC?")
    print(f"Value: {fullReadingLine} ")
    print(f"Ready: {ready} ")
    time.sleep(0.5)

print('Diabling remote\n')
time.sleep(1)
myInstrument.write("SYSTem:LOCal")
myInstrument.close()
rm.close()

And here is the full traceback:

pi@raspberrypi:~ $ python3 ./test_scripts/pyvisa-test.py
2022-09-01 00:47:20,378 - pyvisa - DEBUG - SerialSession was correctly imported.
2022-09-01 00:47:20,417 - pyvisa - DEBUG - USBSession and USBRawSession were correctly imported.
2022-09-01 00:47:20,426 - pyvisa - DEBUG - TCPIPSession was correctly imported.
2022-09-01 00:47:20,428 - pyvisa - DEBUG - GPIBSession was not imported No module named 'gpib'.
2022-09-01 00:47:20,428 - pyvisa - DEBUG - Created library wrapper for unset
2022-09-01 00:47:20,429 - pyvisa - DEBUG - Created ResourceManager with session 8316124
2022-09-01 00:47:20,429 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - opening ...
2022-09-01 00:47:20,695 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - is open with session 9381755

Enabling remote
2022-09-01 00:47:21,698 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
2022-09-01 00:47:21,711 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
Value: 9.18463000E-03
Ready: 1
Diabling remote

2022-09-01 00:47:23,223 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - closing
2022-09-01 00:47:23,225 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - is closed
2022-09-01 00:47:23,225 - pyvisa - DEBUG - Closing ResourceManager (session: 8316124)
2022-09-01 00:47:23,226 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - closing
pi@raspberrypi:~l $ python3 ./test_scripts/pyvisa-test.py
2022-09-01 00:47:25,620 - pyvisa - DEBUG - SerialSession was correctly imported.
2022-09-01 00:47:25,659 - pyvisa - DEBUG - USBSession and USBRawSession were correctly imported.
2022-09-01 00:47:25,668 - pyvisa - DEBUG - TCPIPSession was correctly imported.
2022-09-01 00:47:25,669 - pyvisa - DEBUG - GPIBSession was not imported No module named 'gpib'.
2022-09-01 00:47:25,670 - pyvisa - DEBUG - Created library wrapper for unset
2022-09-01 00:47:25,670 - pyvisa - DEBUG - Created ResourceManager with session 8698880
2022-09-01 00:47:25,671 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - opening ...
2022-09-01 00:47:25,945 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - is open with session 7744921

Enabling remote
2022-09-01 00:47:26,948 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
2022-09-01 00:47:31,960 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - exception while reading: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
Buffer content: bytearray(b'')
Traceback (most recent call last):
  File "./test_scripts/pyvisa-test.py", line 18, in <module>
    fullReadingLine = myInstrument.query("READ?")
  File "/home/pi/.local/lib/python3.7/site-packages/pyvisa/resources/messagebased.py", line 600, in query
    return self.read()
  File "/home/pi/.local/lib/python3.7/site-packages/pyvisa/resources/messagebased.py", line 414, in read
    message = self._read_raw().decode(enco)
  File "/home/pi/.local/lib/python3.7/site-packages/pyvisa/resources/messagebased.py", line 387, in _read_raw
    chunk, status = self.visalib.read(self.session, size)
  File "/home/pi/.local/lib/python3.7/site-packages/pyvisa-py/highlevel.py", line 350, in read
    raise errors.VisaIOError(ret[1])
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
2022-09-01 00:47:32,002 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - closing
2022-09-01 00:47:32,003 - pyvisa - DEBUG - USB0::2733::309::037282696::0::INSTR - is closed
2022-09-01 00:47:32,035 - pyvisa - DEBUG - Closing ResourceManager (session: 8698880)
MatthieuDartiailh commented 2 years ago

Just to be sure, you can run your script once just fine, but running it second time fail with a timeout ?

XaniXxable commented 2 years ago

Yeah. I start the scirpt it runs. I start it againg it fails. Every second run it runs into that timeout.

MatthieuDartiailh commented 2 years ago

I am trying to fix some issues with USB resource related to how we initialize the communication in https://github.com/pyvisa/pyvisa-py/issues/336

In your case maybe there are also some issues with closing the connection but could you try running with that branch to see if it helps.

XaniXxable commented 2 years ago

Yeah I can check that, this can take a while since I have to install it on the pi offline.

MatthieuDartiailh commented 2 years ago

Thanks (the PR touches a single file so you can probably just copy this one over: usbtmc.py)

XaniXxable commented 2 years ago

Sorry for the late response. Your fix helped so far. I tested it know multiple times and it worked fine! :)

MatthieuDartiailh commented 2 years ago

Great thanks for testing !!!