作者:zzssdd2

E-mail:zzssdd2@foxmail.com

一、前言

开发环境:Qt5.12.10 + MinGW

实现的功能

  • 串口数据的接收
  • ascii字符形式显示与hex字符形式显示
  • 时间戳的显示
  • 接收数据的统计与显示
  • 接收清零

涉及的知识点

  • QSerialPort类的使用
  • 数据格式的转换
  • QTime类的使用
  • 控件QTextEditQCheckBoxQPushButtonQLabel的使用

二、功能实现

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

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 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 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 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 simplified(), trimmed() leaves internal whitespace alone.

QByteArray 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 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、数据显示

以上功能完成后将数据显示到接收框中(为了区分不同显示格式,做了不同的颜色显示)。完整的数据接收功能展示如下:

/*
函 数:SerialPortReadyRead_slot
描 述:readyRead()信号对应的数据接收槽函数
输 入:无
输 出:无
*/
void Widget::SerialPortReadyRead_slot()
{
QString framedata;
/*读取串口收到的数据*/
QByteArray bytedata = serial->readAll(); /*数据是否为空*/
if (!bytedata.isEmpty())
{
if(ui->HexDisp_checkBox->isChecked())
{
/*hex显示*/
framedata = bytedata.toHex(' ').trimmed().toUpper();
ui->Receive_TextEdit->setTextColor(QColor(Qt::green));
}
else
{
/*ascii显示*/
framedata = QString(bytedata);
ui->Receive_TextEdit->setTextColor(QColor(Qt::magenta));
} /*是否显示时间戳*/
if (ui->TimeDisp_checkBox->isChecked())
{
framedata = QString("[%1]:RX -> %2").arg(QTime::currentTime().toString("HH:mm:ss:zzz")).arg(framedata);
ui->Receive_TextEdit->append(framedata);
}
else
{
ui->Receive_TextEdit->insertPlainText(framedata);
} /*更新接收计数*/
dataTotalRxCnt += bytedata.length();
ui->RxCnt_label->setText(QString::number(dataTotalRxCnt));
}
}

演示效果如下:

补充:

QColor::QColor(Qt::GlobalColor color)

This is an overloaded function.

Constructs a new color with a color value of color.

enum Qt::GlobalColor

Qt's predefined QColor objects:

Constant Value Description
Qt::white 3 White (#ffffff)
Qt::black 2 Black (#000000)
Qt::red 7 Red (#ff0000)
Qt::darkRed 13 Dark red (#800000)
Qt::green 8 Green (#00ff00)
Qt::darkGreen 14 Dark green (#008000)
Qt::blue 9 Blue (#0000ff)
Qt::darkBlue 15 Dark blue (#000080)
Qt::cyan 10 Cyan (#00ffff)
Qt::darkCyan 16 Dark cyan (#008080)
Qt::magenta 11 Magenta (#ff00ff)
Qt::darkMagenta 17 Dark magenta (#800080)
Qt::yellow 12 Yellow (#ffff00)
Qt::darkYellow 18 Dark yellow (#808000)
Qt::gray 5 Gray (#a0a0a4)
Qt::darkGray 4 Dark gray (#808080)
Qt::lightGray 6 Light gray (#c0c0c0)
Qt::transparent 19 a transparent black value (i.e., QColor(0, 0, 0, 0))
Qt::color0 0 0 pixel value (for bitmaps)
Qt::color1 1 1 pixel value (for bitmaps)

2.6、清除接收

清除接收按键点击后,会清除接收框显示的内容以及接收计数。使用QPushButton的点击信号槽实现如下:

/*
函 数:on_ClearRx_Bt_clicked
描 述:清除接收按键点击信号对应的槽函数
输 入:无
输 出:无
*/
void Widget::on_ClearRx_Bt_clicked()
{
ui->Receive_TextEdit->clear();
ui->RxCnt_label->setText(QString::number(0));
dataTotalRxCnt = 0;
}

三、总结

本篇文章主要是讲述如何对串口数据进行接收和显示。除了上面列出的主要功能外,还需要了解各个控件的操作方法,比如QTextEdit文本的添加、QLabel文本的设置等。还有就是QT中基本的数据类型的数据使用,比如QStringQBytArray等。

QT串口助手(三):数据接收的更多相关文章

  1. QT串口助手(四):数据发送

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 开发环境:Qt5.12.10 + MinGW 实现的功能 串口数据的发送 ascii字符与hex字符的相互转换 自 ...

  2. QT串口助手(五):文件操作

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 开发环境:Qt5.12.10 + MinGW 功能 文件的发送 数据的保存 知识点 QFile类的使用 QTime ...

  3. QT串口助手(二):参数配置

    作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 主要实现功能 串口参数的配置:波特率.数据位.停止位.校验位 本机串口设备的查询与添加显示 串口设备的手动更新与打 ...

  4. python串口助手

    最近项目中要使用模拟数据源通过向外发送数据,以前都是用C#编写,最近在研究python,所以就用python写了一个串口助手,方便以后的测试. 在电脑上通过虚拟串口助手产生两个虚拟串口,运行编写的串口 ...

  5. 玩转X-CTR100 | STM32F4 l X-Assistant串口助手控制功能

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      X-CTR100控制器配套的X-Assis ...

  6. 玩转X-CTR100 | X-Assistant串口助手控制功能

      更多塔克创新资讯欢迎登陆[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]       X-CTR100控制器配套的X-Assistant串口调试 ...

  7. 【转】QT 串口QSerialPort + 解决接收数据不完整问题

    类:QSerialPort 例程:Examples\Qt-5.9.1\serialport\terminal,该例子完美展示了qt串口收发过程,直接在这上面修改就可以得到自己的串口软件.核心方法 // ...

  8. STM32移植RT-Thread后的串口在调试助手上出现:(mq != RT_NULL) assert failed at rt_mq_recv:2085和串口只发送数据不能接收数据问题

    STM32移植RT-Thread后的串口在调试助手上出现:(mq != RT_NULL) assert failed at rt_mq_recv:2085的问题讨论:http://www.rt-thr ...

  9. python3 Serial 串口助手的接收读取数据

    其实网上已经有许多python语言书写的串口,但大部分都是python2写的,没有找到一个合适的python编写的串口助手,只能自己来写一个串口助手,由于我只需要串口能够接收读取数据就可以了,故而这个 ...

随机推荐

  1. [日常摸鱼][poj2777]Count Color-线段树

    辣鸡会考考完啦哈哈哈哈 题意:一块板分成$L$块,每次给一段连续的块染色或者询问一段有几种颜色,颜色的范围$\leq 30$ 我记得我好像做过一个类似的二维染色的问题-不过那个用树状数组直接过掉了- ...

  2. 第 16 章 【硬核!】 垃圾回收相关 GC细讲

    第 16 章 垃圾回收相关概念 1.System.gc() 的理解 1.1.System.gc() 方法 System.gc() 方法 在默认情况下,通过System.gc()者Runtime.get ...

  3. 使用NPOI读取Excel表格内容并进行修改

    前言 网上使用NPOI读取Excel文件的例子现在也不少,本文就是参考网上大神们的例子进行修改以适应自己需求的. 参考博文 http://www.cnblogs.com/restran/p/38894 ...

  4. sklearn: CountVectorize处理及一些使用参数

    sklearn: CountVectorize处理及一些使用参数 CountVectorizer是属于常见的特征数值计算类,是一个文本特征提取方法.对于每一个训练文本,它只考虑每种词汇在该训练文本中出 ...

  5. JPA 缓存

    JPA有两种类型的缓存: EntityManager自身就是一种缓存.事务中从数据库获取的和写入到数据库的数据会被缓存(什么样的数据会被缓存,在后面有介绍).在一个程序中也许会有很多个不同的Entit ...

  6. [leetcode]355. Design Twitter设计实现一个微博系统

    //先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...

  7. [leetcode712]202. Happy Number判断快乐数字

    题目很简单,就是用哈希表存,判断有没有重复 学到了:java中字符串的比较有两种: 1.==这种是比较引用,只用两个字符串变量指向同一个地址才相等 2..equals()这种是值的比较,只要两个字符串 ...

  8. 96. Unique Binary Search Trees1和2

    /* 这道题的关键是:动态表尽量的选取,知道二叉搜索树中左子树的点都比根节点小,右子树的点都比根节点大 所以当i为根节点,左子树有i-1个点,右子树有n-i个点,左右子树就可以开始递归构建,过程和一开 ...

  9. 对CROS OPTIONS预检请求的一些思考

    前后端分离模大势所趋,跨域问题更是老生常谈. 问题背景: 浏览器最基本的安全规范-同源策略.所谓同源是指域名.协议.端口相同.不同源的浏览器脚本(javascript.ActionScript.can ...

  10. sql优化最佳实践

    1.选择最有效率的表连接顺序 首先要明白一点就是SQL 的语法顺序和执行顺序是不一致的 SQL的语法顺序: select   [distinct] ....from ....[xxx  join][o ...