Skip to content

Communication Interface¤

Internal support package (instro.lib.cominterface).

Contains shared primitives used by instruments and drivers (types, transports, publishers, utilities).

ComInterfaceClass ¤

ComInterfaceClass(connect_config: ConnectConfig)

Bases: ComInterfaceClassBase

Parent abstract class for ComInterface.

Parameters:

connect_config instance-attribute ¤

connect_config = connect_config

timeout instance-attribute ¤

timeout = -1

enable instance-attribute ¤

enable = False

buf instance-attribute ¤

buf = BytesBuffer()

open ¤

open()

Open the interface.

close ¤

close() -> None

Close the interface when deleting the object.

apply_timeout ¤

apply_timeout(
    timeout_type: TimeoutType, timeout: int | None = None
) -> None

The helper function for apply the timeout.

Parameters:

  • timeout_type ¤
    (TimeoutType) –

    The type of timeout

  • timeout ¤
    (int | None, default: None ) –

    Timeout in seconds. Defaults to None.

set_timeout ¤

set_timeout(seconds: int) -> None

Set the timeout to the interface.

Parameters:

  • seconds ¤
    (int) –

    The timeout setting in seconds

send_raw ¤

send_raw(data: bytes) -> None

Send RAW command to the interface.

Parameters:

  • data ¤
    (bytes) –

    The bytes command

recv_raw ¤

recv_raw(size: int = -1) -> bytes

Receive RAW command from the interface.

Parameters:

  • size ¤
    (int, default: -1 ) –

    The size to read back

Returns:

  • bytes

    The RAW reply from the instrument.

query_raw ¤

query_raw(data: bytes, size: int = -1) -> bytes

Query RAW command to the interface.

Parameters:

  • data ¤
    (bytes) –

    The bytes command.

  • size ¤
    (int, default: -1 ) –

    The size to read back

Returns:

  • str

    The reply from the instrument.

ConnectConfig dataclass ¤

ConnectConfig(
    visa_resource: str = "",
    visa_backend: str = "@ivi",
    serial_config: SerialConfig = SerialConfig(),
    terminator: TerminatorConfig = TerminatorConfig(),
    timeout: TimeoutConfig = TimeoutConfig(),
    network: NetworkConfig = NetworkConfig(),
    interface_type: str = "visa",
)

The config data class for connection.

For non-windows user, will use the self rewrite backend to handle For Windows user, will use the NI-VISA as the backend For using socket, will use the python socket as the backend

If you are using the socket, fill out socket_host, socket_port If using visa and it's non Serial, fill out visa_resource If using visa and it's Serial, fill out visa_resource, serial_config

Attributes:

  • visa_resource (str) –

    The visa resource that you can find via scan

  • visa_backend (str) –

    The visa backend to use. Default is '@ivi'

  • serial_config (SerialConfig) –

    The config for connect with serial

  • terminator (TerminatorConfig) –

    The terminator config

  • timeout (TimeoutConfig) –

    The timeout config

  • network (NetworkConfig) –

    The network config

visa_resource class-attribute instance-attribute ¤

visa_resource: str = ''

visa_backend class-attribute instance-attribute ¤

visa_backend: str = '@ivi'

serial_config class-attribute instance-attribute ¤

serial_config: SerialConfig = field(default_factory=SerialConfig)

terminator class-attribute instance-attribute ¤

terminator: TerminatorConfig = field(default_factory=TerminatorConfig)

timeout class-attribute instance-attribute ¤

timeout: TimeoutConfig = field(default_factory=TimeoutConfig)

network class-attribute instance-attribute ¤

network: NetworkConfig = field(default_factory=NetworkConfig)

interface_type class-attribute instance-attribute ¤

interface_type: str = 'visa'

name property ¤

name: str

DataHandler ¤

DataHandler(interface: ComInterfaceClass)

The data handler for interface and datagram.

interface instance-attribute ¤

interface = interface

send ¤

send(command: str, timeout: int = -1) -> None

Send command to the interface.

Parameters:

  • command ¤
    (str) –

    The command to send.

  • timeout ¤
    (int, default: -1 ) –

    Timeout in seconds. Defaults to -1.

send_raw ¤

send_raw(data: bytes, timeout: int = -1) -> None

Send raw data to the interface.

Parameters:

  • data ¤
    (bytes) –

    The raw data to send.

  • timeout ¤
    (int, default: -1 ) –

    Timeout in seconds. Defaults to -1.

send_dataram ¤

send_dataram(dg: Datagram, timeout: int = -1)

Send datagram to the interface.

Parameters:

  • dg ¤
    (Datagram) –

    The datagram to send.

  • timeout ¤
    (int, default: -1 ) –

    Timeout in seconds. Defaults to -1.

recv ¤

recv(timeout: int = -1, size: int = -1) -> str

Receive data from the interface.

Parameters:

  • timeout ¤
    (int, default: -1 ) –

    Timeout in seconds. Defaults to -1.

  • size ¤
    (int, default: -1 ) –

    The size to read back. Defaults to -1.

Returns:

  • str ( str ) –

    The data from the interface.

recv_raw ¤

recv_raw(timeout: int = -1, size: int = -1) -> bytes

Receive raw data from the interface.

Parameters:

  • timeout ¤
    (int, default: -1 ) –

    Timeout in seconds. Defaults to -1.

  • size ¤
    (int, default: -1 ) –

    The size to read back. Defaults to -1.

Returns:

  • bytes ( bytes ) –

    The raw data from the interface.

recv_datagram ¤

recv_datagram(dg: T, timeout: int = -1) -> T

Receive datagram from the interface.

Parameters:

  • dg ¤
    (T) –

    The datagram to receive.

  • timeout ¤
    (int, default: -1 ) –

    Timeout in seconds. Defaults to -1.

Returns:

  • T ( T ) –

    The datagram from the interface.

query ¤

query(command: str, timeout: int = -1, size: int = -1) -> str

query_raw ¤

query_raw(data: bytes, timeout: int = -1, size: int = -1) -> bytes

query_datagram ¤

query_datagram(send_dg: Datagram, recv_dg: T, timeout: int = -1) -> T

Visa ¤

Visa(connect_config)

Bases: ComInterfaceClass

Child ComInterfaceClass Module of visa.

connect_config instance-attribute ¤

connect_config = connect_config

timeout instance-attribute ¤

timeout = -1

enable instance-attribute ¤

enable = False

buf instance-attribute ¤

buf = BytesBuffer()

open ¤

open()

Open the interface.

close ¤

close() -> None

Close the interface when deleting the object.

apply_timeout ¤

apply_timeout(
    timeout_type: TimeoutType, timeout: int | None = None
) -> None

The helper function for apply the timeout.

Parameters:

  • timeout_type ¤
    (TimeoutType) –

    The type of timeout

  • timeout ¤
    (int | None, default: None ) –

    Timeout in seconds. Defaults to None.

set_timeout ¤

set_timeout(seconds: int) -> None

Set the timeout to the interface.

Parameters:

  • seconds ¤
    (int) –

    The timeout setting in seconds

send_raw ¤

send_raw(data: bytes) -> None

Send RAW command to the interface.

Parameters:

  • data ¤
    (bytes) –

    The bytes command

recv_raw ¤

recv_raw(size: int = -1) -> bytes

Receive RAW command from the interface.

Parameters:

  • size ¤
    (int, default: -1 ) –

    The size to read back

Returns:

  • bytes

    The RAW reply from the instrument.

query_raw ¤

query_raw(data: bytes, size: int = -1) -> bytes

Query RAW command to the interface.

Parameters:

  • data ¤
    (bytes) –

    The bytes command.

  • size ¤
    (int, default: -1 ) –

    The size to read back

Returns:

  • str

    The reply from the instrument.

acquire_resource ¤

acquire_resource()

Context manager to acquire the VISA resource lock for complex operations.

This allows users to perform multiple operations atomically without releasing the lock between operations.

Returns:

  • A context manager that can be used with 'with' statement.

select ¤

The select function for cominterface.

This function will auto select the right backend for user base on the input information.

Parameters:

Returns: