tsp_control

Base Test Script Processing (TSP) control class module.

TSPControl

TSPControl(
    config_entry: DeviceConfigEntry,
    verbose: bool,
    visa_resource: MessageBasedResource,
    default_visa_timeout: int,
)

Bases: PIControl, ABC

flowchart LR tm_devices.driver_mixins.device_control.tsp_control.TSPControl[TSPControl] tm_devices.driver_mixins.device_control.pi_control.PIControl[PIControl] tm_devices.driver_mixins.device_control._abstract_device_visa_write_query_control._AbstractDeviceVISAWriteQueryControl[_AbstractDeviceVISAWriteQueryControl] tm_devices.driver_mixins.device_control._abstract_device_control._AbstractDeviceControl[_AbstractDeviceControl] tm_devices.driver_mixins.shared_implementations._extension_mixin._ExtendableMixin[_ExtendableMixin] tm_devices.driver_mixins.device_control.pi_control.PIControl --> tm_devices.driver_mixins.device_control.tsp_control.TSPControl tm_devices.driver_mixins.device_control._abstract_device_visa_write_query_control._AbstractDeviceVISAWriteQueryControl --> tm_devices.driver_mixins.device_control.pi_control.PIControl tm_devices.driver_mixins.device_control._abstract_device_control._AbstractDeviceControl --> tm_devices.driver_mixins.device_control._abstract_device_visa_write_query_control._AbstractDeviceVISAWriteQueryControl tm_devices.driver_mixins.shared_implementations._extension_mixin._ExtendableMixin --> tm_devices.driver_mixins.device_control.pi_control.PIControl click tm_devices.driver_mixins.device_control.tsp_control.TSPControl href "" "tm_devices.driver_mixins.device_control.tsp_control.TSPControl" click tm_devices.driver_mixins.device_control.pi_control.PIControl href "" "tm_devices.driver_mixins.device_control.pi_control.PIControl" click tm_devices.driver_mixins.device_control._abstract_device_visa_write_query_control._AbstractDeviceVISAWriteQueryControl href "" "tm_devices.driver_mixins.device_control._abstract_device_visa_write_query_control._AbstractDeviceVISAWriteQueryControl" click tm_devices.driver_mixins.device_control._abstract_device_control._AbstractDeviceControl href "" "tm_devices.driver_mixins.device_control._abstract_device_control._AbstractDeviceControl" click tm_devices.driver_mixins.shared_implementations._extension_mixin._ExtendableMixin href "" "tm_devices.driver_mixins.shared_implementations._extension_mixin._ExtendableMixin"

Base Test Script Processing (TSP) control class.

Important

Any class that inherits this control mixin must also inherit a descendant of the Device class in order to have access to the attributes required by this class.

Parameters:
  • config_entry (DeviceConfigEntry) –

    A config entry object parsed by the DMConfigParser.

  • verbose (bool) –

    A boolean indicating if verbose output should be printed. If True, communication printouts will be logged with a level of INFO. If False, communication printouts will be logged with a level of DEBUG.

  • visa_resource (MessageBasedResource) –

    The VISA resource object.

  • default_visa_timeout (int) –

    The default VISA timeout value in milliseconds.

default_visa_timeout property

default_visa_timeout: int

Return the default VISA timeout value in milliseconds.

device_type abstractmethod cached property

device_type: str

Return a string representing the device type.

idn_string cached property

idn_string: str

Return the string returned from the *IDN? query when the device was created.

ieee_cmds property

Return an internal class containing methods for the standard IEEE 488.2 command set.

ip_address abstractmethod cached property

ip_address: str

The IP address of the device.

manufacturer cached property

manufacturer: str

Return the manufacturer of the device.

model cached property

model: str

Return the full model of the device.

name_and_alias abstractmethod cached property

name_and_alias: str

A string containing the device name and alias.

resource_expression property

resource_expression: str

Return the VISA resource expression.

serial cached property

serial: str

Return the serial number of the device.

series cached property

series: str

Return the series of the device.

Returns:
  • str

    The series of the device, e.g. MSO5, TSOVu, TEKSCOPE, AFG3K, AWG5200

sw_version cached property

sw_version: Version

Return the software version of the device.

visa_backend cached property

visa_backend: str

Return the VISA backend in use.

visa_resource property

visa_resource: MessageBasedResource

Return the VISA resource object.

This gives access to all the attributes and methods that PyVISA provides.

visa_timeout property writable

visa_timeout: float

Return the current VISA timeout of the device in milliseconds.

