Files:
Images:
The Terminal example shows how to create a terminal for a simple serial interface by using SerialPort.

This example shows the main features of the SerialPort class, like configuration, I/O implementation and so forth. Also, the class SerialPortInfo is invoked to display information about the serial ports available in the system.
SerialPort supports two general programming approaches:
In this example, the asynchronous approach is demonstrated. The Blocking Simple Terminal example illustrates the synchronous approach.
Our example contains some GUI widgets:
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 the parent and child mechanism in Qt:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ... serial = new SerialPort(this);
The only SerialPort signal invoked in this example is SerialPort::readyRead(), which shows that new data has been received and hence available:
...
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
...
}
Clicking on the Connect button invokes the openSerialPort() slot:
void MainWindow::openSerialPort() { SettingsDialog::Settings p = settings->settings(); serial->setPort(p.name); if (serial->open(QIODevice::ReadWrite)) { if (serial->setRate(p.rate) && serial->setDataBits(p.dataBits) && serial->setParity(p.parity) && serial->setStopBits(p.stopBits) && serial->setFlowControl(p.flowControl)) { console->setEnabled(true); ui->actionConnect->setEnabled(false); ui->actionDisconnect->setEnabled(true); ui->actionConfigure->setEnabled(false); ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") .arg(p.name).arg(p.stringRate).arg(p.stringDataBits) .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); } else { serial->close(); QMessageBox::critical(this, tr("Error"), tr("Can't configure the serial port: %1,\n" "error code: %2") .arg(p.name).arg(serial->error())); ui->statusBar->showMessage(tr("Open error")); } } else { QMessageBox::critical(this, tr("Error"), tr("Can't opened the serial port: %1,\n" "error code: %2") .arg(p.name).arg(serial->error())); ui->statusBar->showMessage(tr("Configure 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() { serial->close(); console->setEnabled(false); ui->actionConnect->setEnabled(true); ui->actionDisconnect->setEnabled(false); ui->actionConfigure->setEnabled(true); ui->statusBar->showMessage(tr("Disconnected")); }
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) { serial->write(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(); console->putData(data); }
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.
See also Blocking Simple Terminal Example.