pyvisa: Exception pyvisa.errors.VisaIOError: VisaIOError(u'VI_ERROR_INV_OBJECT (-1073807346))

Today I updated to the latest Pyvisa 1.5.dev3, however, I got this exception every time the script is finished, by finished, I mean the following exception occurs only once no matter how many commands were sent to the instrument, which means, the exception is not thrown inside the write or read methods, but probably when the script is about to quit. for example, I asked “*IDN?” twice and quit, as a result I got the identities of the instrument twice and an exception at the end. Did anyone know the solution?

Rohde&Schwarz,FSV-3,1307.9002K03/101699,2.10
Rohde&Schwarz,FSV-3,1307.9002K03/101699,2.10
Exception pyvisa.errors.VisaIOError: VisaIOError(u'VI_ERROR_INV_OBJECT (-1073807346): The given session or object reference is invalid.',) in  ignored
Hit any key to close this window...

Here’s the debug info:

Machine Details:
Platform ID:    Windows-7-6.1.7601-SP1
Processor:      Intel64 Family 6 Model 37 Stepping 5, GenuineIntel

Python:
Implementation: CPython
Executable:     c:\python27\python.exe
Version:        2.7.6
Compiler:       MSC v.1500 32 bit (Intel)
Bits:           32bit
Build:          Nov 10 2013 19:24:18 (#default)
Unicode:        UCS2

VISA:
#1: C:\Windows\system32\visa32.dll
found by: auto
32bit: True
64bit: False

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 33 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Since I’m not the only one experiencing this I’m posting a bare bone version of my solution to the issue:

import pyvisa

def log(*args, **kwargs):
    pass#print(*args, **kwargs)

class Instrument:
    def __init__(self, adress = 'GPIB0::2::INSTR'):
        self.rm, self.inst = self.get_inst(adress)

    def get_inst(self, adress):
        rm = pyvisa.ResourceManager()
        res = rm.list_resources()
        log(res)
        inst = rm.open_resource(adress)
        return rm, inst

    def __del__(self): 
        self.inst.close()
        self.rm.close()

if __name__ == '__main__':
    inst = Instrument()
    #do stuff
    del inst

The trouble I have with atexit is that it delays the cleanup of the ResourceManager to the interpreter shutdown if used naively which is fine for a single script but more of a concern for long running applications, which is why using it for resources is not an option. Honestly not closing your resource before the end of a script is just bad practice (resources can be used as context managers to avoid that kind of problem) and in larger program it is also easy to avoid. All that said we could try to do something with an atexit handler using only a weakref so that it does not keep the ResourceManager alive for no reason. I am not sure that would fix the issue because we may still have some weird interaction between atexit and the gc but it may be worth trying.