Exemplo n.º 1
0
    def __init__(self,
                 name: str,
                 metadata: Optional[Dict[Any, Any]] = None) -> None:
        self._name = str(name)
        self._short_name = str(name)

        self.parameters: Dict[str, _BaseParameter] = {}
        """
        All the parameters supported by this instrument.
        Usually populated via :py:meth:`add_parameter`.
        """
        self.functions: Dict[str, Function] = {}
        """
        All the functions supported by this
        instrument. Usually populated via :py:meth:`add_function`.
        """
        self.submodules: Dict[str, Union['InstrumentBase', 'ChannelList']] = {}
        """
        All the submodules of this instrument
        such as channel lists or logical groupings of parameters.
        Usually populated via :py:meth:`add_submodule`.
        """

        super().__init__(metadata)

        # This is needed for snapshot method to work
        self._meta_attrs = ['name']

        self.log = get_instrument_logger(self, __name__)
Exemplo n.º 2
0
    def __init__(self, *args, **kwargs) -> None:
        # ignore this line in mypy: Mypy does not support mixins yet
        # and seen by itself with this class definition it does not make sense
        # to call __init__ on the super()
        super().__init__(*args, **kwargs)  # type: ignore
        self.visa_log = get_instrument_logger(self, VISA_LOGGER)

        # This base class mixin holds two dictionaries associated with the
        # pyvisa_instrument.write()
        self.cmds: Dict[str, Callable] = {}
        # and pyvisa_instrument.query() functions
        self.queries: Dict[str, Callable] = {}
        # the keys are the issued VISA commands like '*IDN?' or '*OPC'
        # the values are the corresponding methods to be called on the mock
        # instrument.

        # To facilitate the definition there are the decorators `@query' and
        # `@command`. These attach an attribute to the method, so that the
        # dictionaries can be filled here in the constructor. (This is
        # borderline abusive, but makes a it easy to define mocks)
        func_names = dir(self)
        # cycle through all methods
        for func_name in func_names:
            f = getattr(self, func_name)
            # only add for methods that have such an attribute
            with suppress(AttributeError):
                self.queries[getattr(f, 'query_name')] = f
            with suppress(AttributeError):
                self.cmds[getattr(f, 'command_name')] = f
Exemplo n.º 3
0
    def __init__(self, name: str, metadata: Optional[dict] = None) -> None:
        self.name = str(name)
        self.short_name = str(name)

        self.parameters: Dict[str, _BaseParameter] = {}
        self.functions: Dict[str, Function] = {}
        self.submodules: Dict[str, Union['InstrumentBase', 'ChannelList']] = {}
        super().__init__(metadata)

        # This is needed for snapshot method to work
        self._meta_attrs = ['name']

        self.log = get_instrument_logger(self, __name__)
Exemplo n.º 4
0
    def __init__(self,
                 name: str,
                 address: str,
                 timeout: Union[int, float] = 5,
                 terminator: str = '',
                 device_clear: bool = True,
                 visalib: Optional[str] = None,
                 **kwargs: Any):

        super().__init__(name, **kwargs)
        self.visa_log = get_instrument_logger(self, VISA_LOGGER)
        self.visabackend: str
        self.visa_handle: visa.ResourceManager
        self.visalib: Optional[str]

        self.add_parameter('timeout',
                           get_cmd=self._get_visa_timeout,
                           set_cmd=self._set_visa_timeout,
                           unit='s',
                           vals=vals.MultiType(vals.Numbers(min_value=0),
                                               vals.Enum(None)))

        # backwards-compatibility
        if address and '@' in address:
            address, visa_library = address.split('@')
            if visalib:
                warnings.warn('You have specified the VISA library in two '
                              'different ways. Please do not include "@" in '
                              'the address kwarg and only use the visalib '
                              'kwarg for that.')
                self.visalib = visalib
            else:
                warnings.warn('You have specified the VISA library using '
                              'an "@" in the address kwarg. Please use the '
                              'visalib kwarg instead.')
                self.visalib = '@' + visa_library
        else:
            self.visalib = visalib

        try:
            self.set_address(address)
        except Exception as e:
            self.visa_log.info(f"Could not connect at {address}")
            self.close()
            raise e

        if device_clear:
            self.device_clear()

        self.set_terminator(terminator)
        self.timeout.set(timeout)
