stoner_measurement.instruments.transport.base.BaseTransport¶
- class stoner_measurement.instruments.transport.base.BaseTransport(timeout: float = 2.0)[source]¶
Bases:
ABCAbstract 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
0means non-blocking andNonemeans 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
- 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 useserial://COM3?baud_rate=9600. Query parameters mirror theSerialTransportconstructor keyword arguments:baud_rate(aliasbaud),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.0orgpib://address/?timeout=2.0—GpibTransport. When only one component is present it is treated as the address; the board defaults to0.
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—EthernetTransporton the default SCPI port5025.ASRL<port>::INSTR—SerialTransport. The port name is everything afterASRLand before::INSTR, e.g.ASRL/dev/ttyUSB0::INSTRorASRLCOM3::INSTR.
- Parameters:
uri (str) – URI string or VISA resource string describing the transport.
- Returns:
A concrete transport instance corresponding to uri.
- Return type:
- 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 lock_key: str | None¶
Canonical key used to coordinate transport-level access locks.
- Returns:
Canonicalised key derived from
transport_address, orNonewhen 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:
ConnectionError – If the transport is not open.
TimeoutError – If no data is received within
timeoutseconds.OSError – If a low-level I/O error occurs.
- 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:
ConnectionError – If the transport is not open.
TimeoutError – If no data is received within
timeoutseconds.
- 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()returnsNone,check_for_errors()will fall back to issuing the protocol’serror_querycommand directly.- Returns:
The 8-bit status byte, or
Noneif 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:
ConnectionError – If the transport is not open.
TimeoutError – If no data is received within
timeoutseconds.
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 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
NullTransportand 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
Nonewhen 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:
ConnectionError – If the transport is not open.
OSError – If a low-level I/O error occurs.
InstrumentError – If the write method can detect an error immeditately