Terminal Example

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

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. QSerialPort::waitForReadyRead()) to suspend the calling thread until the operation has completed.

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

Our example contains some GUI widgets:

  • MainWindow - 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 - is the central widget of the main window, displaying the transmitted or received data. The widget is derived from the QPlainTextEdit class.
  • SettingsDialog - 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:

MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    serial = new QSerialPort(this);

The only QSerialPort signal invoked in this example is readyRead(), which shows that new data has been received and hence available:

    connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);

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

void MainWindow::openSerialPort()
    SettingsDialog::Settings p = settings->settings();
    if (serial->open(QIODevice::ReadWrite)) {
        showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
    } else {
        QMessageBox::critical(this, tr("Error"), 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 SettingsDialog, then the terminal attempts to open the port with the default settings: 9600 8N1.

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

void MainWindow::closeSerialPort()
    if (serial->isOpen())

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

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

void MainWindow::writeData(const QByteArray &data)

This slot sends the characters typed in the given Console widget to the serial port.

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

void MainWindow::readData()
    QByteArray data = serial->readAll();

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

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

This method 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.

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.



See also Blocking Slave Example.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.