question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:33 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
jugge83commented, Feb 28, 2020

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
1reaction
MatthieuDartiailhcommented, Feb 26, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PyVisa.errors.VisaIOError exception handling in Python
If all you import is visa , you can catch these exceptions with except visa.VisaIOError . Example: import visa try: # Things that...
Read more >
Given Session or Object Reference Invalid in NI VISA - Support
When I attempt to open a new VISA session to my instrument or use an existing session, I receive Error – 1073807346 VISA:...
Read more >
Python - Error VI_ERROR_INV_OBJECT while trying examples
pyvisa.errors.VisaIOError: VI_ERROR_INV_OBJECT (-1073807346): The given session or object reference is invalid.
Read more >
How to use the pyvisa.errors.VisaIOError function in ... - Snyk
VisaIOError function in PyVISA. To help you get started, we've selected a few PyVISA examples, based on popular ways it is used in...
Read more >
PyVISA error, version support - Google Groups
pyvisa.errors.VisaIOError: VI_ERROR_INV_OBJECT (-1073807346): The given session or object reference is invalid. Am i doing something wrong or is it ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found