QSerialInterface#
- class QInstrument.lib.QSerialInterface.QSerialInterface(portName='', eol='', timeout=None, baudRate=None, dataBits=None, stopBits=None, parity=None, flowControl=None, **kwargs)[source]#
Bases:
QSerialPortSerial port wrapper providing framed I/O for instrument communication.
Wraps
QSerialPortto provide raw serial I/O with custom end-of-line handling fortransmit()andreceive(). Intended to run in a dedicated worker thread owned byQSerialInstrument; 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 byreceive(). Default:''(no terminator).timeout (int | None) – Milliseconds to wait for incoming data before giving up. Default:
None, which is treated as100ms.baudRate (QSerialPort.BaudRate | None) – Port baud rate. Uses the
QSerialPortdefault ifNone.dataBits (QSerialPort.DataBits | None) – Number of data bits. Uses the
QSerialPortdefault ifNone.stopBits (QSerialPort.StopBits | None) – Number of stop bits. Uses the
QSerialPortdefault ifNone.parity (QSerialPort.Parity | None) – Parity mode. Uses the
QSerialPortdefault ifNone.flowControl (QSerialPort.FlowControl | None) – Flow control mode. Uses the
QSerialPortdefault ifNone.
- BaudRate#
Alias for
QSerialPort.BaudRate. Use asQSerialInstrument.BaudRate.Baud9600incommdicts.- 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:
- Returns:
bool –
Trueif the port was opened successfully.
- transmit(data)[source]#
Transmit data to the instrument.
Strings are encoded to bytes and appended with
eolbefore transmission. Rawbytesare written as-is without appendingeol.- Parameters:
data (str | bytes) – Data to transmit. Pass a
strfor normal ASCII commands; passbytesfor binary payloads that must not be modified.- Return type:
- receive(eol=None, raw=False)[source]#
Read from the serial interface until the end-of-line sequence.
Reads available data into a buffer until
eolis 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 withwaitForReadyRead()is correct and prevents reentrancy.- Parameters:
eol (str | bytes | None) – End-of-line sequence to match. Defaults to the instance
eolattribute.raw (bool) – If
True, return the response asbytes. IfFalse, decode and return asstr. Default:False.
- Return type:
- 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:
- Returns:
bytes – Data received from the instrument. May be shorter than
nif 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(),NonefromQSerialInstrument.getValue()), flush buffers, callsendbreak(), 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: