Skip to content

PyVISA¤

PyVISA (Interface) ¤

PyVISA(equipment: Equipment)

Use PyVISA as the backend to communicate with the equipment.

The backend value must be equal to PyVISA to use this class for the communication backend.

The PYVISA_LIBRARY environment variable is used (if it exists) to create the ResourceManager. This environment variable can be defined in a configuration file or by defining the environment variable in your code before connecting to the equipment using PyVISA for the first time. The default value is @ivi if this environment variable is not defined.

The returned object using msl-equipment to connect to the equipment is equivalent to calling open_resource, e.g.,

from msl.equipment import Backend, Connection

connection = Connection("GPIB::12", backend=Backend.PyVISA)
inst = connection.connect()
print(inst.query("*IDN?"))

is equivalent to

import pyvisa

rm = pyvisa.ResourceManager()
inst = rm.open_resource("GPIB::12")
print(inst.query("*IDN?"))

You can also combine the packages, use msl-equipment for managing information about the equipment and directly use pyvisa for the connection. If you use this combination, the editor you use to develop your code may have better support for features like code completion and type checking.

import pyvisa
from msl.equipment import Config

# config.xml contains <equipment eid="MSLE.0.063" name="dmm"/>
# and specifies where the equipment registers are and the connections file.
cfg = Config("config.xml")
equipment = cfg.equipment["dmm"]

rm = pyvisa.ResourceManager()
inst = rm.open_resource(equipment.connection.address)
data = inst.query("READ?")

# You could now use the `equipment` instance to apply a correction to the `data`

Parameters:

Name Type Description Default
equipment Equipment

An Equipment instance.

required
Source code in src/msl/equipment/interfaces/pyvisa.py
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def __init__(self, equipment: Equipment) -> None:
    """Use [PyVISA] as the backend to communicate with the equipment.

    The [backend][msl.equipment.schema.Connection.backend] value must be equal to
    `PyVISA` to use this class for the communication backend.

    The `PYVISA_LIBRARY` environment variable is used (if it exists) to create the
    [ResourceManager][pyvisa.highlevel.ResourceManager]. This environment variable
    can be defined in a [configuration file][config-xml-example] or by defining the
    environment variable in your code before connecting to the equipment using
    [PyVISA][msl.equipment.interfaces.pyvisa.PyVISA] for the first time. The default
    value is `@ivi` if this environment variable is not defined.

    The returned object using `msl-equipment` to connect to the equipment is equivalent
    to calling [open_resource][pyvisa.highlevel.ResourceManager.open_resource], e.g.,

    ```python
    from msl.equipment import Backend, Connection

    connection = Connection("GPIB::12", backend=Backend.PyVISA)
    inst = connection.connect()
    print(inst.query("*IDN?"))
    ```

    is equivalent to

    ```python
    import pyvisa

    rm = pyvisa.ResourceManager()
    inst = rm.open_resource("GPIB::12")
    print(inst.query("*IDN?"))
    ```

    You can also combine the packages, use `msl-equipment` for managing information
    about the equipment and directly use `pyvisa` for the connection. If you use this
    combination, the editor you use to develop your code may have better support for
    features like code completion and type checking.

    ```python
    import pyvisa
    from msl.equipment import Config

    # config.xml contains <equipment eid="MSLE.0.063" name="dmm"/>
    # and specifies where the equipment registers are and the connections file.
    cfg = Config("config.xml")
    equipment = cfg.equipment["dmm"]

    rm = pyvisa.ResourceManager()
    inst = rm.open_resource(equipment.connection.address)
    data = inst.query("READ?")

    # You could now use the `equipment` instance to apply a correction to the `data`
    ```

    [PyVISA]: https://pyvisa.readthedocs.io/en/stable/

    Args:
        equipment: An [Equipment][] instance.
    """
    self._resource: Resource | None = None
    super().__init__(equipment)

    if pyvisa is None:  # pragma: no branch
        msg = "pyvisa is not installed, run: pip install pyvisa"  # type: ignore[unreachable]
        raise RuntimeError(msg)

    assert equipment.connection is not None  # noqa: S101
    kwargs = _prepare_kwargs(equipment.connection.properties)

    if PyVISA.rm is None:
        PyVISA.rm = pyvisa.ResourceManager()

    self._resource = PyVISA.rm.open_resource(equipment.connection.address, **kwargs)

equipment property ¤

equipment: Equipment

The Equipment associated with the interface.

rm class-attribute instance-attribute ¤

rm: ResourceManager | None = None

PyVISA Resource Manager.

disconnect ¤

disconnect() -> None

Calls pyvisa.resources.Resource.close.

Source code in src/msl/equipment/interfaces/pyvisa.py
126
127
128
129
130
131
132
def disconnect(self) -> None:  # pyright: ignore[reportImplicitOverride]
    """Calls [pyvisa.resources.Resource.close][]."""
    if self._resource is not None:
        self._resource.close()
        logger.debug("Disconnected from %s", self)
        self._resource = None
    super().disconnect()