Skip to content
Advertisement

Issue with Constructor creation/File Referencing (AttributeError: ‘Sr830’ object has no attribute ‘resource_manager’)

I am relatively new to Python, so please bear with my lack of technical knowledge.

I have a lock-in amplifier setup with a GPIB to USB cable that is plugged into my computer. I am able to read the instrument fine with Pyvisa. I am additionally using other driver classes (labdrivers) to write and read values to and from the instruments.

Here is the __init__ method for the Python class I am referencing. There is of course more to it in terms of other methods that specifically run the commands using the instrument read from the resource manager, but I believe the error lies in this method when specifically reading the instrument. I have added three stars (***) to the beginning and end of the resourcemanager lines that are likely causing the issue for easier reference.

import pyvisa

# create a logger object for this module
logger = logging.getLogger(__name__)
# added so that log messages show up in Jupyter notebooks
logger.addHandler(logging.StreamHandler())


class Sr830:
    """Interface to a Stanford Research Systems 830 lock in amplifier."""

    def __init__(self, gpib_addr):
        """Create an instance of the Sr830 object.

        :param gpib_addr: GPIB address of the SR830
        """
        try:
            # the pyvisa manager we'll use to connect to the GPIB resources
            ***self.resource_manager = pyvisa.ResourceManager()***
        except OSError:
            logger.exception("ntCould not find the VISA library. Is the VISA driver installed?nn")
        
        self._gpib_addr = gpib_addr
        self._instrument = None
        ***self._instrument = self.resource_manager.open_resource("GPIB::%d" % self._gpib_addr)***

The code I am running on my test file is this.

from labdrivers.srs import sr830

sr_gpib_address = 8

lock_in = sr830.Sr830(sr_gpib_address)

The reason why I create the object using sr830.Sr830 is because this is the file path. anaconda3libsite-packageslabdriverssrssr830.py.

The error output from the terminal is this.

Traceback (most recent call last):

  File ~test.py:14 in <module>
    lock_in = sr830.Sr830(sr_gpib_address)

  File ~anaconda3libsite-packageslabdriverssrssr830.py:28 in __init__
    self._instrument = self.resource_manager.open_resource("GPIB::%d" % self._gpib_addr)

AttributeError: 'Sr830' object has no attribute 'resource_manager'

My files are in different directories. Could this be a possible issue? I am not sure if I am just creating the object wrong (I may be referencing the files incorrectly) or if there is some issue with pyvisa, although the pyvisa commands seem correct. I am running Python 3.9 on Spyder.

Please let me know any more information that I should provide. Thank you for all and any help.

Advertisement

Answer

I was able to resolve my issue. I believe the main problem was using a driver class written from someone else, which may have included other information or incompatible reference programs. By simply calling creating the Pyvisa resource manager object by myself in a separate file and passing information to the machine with the methods specified in the manual, the error no longer appeared. It is most likely necessary to make the methods simpler as well, as the driver methods included a great deal of other information which may have needed specific installations or versions of the supposed installations. It was not an issue of the files being in different directories. Additionally, I would advise making the class and file names as different as possible, as that led to some conflicts when I was creating objects. The file name was Sr830 which was similar to the class name of Sr830. This other error persisted despite my attempts to work around it, such as:

from labdrivers import Sr830

from Sr830 import Sr830

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement