Skip to content

Adding a new instrument class

Dardo edited this page Apr 2, 2025 · 5 revisions

Adding and Registering a New Instrument

To add support for a new instrument, follow these steps:

1. Create the Instrument File

Inside the user_defined/instruments/ directory, create a new Python file named after the instrument, e.g., TestInstrument.py.

2. Define the Instrument Class

Inside TestInstrument.py, define a class with the same name as the file, inheriting from Instrument or another appropriate base class:

from easy_scpi import Instrument
from instruments import SCPI_Info
from config import Config

class TestInstrument(Instrument):
    def __init__(self, scpi_info: SCPI_Info):
        super().__init__(
            port=scpi_info.port,
            timeout=5000,
            baud_rate=scpi_info.baud_rate,
        )
        self.init_properties()

    def init_properties(self) -> None:
        self.properties_list: List[property_info] = [
            property_info(
                "Voltage",
                float,
                lambda: self.voltp,
                lambda val: setattr(self, "voltp", val),
            ),
        ]

    @property
    def voltp(self):
        """
        Returns the voltage reading.
        """
        return self.voltage()

    @voltp.setter
    def voltp(self, volts):
        """
        Sets the voltage of the instrument.
        """
        self.voltage(volts)


# Register the instrument in Config
Config().add_instrument_extension(("TestInstrumentMatch", TestInstrument))

When registering the class, make sure that the string "TestInstrumentMatch" matches a subset of the IDN string. For example, if you are working with a Keithley 2000, you may want to set the key to "Model 2000" as the IDN string contains it. Don't put serial numbers here as you may have multiple instruments of the same kind.

3. Import the Instrument in __init__.py

Modify user_defined/instruments/__init__.py to include:

from .TestInstrument import TestInstrument

4. Automatic Recognition of the Instrument

The system automatically detects and instantiates the correct instrument class based on the received IDN. This is handled by the custom_instr_handler() function, which matches the IDN to registered instrument extensions key specified before.

Clone this wiki locally