pyvisa / pyvisa-py

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

pyvisa fails after about 200 instrument open/close cycles #258

Open msteunebrink opened 8 years ago

msteunebrink commented 8 years ago

Hi, I am using the pyvisa-py together with pyvisa to control and read out two BK-precision power supplies. But if I apply the simple script below, it fails. Note, I reduced this simple script from a (much) larger script to reproduce the problem.

import visa

PSU1="USB0::65535::37168::602361010707110005::0::INSTR"
PSU2="USB0::65535::37168::602361010716920013::0::INSTR"

visa.log_to_screen()    
rm= visa.ResourceManager('@py') 

for i in range(220):
#   rm= visa.ResourceManager('@py') 
#   inst1 = rm.open_resource(PSU1)
#   inst2 = rm.open_resource(PSU2)

    inst1 = rm.get_instrument(PSU1)
    inst2 = rm.get_instrument(PSU2)

    inst1.close()
    inst2.close()

#   del inst1
#   del inst2

#   rm.close()
#   del rm

    print i

This always fails in the 102nd cycle if both instruments are opened/closed, and somewhere in the 204th if only one is open/closed.

output for the first two cycles:

2016-07-26 16:10:07,197 - pyvisa - DEBUG - SerialSession was correctly imported.
2016-07-26 16:10:07,205 - pyvisa - DEBUG - USBSession and USBRawSession were correctly imported.
2016-07-26 16:10:07,210 - pyvisa - DEBUG - TCPIPSession was not imported cannot import name TCPIPSession.
2016-07-26 16:10:07,211 - pyvisa - DEBUG - GPIBSession was not imported No module named gpib.
2016-07-26 16:10:07,211 - pyvisa - DEBUG - Created library wrapper for unset
2016-07-26 16:10:07,211 - pyvisa - DEBUG - Created ResourceManager with session 2118087
2016-07-26 16:10:07,211 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - opening ...
2016-07-26 16:10:07,411 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - is open with session 2261721
2016-07-26 16:10:07,411 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - opening ...
2016-07-26 16:10:07,611 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - is open with session 2751984
2016-07-26 16:10:07,611 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - closing
2016-07-26 16:10:07,611 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - is closed
2016-07-26 16:10:07,611 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - closing
2016-07-26 16:10:07,611 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - is closed
0
2016-07-26 16:10:07,611 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - opening ...
2016-07-26 16:10:07,811 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - is open with session 2797979
2016-07-26 16:10:07,811 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - closing
2016-07-26 16:10:07,811 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - opening ...
2016-07-26 16:10:08,011 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - is open with session 2240824
2016-07-26 16:10:08,011 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - closing
2016-07-26 16:10:08,011 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - closing
2016-07-26 16:10:08,011 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - is closed
2016-07-26 16:10:08,011 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - closing
2016-07-26 16:10:08,012 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - is closed
1

Output for the last cycle before fail for both instruments:

100
2016-07-26 16:10:47,100 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - opening ...
2016-07-26 16:10:47,559 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - is open with session 8613882
2016-07-26 16:10:47,559 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - closing
2016-07-26 16:10:47,559 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - opening ...
Traceback (most recent call last):
  File "simplevisatest.py", line 16, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pyvisa/highlevel.py", line 1644, in open_resource
  File "/usr/local/lib/python2.7/dist-packages/pyvisa/resources/resource.py", line 203, in open
  File "/usr/local/lib/python2.7/dist-packages/pyvisa/highlevel.py", line 1601, in open_bare_resource
  File "/usr/local/lib/python2.7/dist-packages/pyvisa-py/highlevel.py", line 191, in open
  File "/usr/local/lib/python2.7/dist-packages/pyvisa-py/sessions.py", line 184, in __init__
  File "/usr/local/lib/python2.7/dist-packages/pyvisa-py/usb.py", line 176, in after_parsing
  File "/usr/local/lib/python2.7/dist-packages/pyvisa-py/protocols/usbtmc.py", line 232, in __init__
  File "/usr/local/lib/python2.7/dist-packages/pyvisa-py/protocols/usbtmc.py", line 146, in __init__
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 1249, in device_iter
  File "/usr/local/lib/python2.7/dist-packages/pyvisa-py/protocols/usbutil.py", line 194, in cm
  File "/usr/local/lib/python2.7/dist-packages/usb/core.py", line 830, in serial_number
  File "/usr/local/lib/python2.7/dist-packages/usb/util.py", line 314, in get_string
ValueError: The device has no langid
2016-07-26 16:10:47,573 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - closing
2016-07-26 16:10:47,573 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - closing
2016-07-26 16:10:47,574 - pyvisa - DEBUG - USB0::65535::37168::602361010707110005::0::INSTR - is closed
2016-07-26 16:10:47,574 - pyvisa - DEBUG - USB0::65535::37168::602361010716920013::0::INSTR - closing
Segmentation fault (core dumped)

As you can see in the code, I've been experimenting with deleting both the RersourceManager as the session handlers. This does not make a difference. Notice how in the second cycle the first instrument gets closed before opening the second instrument.

I am wondering if there is something I can do to make a structure like this possible for infinite loops?

Output of the info command:

python -m visa info
Machine Details:
   Platform ID:    Linux-3.13.0-91-generic-x86_64-with-Ubuntu-14.04-trusty
   Processor:      x86_64

Python:
   Implementation: CPython
   Executable:     /usr/bin/python
   Version:        2.7.6
   Compiler:       GCC 4.8.2
   Bits:           64bit
   Build:          Jun 22 2015 17:58:13 (#default)
   Unicode:        UCS4

PyVISA Version: 1.8

Backends:
   ni:
      Version: 1.8 (bundled with PyVISA)
      Binary library: Not found
   py:
      Version: 0.2
      ASRL INSTR: Available via PySerial (2.6)
      TCPIP INSTR: Available 
      USB RAW: Available via PyUSB (1.0.0). Backend: libusb1
      USB INSTR: Available via PyUSB (1.0.0). Backend: libusb1
      GPIB INSTR:
         Please install linux-gpib to use this resource type.
         No module named gpib
      TCPIP SOCKET: Available 
hgrecco commented 8 years ago

This langid issue comes up every now and them. I think is not properly handled by pyusb but I have to say it is quite surprising how deterministic it is.