Exemplo n.º 5
0
    def __init__(self,
                 name: str,
                 metadata: Optional[Mapping[Any, Any]] = None) -> None:
        name = self._replace_hyphen(name)
        self._short_name = name
        self._is_valid_identifier(self.full_name)

        self.parameters: Dict[str, ParameterBase] = {}
        """
        All the parameters supported by this instrument.
        Usually populated via :py:meth:`add_parameter`.
        """
        self.functions: Dict[str, Function] = {}
        """
        All the functions supported by this
        instrument. Usually populated via :py:meth:`add_function`.
        """
        self.submodules: Dict[str, Union["InstrumentModule",
                                         "ChannelTuple"]] = {}
        """
        All the submodules of this instrument
        such as channel lists or logical groupings of parameters.
        Usually populated via :py:meth:`add_submodule`.
        """
        self.instrument_modules: Dict[str, "InstrumentModule"] = {}
        """
        All the instrument_modules of this instrument
        Usually populated via :py:meth:`add_submodule`.
        """

        self._channel_lists: Dict[str, "ChannelTuple"] = {}
        """
        All the ChannelTuples of this instrument
        Usually populated via :py:meth:`add_submodule`.
        This is private until the correct name has been decided.
        """

        super().__init__(metadata)

        # This is needed for snapshot method to work
        self._meta_attrs = ['name']

        self.log = get_instrument_logger(self, __name__)
Exemplo n.º 6
0
    def __init__(self,
                 name,
                 address,
                 port,
                 visalib,
                 metadata=None,
                 device_clear=False,
                 terminator='\n',
                 timeout=3,
                 **kwargs):

        # remove IPInstrument-specific kwargs
        ipkwargs = ['write_confirmation']
        newkwargs = {
            kw: val
            for (kw, val) in kwargs.items() if kw not in ipkwargs
        }

        Instrument.__init__(self, name, metadata=metadata, **newkwargs)
        self.visa_log = get_instrument_logger(self, VISA_LOGGER)

        ##################################################
        # __init__ of VisaInstrument

        self.add_parameter('timeout',
                           get_cmd=self._get_visa_timeout,
                           set_cmd=self._set_visa_timeout,
                           unit='s',
                           vals=vals.MultiType(vals.Numbers(min_value=0),
                                               vals.Enum(None)))

        # auxiliary VISA library to use for mocking
        self.visalib = visalib
        self.visabackend = None

        self.set_address(address)
        if device_clear:
            self.device_clear()

        self.set_terminator(terminator)
        self.timeout.set(timeout)
Exemplo n.º 7
0
    def __init__(
        self,
        name: str,
        address: str,
        timeout: Union[int, float] = 5,
        terminator: Optional[str] = None,
        device_clear: bool = True,
        visalib: Optional[str] = None,
        **kwargs: Any,
    ):

        super().__init__(name, **kwargs)
        self.visa_log = get_instrument_logger(self, VISA_LOGGER)
        self.visabackend: str
        self.visa_handle: visa.resources.MessageBasedResource
        self.visalib: Optional[str] = visalib

        self.add_parameter('timeout',
                           get_cmd=self._get_visa_timeout,
                           set_cmd=self._set_visa_timeout,
                           unit='s',
                           vals=vals.MultiType(vals.Numbers(min_value=0),
                                               vals.Enum(None)))

        try:
            self.set_address(address)
        except Exception as e:
            self.visa_log.exception(f"Could not connect at {address}")
            self.close()
            raise e

        if device_clear:
            self.device_clear()

        self.set_terminator(terminator)
        self.timeout.set(timeout)