stoner_measurement.instruments.transport.base.BaseTransport

class stoner_measurement.instruments.transport.base.BaseTransport(timeout: float = 2.0)[source]

Bases: ABC

Abstract base for all instrument transport layers.

A transport is responsible for the physical transmission and reception of raw bytes over a specific interface (serial port, Ethernet socket, GPIB bus, etc.). Higher-level protocol formatting is handled separately by BaseProtocol.

Variables:

timeout (float) – Read timeout in seconds. A value of 0 means non-blocking and None means block indefinitely.

Examples

>>> from stoner_measurement.instruments.transport import NullTransport
>>> t = NullTransport()
>>> t.is_open
False
>>> t.open()
>>> t.is_open
True
>>> t.close()
>>> t.is_open
False
abstractmethod close() → None[source]

Close the physical connection to the instrument.

flush() → None[source]

Flush any pending data in the transport buffers.

The default implementation is a no-op; override in subclasses where a hardware flush operation is meaningful.

classmethod from_uri(uri: str) → BaseTransport[source]

Construct the appropriate transport instance from a URI or VISA resource string.

Two input formats are recognised:

URI strings use a scheme://... format:

  • serial:///dev/ttyUSB0?baud_rate=9600 — SerialTransport. On Windows use serial://COM3?baud_rate=9600. Query parameters mirror the SerialTransport constructor keyword arguments: baud_rate (alias baud), data_bits, stop_bits, parity, timeout.

  • tcp://host:port?timeout=2.0 — EthernetTransport.

  • udp://host:port?timeout=2.0 — UdpTransport.

  • gpib://board:address/?timeout=2.0 or gpib://address/?timeout=2.0 — GpibTransport. When only one component is present it is treated as the address; the board defaults to 0.

VISA resource strings follow the IVI/VISA convention and do not contain ://:

  • GPIB[N]::primary[::secondary][::INSTR] — GpibTransport.

  • TCPIP[N]::host::port::SOCKET — EthernetTransport.

  • TCPIP[N]::host::INSTR — EthernetTransport on the default SCPI port 5025.

  • ASRL<port>::INSTR — SerialTransport. The port name is everything after ASRL and before ::INSTR, e.g. ASRL/dev/ttyUSB0::INSTR or ASRLCOM3::INSTR.

Parameters:

uri (str) – URI string or VISA resource string describing the transport.

Returns:

A concrete transport instance corresponding to uri.

Return type:

(BaseTransport)

Raises:

ValueError – If the URI scheme or VISA resource type is not supported, or if required components (host, port, address) are missing.

Examples

>>> from stoner_measurement.instruments.transport import BaseTransport, SerialTransport
>>> t = BaseTransport.from_uri("serial:///dev/ttyUSB0?baud_rate=9600")
>>> isinstance(t, SerialTransport)
True
>>> t.port
'/dev/ttyUSB0'
>>> t.baud_rate
9600
>>> from stoner_measurement.instruments.transport import EthernetTransport
>>> t = BaseTransport.from_uri("tcp://192.168.1.100:5025")
>>> isinstance(t, EthernetTransport)
True
>>> t.host
'192.168.1.100'
>>> t.port
5025
>>> from stoner_measurement.instruments.transport import GpibTransport
>>> t = BaseTransport.from_uri("GPIB0::22::INSTR")
>>> isinstance(t, GpibTransport)
True
>>> t.address
22
>>> t.board
0
property is_open: bool

True if the transport connection is currently open.

property lock_key: str | None

Canonical key used to coordinate transport-level access locks.

Returns:

Canonicalised key derived from transport_address, or None when no stable resource identity is available.

Return type:

(str | None)

abstractmethod open() → None[source]

Open the physical connection to the instrument.

Raises:

ConnectionError – If the connection cannot be established.

query(data: bytes, num_bytes: int | None = None, slow: int | None = False) → bytes[source]

Perform a write and then read operation in series.

Parameters:

data (bytes) – Raw bytes to transmit.

Keyword Arguments;
num_bytes (int | None):

Optional maximum frame size for this call. When None, the transport uses the protocol-defined frame-size limit.

slow (int|None, None):

Whether to expect the write to result in a slow query and thus to pause for slow milliseconds afterwards.

Returns:

The bytes received from the instrument.

Return type:

(bytes)

Raises:
abstractmethod read(num_bytes: int | None = None) → bytes[source]

Read one response frame from the instrument.

Parameters:

num_bytes (int | None) – Optional maximum frame size for this call. When None, the transport uses the protocol-defined frame-size limit.

Returns:

The bytes received from the instrument.

Return type:

(bytes)

Raises:
read_status_byte() → int | None[source]

Return the instrument status byte via an out-of-band mechanism.

Some transports (notably GPIB) can perform a serial poll to read the IEEE 488.2 status byte (STB) without sending a command, allowing the caller to check whether an error condition exists before issuing an expensive error-queue query.

The default implementation returns None, indicating that no out-of-band mechanism is available. Subclasses that support hardware-level status polling (e.g. GpibTransport) should override this method.

If read_status_byte() returns None, check_for_errors() will fall back to issuing the protocol’s error_query command directly.

Returns:

The 8-bit status byte, or None if not supported.

Return type:

(int | None)

Examples

>>> from stoner_measurement.instruments.transport import NullTransport
>>> NullTransport().read_status_byte() is None
True
read_until(terminator: bytes = b'\n') → bytes[source]

Read bytes from the instrument until terminator is encountered.

The default implementation repeatedly calls read() and appends complete frames until terminator is found.

Parameters:

terminator (bytes) – Byte sequence that marks the end of a response. Defaults to b"\n".

Returns:

All bytes received up to and including the terminator.

Return type:

(bytes)

Raises:

Examples

>>> from stoner_measurement.instruments.transport import NullTransport
>>> t = NullTransport(responses=[b"hello\n"])
>>> t.open()
>>> t.read_until(b"\n")
b'hello\n'
>>> t.close()
set_protocol(protocol: object) → None[source]

Bind a protocol instance to this transport.

Transports that need protocol-specific framing details (for example, a read terminator) can override _apply_protocol() to consume the supplied protocol object.

Parameters:

protocol (object) – Protocol instance used by the owning instrument.

suppress_status_error_check() → Iterator[None][source]

Temporarily permit recovery commands despite a stale status error.

Most transports do not reject writes based on an out-of-band status byte, so the base implementation is a no-op. Status-aware transports override this context manager to suspend only their low-level status rejection while an instrument driver clears or resets the device.

property timeout: float

Read timeout in seconds.

property transport_address: str

Human-readable address string identifying the remote endpoint.

Subclasses override this to expose the connection address in a format appropriate to the transport type (e.g. "192.168.1.100:5025" for TCP, "GPIB0::22::INSTR" for GPIB, "/dev/ttyUSB0" for serial).

The default implementation returns an empty string, which is used by NullTransport and any transport that does not carry an addressable endpoint.

Returns:

Address string, or "" if not applicable.

Return type:

(str)

Examples

>>> from stoner_measurement.instruments.transport import NullTransport
>>> NullTransport().transport_address
''
wait_for_srq(timeout: float) → bool | None[source]

Wait for SRQ natively, or return None when unsupported.

abstractmethod write(data: bytes, slow: int | None = None) → int[source]

Send data to the instrument.

Parameters:

data (bytes) – Raw bytes to transmit.

Keyword Arguments:

slow (int|none, None) – Whether to except the response to be slow and thus to pause for slow milliseconds.

Returns:

A status byte where available

Raises: