终端 展示如何为简单串行接口创建终端使用 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 支持 2 种常规编程方式:
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 阻塞从属 example illustrates the synchronous approach.
我们的范例包含一些 GUI 小部件:
串行端口被实例化在 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) : QMainWindow(parent), ui(new Ui::MainWindow) { ... serial = new QSerialPort(this);
仅 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); ... }
点击
Connect
按钮援引
openSerialPort()
槽:
void MainWindow::openSerialPort() { SettingsDialog::Settings p = settings->settings(); serial->setPortName(p.name); serial->setBaudRate(p.baudRate); serial->setDataBits(p.dataBits); serial->setParity(p.parity); serial->setStopBits(p.stopBits); serial->setFlowControl(p.flowControl); if (serial->open(QIODevice::ReadWrite)) { console->setEnabled(true); console->setLocalEchoEnabled(p.localEchoEnabled); ui->actionConnect->setEnabled(false); ui->actionDisconnect->setEnabled(true); ui->actionConfigure->setEnabled(false); showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6") .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits) .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl)); } 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.
点击
Disconnect
按钮援引
closeSerialPort()
槽:
void MainWindow::closeSerialPort() { if (serial->isOpen()) serial->close(); console->setEnabled(false); ui->actionConnect->setEnabled(true); ui->actionDisconnect->setEnabled(false); ui->actionConfigure->setEnabled(true); showStatusMessage(tr("Disconnected")); }
In this case, handled by the closure of the serial port.
Typing characters in the console invokes the
writeData()
槽:
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()
槽:
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 小部件。
点击
Configure
按钮援引
show()
槽属于
SettingsDialog
小部件。
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.
要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .
文件:
图像:
另请参阅 阻塞从属范例 .