QSerialInstrument#

class QInstrument.lib.QSerialInstrument.QSerialInstrument(portName=None, **kwargs)[source]#

Bases: QAbstractInstrument

Base class for instruments connected via serial ports.

Extends QAbstractInstrument with a serial transport layer and command-response communication helpers. Holds a QSerialInterface by composition and delegates raw I/O through it. Concrete instrument classes inherit from this, declare a comm class attribute with serial parameters, and override identify() to verify the connected device.

The full communication API available to concrete instruments is:

  • transmit() — send a command with no response expected

  • handshake() — send a command and return the raw response

  • expect() — send a command and test the response string

  • getValue() — send a command and return a typed value

All four methods are defined here. A future transport subclass (e.g. QGPIBInstrument) would provide the same API over a different physical layer.

comm#

Serial parameters passed to QSerialInterface on construction. Subclasses define this as a class attribute using the enum aliases re-exported here (e.g. baudRate=QSerialInstrument.BaudRate.Baud9600).

Type:

dict

BaudRate#

Alias for QSerialPort.BaudRate.

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

comm: dict = {}#
identify()[source]#

Return True if the connected device is the expected instrument.

The base implementation always returns True. Subclasses should override this to query the device and verify its identity.

Return type:

bool

Returns:

boolTrue if the device is recognized, False otherwise.

open(portName)[source]#

Open a specific serial port and verify the connected device.

Opens portName via the interface, then calls identify(). Closes the port and returns False if identification fails.

Parameters:

portName (str) – Serial port name without the system path prefix (e.g. 'ttyUSB0', 'COM1').

Return type:

bool

Returns:

boolTrue if the port is open and the device identified.

isOpen()[source]#

Return True if the serial interface is currently open.

Return type:

bool

close()[source]#

Close the serial interface.

Return type:

None

find()[source]#

Scan all available serial ports to locate the instrument.

Calls open() on each port returned by QSerialPortInfo.availablePorts() until one succeeds.

Return type:

QSerialInstrument

Returns:

QSerialInstrument – The instance itself, whether or not a device was found. Call isOpen() to check the result.

transmit(data)[source]#

Transmit data to the instrument via the serial interface.

Parameters:

data (str | bytes) – Data to send. See QSerialInterface.transmit().

Return type:

None

receive(**kwargs)[source]#

Read a response from the instrument via the serial interface.

Parameters:

**kwargs – Passed through to QSerialInterface.receive().

Return type:

str | bytes

Returns:

str | bytes – Response from the instrument.

handshake(data, **kwargs)[source]#

Transmit a command and return the instrument’s response.

Parameters:
  • data (str) – Command string to send to the instrument.

  • **kwargs – Passed through to receive().

Return type:

str

Returns:

str – Stripped response string from the instrument.

expect(query, response, **kwargs)[source]#

Return True if the instrument’s response contains response.

Parameters:
  • query (str) – Command string to send to the instrument.

  • response (str) – Substring expected in the instrument’s reply.

  • **kwargs – Passed through to receive().

Return type:

bool

Returns:

boolTrue if response appears in the instrument’s reply.

getValue(query, dtype=<class 'float'>)[source]#

Query the instrument and return a typed value.

Parameters:
  • query (str) – Command string that elicits a single-value response.

  • dtype (type, optional) – Converts the response string to the desired type. Default: float.

Return type:

bool | int | float | str | None

Returns:

PropertyValue or None – Value converted by dtype, or None if conversion fails.

classmethod example(portname=None)[source]#

Connect to an instrument and print its current settings.

Creates a QCoreApplication, opens the instrument on portname (or auto-detects it with find() when portname is None), then prints the instrument repr.

Intended to be run from __main__ in each instrument module:

if __name__ == '__main__':
    QMyInstrument.example()
Parameters:

portname (str | None, optional) – Serial port to open (e.g. '/dev/ttyUSB0'). If None, all available ports are scanned via find().

Return type:

None