pyvisa / pyvisa-py

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

Gpib.__del__ fails on script end #218

Open Snyzar opened 4 years ago

Snyzar commented 4 years ago

I'm rather new to python on Linux, so apologies if I miss anything obvious. I'm also not certain if this is an actual issue or I just need support, but I appreciate any assistance all the same.

My test system is basically a glorified thermometer at the moment, with the following script to get a single measurement:

import pyvisa

inst = pyvisa.ResourceManager('@py').open_resource('GPIB0::8::INSTR')

inst.write('MEAS:VOLT:DC? (@103)')
rawMeas = inst.read()

fmtMeas = '{:.2f}C'.format(float(rawMeas) * 1000)
print(fmtMeas)

This will print the value I'm looking for but won't terminate cleanly:

 pi@raspberrypi:~/Documents/py_scratches $ /usr/bin/python3
/home/pi/Documents/py_scratches/Temp_Tester.py
21.61C
libgpib: invalid descriptor
libgpib: invalid descriptor
Exception ignored in: <function Gpib.__del__ at 0x7475eb70>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/Gpib.py", line 32, in __del__
gpib.GpibError: close() failed: One or more arguments to the function call were invalid.

This error doesn't occur using python 2.7.16. It also does not occur when using Gpib directly with inst = Gpib.Gpib(0,8) on 3.7.3, which is why I figured it has to do with pyvisa-py.

My module info:

pi@raspberrypi:~/Documents/py_scratches $ python3 -m visa info
Machine Details:
   Platform ID:    Linux-4.19.75-v7+-armv7l-with-debian-10.2
   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.1

Backends:
   ni:
      Version: 1.10.1 (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: Available via Linux GPIB (< 4.0)

and the output with log_to_screen():

pi@raspberrypi:~/Documents/py_scratches $ /usr/bin/python3 /home/pi/Documents/py_scratches/Temp_Tester.py
2020-01-21 15:07:38,684 - pyvisa - DEBUG - SerialSession was correctly imported.
2020-01-21 15:07:38,749 - pyvisa - DEBUG - USBSession and USBRawSession were correctly imported.
2020-01-21 15:07:38,766 - pyvisa - DEBUG - TCPIPSession was correctly imported.
2020-01-21 15:07:38,771 - pyvisa - DEBUG - GPIBSession was correctly imported.
2020-01-21 15:07:38,772 - pyvisa - DEBUG - Created library wrapper for unset
2020-01-21 15:07:38,773 - pyvisa - DEBUG - Created ResourceManager with session 9700303
2020-01-21 15:07:38,774 - pyvisa - DEBUG - GPIB0::8::INSTR - opening ...
2020-01-21 15:07:38,777 - pyvisa - DEBUG - GPIB0::8::INSTR - is open with session 3920073
2020-01-21 15:07:38,777 - pyvisa - DEBUG - GPIB.write b'MEAS:VOLT:DC? (@103)\r\n'
2020-01-21 15:07:38,782 - pyvisa - DEBUG - GPIB0::8::INSTR - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
21.61C
2020-01-21 15:07:39,104 - pyvisa - DEBUG - GPIB0::8::INSTR - closing
2020-01-21 15:07:39,105 - pyvisa - DEBUG - GPIB0::8::INSTR - is closed
2020-01-21 15:07:39,143 - pyvisa - DEBUG - Closing ResourceManager (session: 9700303)
libgpib: invalid descriptor
libgpib: invalid descriptor
Exception ignored in: <function Gpib.__del__ at 0x7476da98>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/Gpib.py", line 32, in __del__
gpib.GpibError: close() failed: One or more arguments to the function call were invalid.

For what it's worth, I noticed running against 2.7 with log_to_screen() that the final "Closing ResourceManager" message is not printed.

MatthieuDartiailh commented 4 years ago

This happens because pyvisa assumes that close can be safely called on an already closed resource. This is the case in the standard backend however it is not so for the GPIB part of pyvisa-py. The fix should be relatively easy, if you want to give it a try feel free to do so. And thanks for reporting the issue !