Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Serial Terminal#

Shows how to use various features of QSerialPort .

Terminal shows how to create a terminal for a simple serial interface by using Qt Serial Port .

../_images/terminal-example.png

This example shows the main features of the QSerialPort class, like configuration, I/O implementation and so forth. Also, the class QSerialPortInfo is invoked to display information about the serial ports available in the system.

QSerialPort supports two general programming approaches:

  • The asynchronous (non-blocking) approach. Operations are scheduled and performed when the control returns to Qt’s event loop. QSerialPort emits a signal when the operation is finished. For example, QSerialPort::write() returns immediately. When the data is sent to the serial port, QSerialPort emits bytesWritten().

  • The synchronous (blocking) approach. In non-GUI and multithreaded applications, the waitFor...() functions can be called (i.e. waitForReadyRead() ) to suspend the calling thread until the operation has completed.

In this example, the asynchronous approach is demonstrated. The Blocking Receiver example illustrates the synchronous approach.

Our example contains some GUI widgets:

  • MainWindow (terminal/mainwindow.cpp) - is the main application window that contains all the working logic for the serial port programming, including configuration, I/O processing and so forth, while inheriting the QMainWindow.

  • Console (terminal/console.cpp) - is the central widget of the main window, displaying the transmitted or received data. The widget is derived from the QPlainTextEdit class.

  • SettingsDialog (terminal/settingsdialog.cpp) - is a dialog for configuring the serial port, as well as for displaying the available serial ports and information about them.

The serial port is instantiated in the MainWindow constructor. The main widget is passed as the parent, so the object deletion happens automatically according to the parent and child mechanism in Qt:

def __init__(self, parent):
    QMainWindow(parent),
    m_ui(Ui()::MainWindow),
m_serial(QSerialPort(self))            ...

This example demonstrates the following QSerialPort signals:

  • readyRead() - shows that new data has been received and hence available

  • bytesWritten() - used to check that all data was written successfully

    ...

m_serial.readyRead.connect(self.readData)
m_serial.bytesWritten.connect(self.handleBytesWritten)            ...

Clicking on the Connect button invokes the openSerialPort() slot:

def openSerialPort(self):

    SettingsDialog.Settings p = m_settings.settings()
    m_serial.setPortName(p.name)
    m_serial.setBaudRate(p.baudRate)
    m_serial.setDataBits(p.dataBits)
    m_serial.setParity(p.parity)
    m_serial.setStopBits(p.stopBits)
    m_serial.setFlowControl(p.flowControl)
    if m_serial.open(QIODevice.ReadWrite):
        m_console.setEnabled(True)
        m_console.setLocalEchoEnabled(p.localEchoEnabled)
        m_ui.actionConnect.setEnabled(False)
        m_ui.actionDisconnect.setEnabled(True)
        m_ui.actionConfigure.setEnabled(False)
        showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
                          .arg(p.name, p.stringBaudRate, p.stringDataBits,
                               p.stringParity, p.stringStopBits, p.stringFlowControl))
    else:
        QMessageBox.critical(self, tr("Error"), m_serial.errorString())
        showStatusMessage(tr("Open error"))

In this slot, the settings are read from SettingsDialog and an attempt is made to open and initialize the serial port accordingly. If successful, the status bar displays a message that the opening was successful with the given configuration; otherwise, a messagebox is displayed with the appropriate error code and message. If the serial port settings have never been called then the terminal attempts to open the port with the default settings: 9600 8N1.

Clicking on the Disconnect button invokes the closeSerialPort() slot:

def closeSerialPort(self):

    if m_serial.isOpen():
        m_serial.close()
    m_console.setEnabled(False)
    m_ui.actionConnect.setEnabled(True)
    m_ui.actionDisconnect.setEnabled(False)
    m_ui.actionConfigure.setEnabled(True)
    showStatusMessage(tr("Disconnected"))

In this case, handled by the closure of the serial port.

Clicking on the Configure button invokes the show() slot which belongs to the SettingsDialog widget.

This method (terminal/settingsdialog.cpp) displays the SettingsDialog, in which the user can choose the desired serial port, see the information about the selected port, and set the desired parameters of the given serial port.

Writing Data#

Typing characters in the console invokes the writeData() slot:

def writeData(self, data):

    written = m_serial.write(data)
    if written == data.size():
        m_bytesToWrite += written
        m_timer.start(kWriteTimeout)
    else:
        error = tr("Failed to write all data to port %1.\n"
                                 "Error: %2").arg(m_serial.portName(),
                                                  m_serial.errorString())
        showWriteError(error)

This slot sends the characters typed in the given Console widget to the serial port - see terminal/console.cpp. It also starts a timer to track if the write actually succeeded or not. We use the bytesWritten() signal to make sure that all bytes are actually written. It is connected to the MainWindow::handleBytesWritten() slot:

def handleBytesWritten(self, bytes):

    m_bytesToWrite -= bytes
    if m_bytesToWrite == 0:
        m_timer.stop()

Reading Data#

When the serial port receives new data, the signal readyRead() is emitted, and that signal is connected to the MainWindow::readData() slot:

def readData(self):

    data = m_serial.readAll()
    m_console.putData(data)

This slot reads the data from the serial port and displays that in the Console widget.

Running the Example#

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Example project @ code.qt.io