add_method classmethod

add_method(method: Callable[Concatenate[Self, _P], _T]) -> None

Add a method to the class.

This class method is best used as a decorator on functions in order to add them to a class.

Examples:

>>> from tm_devices.drivers.device import Device
>>>
>>> @Device.add_method
... def print_hello(self: Device, var: str):
...     print("Hello World!")
...     print(f"I am a {self.__class__.__name__}!")
...     print(f"My var is {var}")
Parameters:

add_property classmethod

add_property(method: Callable[Concatenate[_EM, _P], _T]) -> None
add_property(
    method: None = None, /, *, is_cached: bool = False
) -> Callable[[Callable[Concatenate[_EM, _P], _T]], None]
add_property(
    method: Callable[[Self], _T] | None = None, /, *, is_cached: bool = False
) -> Callable[[Callable[[Self], _T]], None] | None

Add a property to the class.

This class method is best used as a decorator on functions in order to add them to a class.

Examples:

>>> from tm_devices.drivers.device import Device
>>>
>>> @Device.add_property
... def foo(self: Device):
...     return "bar"
>>>
>>> @Device.add_property(is_cached=True)
... def foo(self: Device):
...     return self.name + self.address
Parameters:
  • method (Callable[[Self], _T] | None, default: None ) –

    The property to add to the class.

  • is_cached (bool, default: False ) –

    Whether the property is only evaluated once and cached.

check_visa_connection

check_visa_connection() -> bool

Check if a VISA connection can be made to the device.

Wrapper function for check_visa_connection.

device_clear

device_clear() -> None

Clear the input and output buffers of the VISA device.

disable_srq_events

disable_srq_events() -> None

Disable the service request event for the device.

enable_srq_events

enable_srq_events() -> None

Enable the service request event for the device.

expect_esr

expect_esr(
    esr: int, error_messages: tuple[str, ...] = (), *, use_regex_match: bool = False
) -> bool

Checks for the expected esr value and queued error messages.

Parameters:
  • esr (int) –

    Expected *ESR? value as a decimal-weighted integer.

  • error_messages (tuple[str, ...], default: () ) –

    Expected error buffer messages in a tuple.

  • use_regex_match (bool, default: False ) –

    A boolean indicating if the messages should be compared using regular expressions. Does not affect the esr value comparison.

Returns:
  • bool

    Boolean True when all checks pass. False means the checks failed (however, failing a check will always result in an AssertionError being raised, so the result will not really be usable).

Raises:
  • AssertionError

    Indicating that the device’s error code and messages don’t match the expected values.

export_buffers

export_buffers(filepath: str | PathLike[str], *args: str, sep: str = ',') -> None

Export one or more of the device’s buffers to the given filepath.

Parameters:
  • filepath (str | PathLike[str]) –

    A string representing the path of the file to write to.

  • args (str, default: () ) –

    The buffer name(s) to export.

  • sep (str, default: ',' ) –

    The delimiter used to separate data. Defaults to “,”.

factory_reset

factory_reset() -> None

Send the FACTORY command followed by an *OPC? query.

This command is equivalent to pressing the DEFAULT SETUP button located on the instrument front panel or selected Default Setup from the File menu.

It recalls the instrument to factory default settings.

In addition to what *RST does, this command also performs the following operations:

  • Clears any pending OPC operations
  • Resets the following IEEE488.2 registers:
  • *ESR 0 (Event Status Enable Register)
  • *SRE 0 (Service Request Enable Register)
  • DESE 255 (Device Event Status Enable Register)
  • *PSC 1 (Power-on Status Clear Flag)
  • Deletes all defined aliases.
  • Enables command headers (:HEADer 1).

FACTORY only resets the programmable interface settings, it does not change the user interface settings.

get_buffers

get_buffers(*args: str) -> dict[str, list[float | str]]

Get the contents of one or more buffers on the device.

Parameters:
  • args (str, default: () ) –

    The buffer name(s) to get.

Returns:
  • dict[str, list[float | str]]

    A dictionary containing each of the buffers name, and the contents of the buffer.

get_visa_stb

get_visa_stb() -> int

Return the VISA status byte.

load_script

load_script(
    script_name: str,
    *,
    script_body: str = "",
    file_path: str | PathLike[str] | None = None,
    run_script: bool = False,
    to_nv_memory: bool = False
) -> None

Upload a TSP script to the instrument.

Parameters:
  • script_name (str) –

    A string indicating what to name the script being loaded on the instrument.

  • script_body (str, default: '' ) –

    TSP content to load on the instrument, overwritten if file_path defined.

  • file_path (str | PathLike[str] | None, default: None ) –

    a *.tsp file from the local filesystem to read and use as the script_body.

  • run_script (bool, default: False ) –

    Boolean indicating if the script should be run immediately after loading.

  • to_nv_memory (bool, default: False ) –

    Boolean indicating if the script is to be saved to non-volatile memory.

poll_query

poll_query(
    number_of_polls: int,
    query: str,
    wanted_val: float | str,
    sleep_time: float = 0.4,
    tolerance: float = 0,
    percentage: bool = False,
    invert_range: bool = False,
    invalid_values: list[float | str] | None = None,
) -> None

Poll the query until the wanted value appears.

Parameters:
  • number_of_polls (int) –

    The number of times to poll the query.

  • query (str) –

    The query to poll.

  • wanted_val (float | str) –

    The desired value to poll for.

  • sleep_time (float, default: 0.4 ) –

    The time to wait between polls (in seconds).

  • tolerance (float, default: 0 ) –

    The acceptable difference between two floating point values.

  • percentage (bool, default: False ) –

    A boolean indicating what kind of tolerance check to perform. False means absolute tolerance: +/- tolerance. True means percent tolerance: +/- (tolerance / 100) * value.

  • invert_range (bool, default: False ) –

    A boolean indicating when to stop polling. False means polling until the wanted value appears. True means polling until a different value from the wanted value appears.

  • invalid_values (list[float | str] | None, default: None ) –

    A list of values that should never be received when polling.

Raises:
  • AssertionError

    Indicating that the device never reached the wanted value.

print_buffers

print_buffers(*args: str) -> None

Print one of more of the device’s buffers to the console.

Parameters:
  • args (str, default: () ) –

    The buffer name(s) to print.

query

query(
    query: str, *, verbose: bool = True, remove_quotes: bool = False, allow_empty: bool = False
) -> str

Send a query to the device and return the result.

Parameters:
  • query (str) –

    The query to send to the device.

  • verbose (bool, default: True ) –

    Set this to False in order to disable printouts.

  • remove_quotes (bool, default: False ) –

    Set this to True to remove all double quotes from the returned value.

  • allow_empty (bool, default: False ) –

    Set this to True if an empty return string is permitted.

Returns:
  • str

    The results of the query.

Raises:
  • Error

    An error occurred while sending the command.

  • SystemError

    An empty string was returned from the device.

query_binary

query_binary(
    query: str,
    verbose: bool = True,
    datatype: BINARY_DATATYPES = "f",
    is_big_endian: bool = False,
    container: type[T] | Callable[[Iterable[Any]], T] = list,
    delay: float | None = None,
    header_fmt: BINARY_HEADERS = "ieee",
    expect_termination: bool = True,
    data_points: int = 0,
    chunk_size: int | None = None,
) -> T

Send a query to the device and return the binary values.

Parameters:
  • query (str) –

    The query to send to the device.

  • verbose (bool, default: True ) –

    Set this to False in order to disable printouts.

  • datatype (BINARY_DATATYPES, default: 'f' ) –

    Format string for a single element. See struct module. ‘h’ by default.

  • is_big_endian (bool, default: False ) –

    Are the data in big or little endian order. Defaults to False.

  • container (type[T] | Callable[[Iterable[Any]], T], default: list ) –

    Container type to use for the output data. Possible values are: list, tuple, np.ndarray, etc. Default to list.

  • delay (float | None, default: None ) –

    Delay in seconds between write and read operations. If None, defaults to self.query_delay.

  • header_fmt (BINARY_HEADERS, default: 'ieee' ) –

    Format of the header prefixing the data. Defaults to ‘ieee’.

  • expect_termination (bool, default: True ) –

    When set to False, the expected length of the binary values block does not account for the final termination character (the read termination). Defaults to True.

  • data_points (int, default: 0 ) –

    Number of points expected in the block. This is used only if the instrument does not report it itself. This will be converted in a number of bytes based on the datatype. Defaults to 0.

  • chunk_size (int | None, default: None ) –

    Size of the chunks to read from the device. Using larger chunks may be faster for large amount of data.

Returns:
  • T

    The results of the query.

Raises:
  • Error

    An error occurred while sending the command.

  • SystemError

    An empty string was returned from the device.

query_expect_timeout

query_expect_timeout(invalid_command: str, timeout_ms: int = 200, verbose: bool = True) -> str

