stoner_measurement.instruments.transport.serial_transport.SerialTransport

class stoner_measurement.instruments.transport.serial_transport.SerialTransport(port: str, *, baud_rate: int = 9600, data_bits: int = 8, stop_bits: float = 1, parity: str = 'N', xonxoff: bool = False, rtscts: bool = False, timeout: float = 2.0)[source]

Bases: BaseTransport

Serial-port transport using pyserial.

The default parameters match the most common factory settings found on scientific instruments (Keithley, Agilent/Keysight, Oxford Instruments, Lakeshore, Stanford Research Systems, etc.): 9600 baud, 8 data bits, 1 stop bit, no parity, no flow control. Adjust when an instrument’s manual specifies different values.

Variables:
  • port (str) – System device name (e.g. "/dev/ttyUSB0" or "COM3").

  • baud_rate (int) – Baud rate in bits per second. Common values: 9600, 19200, 57600, 115200. Defaults to 9600.

  • data_bits (int) – Number of data bits per character (5–8). Defaults to 8.

  • stop_bits (float) – Number of stop bits (1, 1.5, or 2). Defaults to 1.

  • parity (str) – Parity mode: "N" (none), "E" (even), "O" (odd), "M" (mark), or "S" (space). Defaults to "N".

  • xonxoff (bool) – Enable software (XON/XOFF) flow control. Defaults to False.

  • rtscts (bool) – Enable hardware (RTS/CTS) flow control. Defaults to False.

  • timeout (float) – Read timeout in seconds. Defaults to 2.0.

Examples

>>> from stoner_measurement.instruments.transport import SerialTransport
>>> t = SerialTransport(port="/dev/ttyUSB0", baud_rate=9600)
>>> t.port
'/dev/ttyUSB0'
>>> t.baud_rate
9600
>>> t.xonxoff
False
>>> t.rtscts
False
close() → None[source]

Close the serial port.

flush() → None[source]

Flush both input and output buffers of the serial port.

open() → None[source]

Open the serial port.

Raises:

ConnectionError – If the port cannot be opened.

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

Read one response frame from the serial port.

Parameters:

num_bytes (int | None) – Optional maximum frame size for this read. When None, the protocol-defined frame-size limit is used.

Returns:

Bytes received.

Return type:

(bytes)

Raises:
read_until(terminator: bytes = b'\n') → bytes[source]

Read from the serial port until terminator is received.

Uses pyserial’s native read_until for efficiency.

Parameters:

terminator (bytes) – Terminator byte sequence. Defaults to b"\n".

Returns:

All bytes up to and including the terminator.

Return type:

(bytes)

Raises:
property transport_address: str

Return the serial port device name.

Returns:

Port name, e.g. "/dev/ttyUSB0" or "COM3".

Return type:

(str)

Examples

>>> from stoner_measurement.instruments.transport import SerialTransport
>>> SerialTransport(port="/dev/ttyUSB0").transport_address
'/dev/ttyUSB0'
write(data: bytes, slow: int | None = None) → int[source]

Send data over the serial port.

Parameters:

data (bytes) – Raw bytes to transmit.

Keyword Arguments:

slow (int | None) – Optional delay in milliseconds to wait after writing.

Raises:

ConnectionError – If the port is not open.