functions

Module containing helpers for the tm_devices package.

check_for_update

check_for_update(package_name: str = PACKAGE_NAME, index_name: str = 'pypi') -> None

Check for an update for the provided package.

Parameters:
  • package_name (str, default: PACKAGE_NAME ) –

    Check for an update for the provided package.

  • index_name (str, default: 'pypi' ) –

    The name of the index (pypi|test.pypi) to check for the package.

check_network_connection

check_network_connection(device_name: str, ip_address: str) -> tuple[bool, str]

Check the network connection to the device using the external ping command.

Parameters:
  • device_name (str) –

    The name of the device.

  • ip_address (str) –

    The ip address of the device.

Returns:
  • tuple[bool, str]

    A boolean indicating if there is a network connection and a string with the result of the ping command.

check_port_connection

check_port_connection(
    device_name: str, ip_address: str, port: int, timeout_seconds: int = 5
) -> bool

Check if the given port is open on the device.

Parameters:
  • device_name (str) –

    The name of the device.

  • ip_address (str) –

    The ip address.

  • port (int) –

    The port to check.

  • timeout_seconds (int, default: 5 ) –

    The number of seconds to use as the socket timeout.

Returns:
  • bool

    A boolean indicating if the port is open.

check_visa_connection

check_visa_connection(config_entry: DeviceConfigEntry, visa_library: str, device_name: str) -> bool

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

Parameters:
  • config_entry (DeviceConfigEntry) –

    The device listed in the config of the framework to check.

  • visa_library (str) –

    Indicates which visa library to use for the connection.

  • device_name (str) –

    The name of the device.

Returns:
  • bool

    a boolean indicating if the visa connection was successful

create_visa_connection

create_visa_connection(
    device_config_entry: DeviceConfigEntry,
    visa_library: str,
    *,
    retry_connection: bool = False,
    second_connection_attempt_delay: int = 60,
    verbose_connection_failure_logging: bool = True
) -> MessageBasedResource

Create a VISA resource.

Parameters:
  • device_config_entry (DeviceConfigEntry) –

    The device config entry.

  • visa_library (str) –

    A string containing the VISA library to use to create a ResourceManager.

  • retry_connection (bool, default: False ) –

    Boolean indicating if a second connection attempt should be made. If True, two attempts are made to establish a VISA connection, with a configurable delay in between each attempt.

  • second_connection_attempt_delay (int, default: 60 ) –

    The number of seconds to wait in between the first and second connection attempts when retry_connection=True.

  • verbose_connection_failure_logging (bool, default: True ) –

    Indicates if the connections failures should be logged with an ERROR level. If False, the failures will be logged with a DEBUG level.

Returns:
Raises:
  • ConnectionError

    The VISA connection couldn’t be created to the device.

detect_visa_resource_expression

detect_visa_resource_expression(input_str: str) -> tuple[str, str, str | None] | None

Check if a given string is a VISA resource expression.

This function will check if a string is a VISA resource expression and pull out the pieces needed to make a connection using the DeviceManager.

The pieces consist of
  • The connection type, e.g. TCPIP, GPIB (e.g. GPIB0).
  • The address of the device, an IP address (with port separated by a colon for SOCKET connections), hostname, or string in the format model-serial.
  • The LAN device endpoint, e.g. “inst0”.
Parameters:
  • input_str (str) –

    The string to check for a VISA resource expression.

Returns:
  • tuple[str, str, str | None] | None

    A tuple with the connection information parts.

get_model_series

get_model_series(model: str) -> str

Get the series string from the full model number.

Parameters:
  • model (str) –

    The full model string (ex. MSO58, LPD64).

Returns:
  • str

    The model series string (ex. MSO5, LPD6).

get_version

get_version(version_string: str) -> Version

Get a Version object from a string.

Parameters:
  • version_string (str) –

    The string containing the version to create.

Returns:

get_visa_backend

get_visa_backend(visa_lib_path: str) -> str

Create a human-readable VISA backend name based on the currently used VISA library path.

Parameters:
  • visa_lib_path (str) –

    The path to the currently used VISA library.

Returns:
  • str

    A string indicating the current VISA backend.

ping_address

ping_address(address: str, timeout: int = 2) -> str

Ping the given address.

Parameters:
  • address (str) –

    The address to ping, either an IP or hostname.

  • timeout (int, default: 2 ) –

    The timeout to use, in seconds.

Returns:
  • str

    The response of the ping command.

register_additional_usbtmc_mapping

register_additional_usbtmc_mapping(model_series: str, *, model_id: str, vendor_id: str) -> None

Register USBTMC connection information for a device that doesn’t have native tm_devices USBTMC support.

This function adds an additional mapping between the given model_series and the USBTMC connection information provided. This mapping can then be used by tm_devices to create a USBTMC connection to the device.

Parameters:
  • model_series (str) –

    The model series string. For VISA devices, the model series is based on the model that is returned from the *IDN? query (See the get_model_series() function for details). For REST API devices, the model series is provided via the device_driver parameter in the configuration file, environment variable, or Python code.

  • model_id (str) –

    The hexadecimal Model ID used to create the USBTMC resource expression.

  • vendor_id (str) –

    The hexadecimal Vendor ID used to create the USBTMC resource expression.

sanitize_enum

sanitize_enum(value: str, enum_class: type[CustomStrEnum]) -> CustomStrEnum

Sanitize a string value into its enum value.

Parameters:
  • value (str) –

    The value to check for.

  • enum_class (type[CustomStrEnum]) –

    The Enum class to look in.

Returns:
  • CustomStrEnum

    The enum value corresponding to the string value passed in.

Raises:
  • TypeError

    Indicates that the value doesn’t exist in the enum

string_in_enum

string_in_enum(value: str, enum_class: EnumMeta) -> bool

Check if a string value exists in an Enum class object.

Parameters:
  • value (str) –

    The value to check for.

  • enum_class (EnumMeta) –

    The Enum class to look in.

Returns:
  • bool

    A boolean indicating if the string exists in the Enum.