Send a query expecting it to time out and fail.

Parameters:
  • invalid_command (str) –

    The bad command to query.

  • timeout_ms (int, default: 200 ) –

    The VISA timeout duration for this function (in milliseconds).

  • verbose (bool, default: True ) –

    A flag to decide if we should print out a message indicating what query was sent.

Returns:
  • str

    The timeout error or the response to the query.

Raises:
  • Error

    Any non-timeout error will be re-raised.

query_less_than

query_less_than(
    query: str,
    value: float | str,
    tolerance: float = 0,
    percentage: bool = False,
    allow_equal: bool = False,
) -> bool

Send the given query and verify the result is less than the expected response.

Parameters:
  • query (str) –

    The command that is being checked.

  • value (float | str) –

    The expected value of the query.

  • tolerance (float, default: 0 ) –

    The acceptable difference between two floating point values.

  • percentage (bool, default: False ) –

    A boolean indicating what kind of tolerance check to perform. False means absolute tolerance: +/- tolerance. True means percent tolerance: +/- (tolerance / 100) * value.

  • allow_equal (bool, default: False ) –

    A boolean indicating if equal results should be allowed.

Returns:
  • bool

    Boolean indicating whether the check passed or failed.

query_raw_binary

query_raw_binary(query: str, verbose: bool = True) -> bytes

Send a command to the device and then read and return the raw binary values.

Parameters:
  • query (str) –

    The query to send to the device.

  • verbose (bool, default: True ) –

    Set this to False in order to disable printouts.

Returns:
  • bytes

    The raw results of the query.

Raises:
  • Error

    An error occurred while sending the command.

  • SystemError

    An empty string was returned from the device.

query_response

query_response(
    query: str,
    value: str | float,
    tolerance: float = 0,
    percentage: bool = False,
    remove_quotes: bool = False,
    custom_message_prefix: str = "",
    allow_empty: bool = False,
) -> tuple[bool, str]

Query the device and verify the result.

Parameters:
  • query (str) –

    The query that is being checked.

  • value (str | float) –

    The expected value of the query.

  • tolerance (float, default: 0 ) –

    The acceptable difference between two floating point values.

  • percentage (bool, default: False ) –

    A boolean indicating what kind of tolerance check to perform. False means absolute tolerance: +/- tolerance. True means percent tolerance: +/- (tolerance / 100) * value.

  • remove_quotes (bool, default: False ) –

    Set this to True to remove all double quotes from the returned value.

  • custom_message_prefix (str, default: '' ) –

    A custom message to be prepended to the failure message.

  • allow_empty (bool, default: False ) –

    Set this to True if an empty return string is permitted.

Returns:
  • tuple[bool, str]

    Tuple containing the boolean verification result and the value returned from the query.

query_response_not

query_response_not(
    query: str, value: str, remove_quotes: bool = False, custom_message_prefix: str = ""
) -> tuple[bool, str]

Query the device and verify the result is not the given value.

Parameters:
  • query (str) –

    The query that is being checked.

  • value (str) –

    The value that the query should not return.

  • remove_quotes (bool, default: False ) –

    Set this to True to remove all double quotes from the returned value.

  • custom_message_prefix (str, default: '' ) –

    A custom message to be prepended to the failure message.

Returns:
  • tuple[bool, str]

    Tuple containing the boolean verification result and the value returned from the query.

read

read() -> str

Return the read results from the VISA resource.

read_raw

read_raw(size: int | None = None) -> bytes

Return the read_raw results from the VISA resource.

Parameters:
  • size (int | None, default: None ) –

    The chunk size to use to perform the reading. Defaults to None, meaning the resource wide set value is set.

Returns:
  • bytes

    The bytes read from the device.

reset

reset() -> None

Send the *RST command followed by an *OPC? query.

*RST only resets the programmable interface settings, it does not change the user interface settings.

reset_visa_timeout

reset_visa_timeout() -> None

Reset the VISA timeout to the default value.

run_script

run_script(script_name: str) -> None

Run a TSP script on the instrument.

Parameters:
  • script_name (str) –

    String indicating the name to of the script to run.

set_and_check

set_and_check(
    command: str,
    value: str | float,
    tolerance: float = 0,
    percentage: bool = False,
    remove_quotes: bool = False,
    custom_message_prefix: str = "",
    *,
    expected_value: str | float | None = None,
    opc: bool = False
) -> str

Send the given command with the given value and then verify the results.

