QSerialInterface#

class QInstrument.lib.QSerialInterface.QSerialInterface(portName='', eol='', timeout=None, baudRate=None, dataBits=None, stopBits=None, parity=None, flowControl=None, **kwargs)[source]#

Bases: QSerialPort

Serial port wrapper providing framed I/O for instrument communication.

Wraps QSerialPort to provide raw serial I/O with custom end-of-line handling for transmit() and receive(). Intended to run in a dedicated worker thread owned by QSerialInstrument; port discovery and device identification are handled by the instrument layer, not here.

Parameters:
  • portName (str) – Name of the serial port to open on construction, without the system-dependent path prefix (e.g. 'ttyUSB0', 'COM1'). Pass an empty string (default) to skip opening on construction.

  • eol (bytes | str) – End-of-line sequence appended to outgoing strings by transmit() and used as the read terminator by receive(). Default: '' (no terminator).

  • timeout (int | None) – Milliseconds to wait for incoming data before giving up. Default: None, which is treated as 100 ms.

  • baudRate (QSerialPort.BaudRate | None) – Port baud rate. Uses the QSerialPort default if None.

  • dataBits (QSerialPort.DataBits | None) – Number of data bits. Uses the QSerialPort default if None.

  • stopBits (QSerialPort.StopBits | None) – Number of stop bits. Uses the QSerialPort default if None.

  • parity (QSerialPort.Parity | None) – Parity mode. Uses the QSerialPort default if None.

  • flowControl (QSerialPort.FlowControl | None) – Flow control mode. Uses the QSerialPort default if None.

eol#

End-of-line sequence used for read/write termination.

Type:

bytes

timeout#

Read timeout in milliseconds.

Type:

int

BaudRate#

Alias for QSerialPort.BaudRate. Use as QSerialInstrument.BaudRate.Baud9600 in comm dicts.

Type:

type

DataBits#

Alias for QSerialPort.DataBits.

Type:

type

StopBits#

Alias for QSerialPort.StopBits.

Type:

type

Parity#

Alias for QSerialPort.Parity.

Type:

type

FlowControl#

Alias for QSerialPort.FlowControl.

Type:

type

Examples

>>> iface = QSerialInterface(eol='\n')
>>> iface.open('ttyUSB0')
open(portName)[source]#

Open the serial port for read/write access.

Parameters:

portName (str) – Name of the serial port device file, without the system-dependent path prefix. Examples: 'ttyUSB0', 'COM1'.

Return type:

bool

Returns:

boolTrue if the port was opened successfully.

transmit(data)[source]#

Transmit data to the instrument.

Strings are encoded to bytes and appended with eol before transmission. Raw bytes are written as-is without appending eol.

Parameters:

data (str | bytes) – Data to transmit. Pass a str for normal ASCII commands; pass bytes for binary payloads that must not be modified.

Return type:

None

receive(eol=None, raw=False)[source]#

Read from the serial interface until the end-of-line sequence.

Reads available data into a buffer until eol is found or the read times out. The EOL bytes are stripped from the returned value.

Intended to run in a dedicated worker thread (see QInstrumentWidget), where blocking the thread with waitForReadyRead() is correct and prevents reentrancy.

Parameters:
  • eol (str | bytes | None) – End-of-line sequence to match. Defaults to the instance eol attribute.

  • raw (bool) – If True, return the response as bytes. If False, decode and return as str. Default: False.

Return type:

str | bytes

Returns:

str | bytes – Data received from the instrument, with the EOL sequence stripped. Returns an empty value on timeout.

readn(n=1)[source]#

Receive exactly n bytes from the instrument.

Parameters:

n (int) – Number of bytes to read. Default: 1.

Return type:

bytes

Returns:

bytes – Data received from the instrument. May be shorter than n if the read times out.

sendbreak(duration=250)[source]#

Send a break signal to the instrument.

Some instruments use a serial break to reset their communication state after a desynchronisation (e.g. a timeout caused by a dropped response). This method is not currently called anywhere; it is retained as a building block for future error-recovery logic. A complete recovery strategy would: detect failure (timeout in receive(), None from QSerialInstrument.getValue()), flush buffers, call sendbreak(), and retry before surfacing a persistent failure to the caller.

Parameters:

duration (int) – Duration of the break state in milliseconds. Valid range: [1, 500]. Default: 250.

Return type:

None