当前位置:网站首页>QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
2022-07-30 07:13:00 【transparent light】
一,问题
Online friends met a serial port receiving functions are generally the following,QTA serial port receive function:
//=============串口定义==============//
connect(&mSerial,SIGNAL(readyRead()),this,SLOT(SerialPort_Readyread()));此函数存在问题:接收数据后,Don't regularly throw,That means of serial port data will be executed at the back of the groove function,Then use the following function to receive:
void MainWindow::SerialPort_Readyread()
{
QByteArray recvData = mSerial.readAll();
}
This receivedrecvDataThe data will subcontract.Met a lot of online said to solve the problem,But in the end is always going to have a problem.前面文章中,I also adopts the one way is to add the header and tail.But this method also can appear the situation of the lost data,So after doing a lot of experiment,How to avoid the problem of packet loss.
二,思路
程序嘛,Is to make the change,Not in accordance with the others to give.So in a way,采用下面的方法.
1.Slot functions do not use a serial port data trigger
Use the following function:
connect(&m_timer_test, &QTimer::timeout, this, &MainWindow::handleTimeout_test);
void MainWindow::handleTimeout_test()
{
nRxAllLength = mSerial.bytesAvailable();
recvDataAD = mSerial.readAll();
m_timer_test.stop();
}注:Start the timer for sending instructions,Delay time after receiving.标准的MODBUS协议.
Serial data using timer slot function receiving,First determine number of received data,Then put data intorecvDataAD数组里.For the need to regularly receive occasions may not be applicable,But can be used as a way of thinking.Of course if the underlying SCM regularly send some data,Need microcontroller give an instruction,Delay after a certain period of time after receiving the main primary data.
2.测试方法
The baud rate19200和38400,Instructions issued by taking MCU some data,修改定时器时间,How much time the shortest test timer receives a frame all data.

举例:波特率19200,Set to take SCM bytes45个,Change the time setting:60msAfter the execution slot function.The collection number has remained steady at45个.Show the single-chip microcomputer distributed access45个指令后,返回45个,Serial port receive completely,And can be a pop up.即:QT串口接收45个byte(uint_8),60msCan receive the full packet.按照这种思路,Reduce the timer time.When the timer time to47ms时,The data received is not stable45个,There will be other number,The serial port has can't complete to receive packets.So is scheduled for48ms.Thus the conclusion that:QT接收45个字节时,At least need interval48ms,To get a complete packet,Of course the longer about good.
三,测试结果
(1) 测试结果如下:
| 波特率:19200 | |
| 采集数量(单位u8) | 最小时间(单位ms) |
| 45 | 48 |
| 85 | 64 |
| 125 | 81 |
| 165 | 107 |
| 205 | 127 |
| 285 | 173 |
波特率为19200时,QTReceive complete data need time.

其中:Blue curve for collecting number corresponding to the minimum time interval between,Red curve for the minimum time interval in19200Baud rate under the maximum number of data can transfer.
可得出:(1)数据量越大,Need the longer the time interval between,And the data quantity and interval time is proportional to a linear.(2)一定时间间隔内,To transmit data to transmit data with the largest number of proportion is about2:1.
(2) 测试结果如下:
| 波特率:38400 | |
| 采集数量(单位u8) | 最小时间(单位ms) |
| 45 | 20 |
| 85 | 34 |
| 125 | 48 |
| 165 | 64 |
| 205 | 78 |
| 285 | 99 |
波特率为38400时,QTReceive complete data need time.

