作者:zzssdd2
E-mail:[email protected]
一、前言
开发环境:Qt5.12.10 + MinGW
实现的功能
- 串口数据的接收
- ascii字符形式显示与hex字符形式显示
- 时间戳的显示
- 接收数据的统计与显示
- 接收清零
涉及的知识点
QSerialPort
类的使用- 数据格式的转换
QTime
类的使用- 控件
QTextEdit
、QCheckBox
、QPushButton
、QLabel
的使用
二、功能实现
下面开始逐步讲解以上列举的功能实现
2.1、数据读取
在《QT串口助手(二):参数配置》中已经实现了串口参数的配置,参数配置完成后就可以开启串口的数据接收功能了。在QT中的QSerialPort类继承自QIODevice类,所以可以使用QIODevice的*readyRead()*信号来触发数据的接收,在槽函数中实现数据的读取与处理。信号槽连接如下:
/* 接收数据信号槽 */
connect(serial, &QSerialPort::readyRead, this, &Widget::SerialPortReadyRead_slot);
补充:
[signal]
void QIODevice::readyRead()This signal is emitted once every time new data is available for reading from the device’s current read channel. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.
readyRead() is not emitted recursively; if you reenter the event loop or call waitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true).
Note for developers implementing classes derived from QIODevice: you should always emit readyRead() when new data has arrived (do not emit it only because there’s data still to