QSerialInstrument#
- class QInstrument.lib.QSerialInstrument.QSerialInstrument(portName=None, **kwargs)[source]#
Bases:
QAbstractInstrumentBase class for instruments connected via serial ports.
Extends
QAbstractInstrumentwith a serial transport layer and command-response communication helpers. Holds aQSerialInterfaceby composition and delegates raw I/O through it. Concrete instrument classes inherit from this, declare acommclass attribute with serial parameters, and overrideidentify()to verify the connected device.The full communication API available to concrete instruments is:
transmit()— send a command with no response expectedhandshake()— send a command and return the raw responseexpect()— send a command and test the response stringgetValue()— 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
QSerialInterfaceon construction. Subclasses define this as a class attribute using the enum aliases re-exported here (e.g.baudRate=QSerialInstrument.BaudRate.Baud9600).- Type:
- 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:
- Returns:
bool –
Trueif the device is recognized,Falseotherwise.
- 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 returnsFalseif identification fails.- Parameters:
portName (str) – Serial port name without the system path prefix (e.g.
'ttyUSB0','COM1').- Return type:
- Returns:
bool –
Trueif the port is open and the device identified.
- find()[source]#
Scan all available serial ports to locate the instrument.
Calls
open()on each port returned byQSerialPortInfo.availablePorts()until one succeeds.- Return type:
- 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:
- expect(query, response, **kwargs)[source]#
Return True if the instrument’s response contains response.
- 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:
- Returns:
PropertyValue or None – Value converted by dtype, or
Noneif 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 withfind()when portname isNone), then prints the instrument repr.Intended to be run from
__main__in each instrument module:if __name__ == '__main__': QMyInstrument.example()