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:
BaseTransportSerial-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
- 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:
ConnectionError – If the port is not open.
TimeoutError – If no data arrives within
timeoutseconds.
- read_until(terminator: bytes = b'\n') bytes[source]¶
Read from the serial port until terminator is received.
Uses pyserial’s native
read_untilfor efficiency.- Parameters:
terminator (bytes) – Terminator byte sequence. Defaults to
b"\n".- Returns:
All bytes up to and including the terminator.
- Return type:
(bytes)
- Raises:
ConnectionError – If the port is not open.
TimeoutError – If no data arrives within
timeoutseconds.