Parameters:
  • command (str) –

    The command to send. For example: smu.output.level

  • value (str | float) –

    The value being set by the command. For example: 10.0

  • tolerance (float, default: 0 ) –

    The acceptable difference between two floating point values.

  • percentage (bool, default: False ) –

    A boolean indicating what kind of tolerance check to perform. False means absolute tolerance: +/- tolerance. True means percent tolerance: +/- (tolerance / 100) * value.

  • remove_quotes (bool, default: False ) –

    Set this to True to remove all double quotes from the returned value.

  • custom_message_prefix (str, default: '' ) –

    A custom message to be prepended to the failure message.

  • expected_value (str | float | None, default: None ) –

    An optional, alternative value expected to be returned.

  • opc (bool, default: False ) –

    Boolean indicating if *OPC? should be queried after sending the command.

Returns:
  • str

    The output of the query portion of the method.

set_if_needed

set_if_needed(
    command: str,
    value: str | float,
    tolerance: float = 0,
    percentage: bool = False,
    remove_quotes: bool = False,
    custom_message_prefix: str = "",
    *,
    expected_value: str | float | None = None,
    opc: bool = False,
    allow_empty: bool = False,
    verify_value: bool = False
) -> tuple[bool, str]

Query the command’s field and update it if the value does not match the input.

Parameters:
  • command (str) –

    The command to send. For example: :AFG:FUNCTION

  • value (str | float) –

    The value being set by the command. For example: SINE

  • tolerance (float, default: 0 ) –

    The acceptable difference between two floating point values.

  • percentage (bool, default: False ) –

    A boolean indicating what kind of tolerance check to perform. False means absolute tolerance: +/- tolerance. True means percent tolerance: +/- (tolerance / 100) * value.

  • remove_quotes (bool, default: False ) –

    Set this to True to remove all double quotes from the returned value.

  • custom_message_prefix (str, default: '' ) –

    A custom message to be prepended to the failure message.

  • expected_value (str | float | None, default: None ) –

    An optional, alternative value expected to be returned.

  • opc (bool, default: False ) –

    Boolean indicating if *OPC? should be queried after sending the command.

  • allow_empty (bool, default: False ) –

    Set this to True if an empty return string is permitted.

  • verify_value (bool, default: False ) –

    Boolean indicating to verify value after write.

Returns:
  • tuple[bool, str]

    Tuple containing the boolean value indicating if the command needed to be set and the value returned from the query.

temporary_visa_timeout

temporary_visa_timeout(temporary_timeout_ms: float) -> Generator[None, None, None]

Set a temporary VISA timeout value for the duration of the context.

This will reset the VISA timeout to the previous value when the context exits.

Parameters:
  • temporary_timeout_ms (float) –

    The temporary VISA timeout value, in milliseconds.

wait_for_srq_event

wait_for_srq_event(timeout: int) -> WaitResponse

Wait for the service request event to happen, up to the given timeout.

Parameters:
  • timeout (int) –

    Time to wait (in seconds).

Returns:
  • WaitResponse

    The WaitResponse for the SRQ event.

wait_for_visa_connection

wait_for_visa_connection(
    wait_time: float, sleep_seconds: int = 5, accept_immediate_connection: bool = False
) -> bool

Wait for a VISA connection to be made to the device.

Parameters:
  • wait_time (float) –

    The maximum time to wait in seconds.

  • sleep_seconds (int, default: 5 ) –

    The number of seconds to sleep in between connection attempts.

  • accept_immediate_connection (bool, default: False ) –

    A boolean indicating if a connection on the first attempt is a valid connection.

Returns:
  • bool

    A boolean indicating if a VISA connection was made within the given time limit.

Raises:
  • AssertionError

    Indicating that the device erroneously connected on the first try.

write

write(command: str, opc: bool = False, verbose: bool = True) -> None

Write a command to the device.

Parameters:
  • command (str) –

    The command to write to the device

  • opc (bool, default: False ) –

    Boolean indicating if *OPC? should be queried after sending the command.

  • verbose (bool, default: True ) –

    Set this to False in order to disable printouts.

Raises:
  • Error

    An error occurred while sending the command.

  • SystemError

    *OPC? did not return “1” after sending the command.

write_raw

write_raw(command: bytes, verbose: bool = True) -> None

Write a raw command to the device.

Parameters:
  • command (bytes) –

    The command to write to the device

  • verbose (bool, default: True ) –

    Set this to False in order to disable printouts.

Raises:
  • Error

    An error occurred while sending the command.