其中:Blue curve for collecting number corresponding to the minimum time interval between,Red curve for the minimum time interval in38400Baud rate under the maximum number of data can transfer.
The same can be concluded that:(1)数据量越大,Need the longer the time interval between,And the data quantity and interval time is proportional to a linear.(2)一定时间间隔内,To transmit data to transmit data with the largest number of proportion is about4:1.
波特率19200和波特率38400Different places is to receive maximum transmission data rate,相同的时间内,Baud rate doubled,Transmission data and the maximum transmission data volume ratio decrease one times.
所以初步得出结论:When the baud rate increase,Some data need time interval is linear decrease.
四,结论
(1)QTSerial data is not lost package method is:Extend the time,执行槽函数,取数据.如果是QTFetch the data issued instructions,则需Extend a certain minimum timeT后,To get send the package complete data.If it is single chip microcomputer fixed time uploading data,The need to extend the time2*T后,To get send the package complete data(Unable to determine when to send a message,There's no way to start the timer accurately,So only time intervalt后,Can get a complete packet).
(2)QTA serial port receives the data amount must be case,Baud rate is higher, the better,Need shorter interval of time.But relative to the baud rate can transport for the maximum number of,Utilization rate is falling.
(3)Can't always use online sayQT函数,Ok change myself,May be able to find a solution.
五,存在问题
(1)Use only two kinds of baud rate safely draw the conclusion that,Might not be practical for further high baud rate,但是时间有限,Work can only be hasty conclusion,Interested friends can try higher baud rate is accord with the conclusion that,If not please also believe in telling the,I will have a further validation.
(2)The underlying SCM it takes time to execute a program,Issued instructions and respond to needs time,Has been to avoid,But surely there will be the execution time,延长了QTA serial port receiving time.
(3)Computer execution time without considering,The better the computer,应该QT执行的越快,这个没办法,The company only a computer,There is no way to make the conclusion that.Computer rate should also affect the final result.If a friend repeated trials,The results vary a bit,You can refer to this question.
六,展望
以后有时间了,Can do all the baud rate under the execution time of the,Give a complete receiving schedule,Contains the baud rate,数据数量,需要时间参数.If there is a friend to give a link also ha,表示衷心感谢!
注:Because the friend need time to source different,The login email interface too much trouble,So set up a subscription number,If there is a problem or need to source,You can add subscription number,A message is sent after the source code or if you have any question can leave a message,Will actively solve the problem.

边栏推荐
- CPU的三种工作模式:实模式、保护模式、长模式
- 边境的悍匪—机器学习实战:第十一章 训练深度神经网络
- R语言 生态环境领域应用
- CPU缓存一致性问题
- 边境的悍匪—机器学习实战:第六章 决策树
- Anaconda 安装低版本tensorflow
- "R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment
- 边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程
- jvm之方法区
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
猜你喜欢

Configure MMdetection environment and train

QT串口和CAN数据动态实时显示最后日志

主机和从机配置,建立ssh连接实现Rviz远程控制

边境的悍匪—机器学习实战:第五章 支持向量机

【正点原子】IIC的学习与使用(未完...)

昆仑通态屏幕制作(连载3)---基础篇(按钮串口发送)

Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
![[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations](/img/7f/d9f480ab9a1e542e4fa1fda7978e4c.png)
[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations

CPU缓存一致性问题

“R语言+遥感”的水环境综合评价方法
随机推荐
Knowledge distillation method of target detection
【Qingdao Station】High-level application of SWAT model and modeling of areas without data, uncertainty analysis and climate change, improvement of land use surface pollution impact model and case analy
MATLAB怎么在图像中显示nii文件切片信息?
CLUE Model Construction Method, Model Validation and Land Use Change Scenario Prediction
Receive emails from gmail with pop3
基于QT的CAN通讯数据实时波形显示(连载八)====“子函数或新类调用ui控件”
关于报错vscode
QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”
边境的悍匪—机器学习实战:第八章 降维
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
边境的悍匪—机器学习实战:第十六章使用RNN和注意力机制进行自然语言处理
配置MMdetection环境并训练
边境的悍匪—机器学习实战:第二章 端到端的机器学习项目
用户密码加密编码使用 Bcrypt 代替 MD5,SHA1和SHA256
Difference between logical shift right and arithmetic right shift
基于R语言地理加权回归、主成分分析、判别分析等空间异质性数据分析
Antd 树拖拽一些细节,官网没有,摸坑篇
一文盘点五款 BLDC 风机参考方案,建议先马
海量遥感数据处理与GEE云计算技术应用【基础、进阶】
Flood Control Assessment Report Compilation Method and Flood Modelling under the New Guidelines (HEC-RAS)