QT串口助手(三):数据接收

E-mail:zzssdd2@foxmail.com

一、前言

开发环境:Qt5.12.10 + MinGW

实现的功能

串口数据的接收

ascii字符形式显示与hex字符形式显示

时间戳的显示

接收数据的统计与显示

接收清零

涉及的知识点

QSerialPort类的使用

数据格式的转换

QTime类的使用

控件QTextEdit、QCheckBox、QPushButton、QLabel的使用

QT串口助手(三):数据接收

二、功能实现

下面开始逐步讲解以上列举的功能实现

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 () inside a slot connected to the readyRead() signal, the signal will not be reemitted (although () 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 be read in your buffers). Do not emit readyRead() in other conditions.

当有收到新数据信号时,就会执行槽函数里面的数据读取功能:

/*读取串口收到的数据*/ QByteArray bytedata = serial->readAll();

补充:

QByteArray QIODevice::readAll()

Reads all remaining data from the device, and returns it as a byte array.

This function has no way of reporting errors; returning an empty QByteArray can mean either that no data was currently available for reading, or that an error occurred.

2.2、数据转换

若需要将接收到的数据以HEX格式显示,则需要对接收到的数据进行以下处理:

/*将数据转换为hex格式并以空格分隔->去掉头尾空白字符->转换为大写形式*/ framedata = bytedata.toHex(' ').trimmed().toUpper();

补充:

QByteArray::toHex(char separator) const

This is an overloaded function.

Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and the letters a-f.

If separator is not '\0', the separator character is inserted between the hex bytes.

Example:

QByteArray macAddress = QByteArray::fromHex("123456abcdef"); macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef" macAddress.toHex(0); // returns "123456abcdef"

This function was introduced in Qt 5.9.

QByteArray::trimmed() const

Returns a byte array that has whitespace removed from the start and the end.

Whitespace means any character for which the standard C++ isspace() function returns true in the C locale. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

Example:

QByteArray ba(" lots\t of\nwhitespace\r\n "); ba = ba.trimmed(); // ba == "lots\t of\nwhitespace";

Unlike (), trimmed() leaves internal whitespace alone.

QByteArray::toUpper() const

Returns an uppercase copy of the byte array. The bytearray is interpreted as a Latin-1 encoded string.

Example:

QByteArray x("Qt by THE QT COMPANY"); QByteArray y = x.toUpper(); // y == "QT BY THE QT COMPANY" 2.3、添加时间戳

有时为了便于观察数据收发时间,需要在数据前插入时间戳显示。使用QTime类中的方法可以获取当前系统的时间(精确到ms),对数据处理如下:

/*在数据前插入时间戳:[时:分:秒:毫秒]:RX -> 数据*/ framedata = QString("[%1]:RX -> %2").arg(QTime::currentTime().toString("HH:mm:ss:zzz")).arg(framedata);

补充:

[static] QTime::currentTime()

Returns the current time as reported by the system clock.

Note that the accuracy depends on the accuracy of the underlying operating system; not all systems provide 1-millisecond accuracy.

Furthermore, currentTime() only increases within each day; it shall drop by 24 hours each time midnight passes; and, beside this, changes in it may not correspond to elapsed time, if a daylight-saving transition intervenes.

2.4、接收计数

使用一个quint32类型数据对每次接收数据长度进行累加,记录接收数据总数,然后将数据更新到ui界面:

dataTotalRx += bytedata.length(); ui->RxCnt_label->setText(QString::number(dataTotalRx)); 2.5、数据显示

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgfds.html