当前位置:网站首页>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.

边栏推荐
猜你喜欢

Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)

基于QT的CAN通讯数据实时波形显示(连载八)====“子函数或新类调用ui控件”

Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
CPU的三种工作模式:实模式、保护模式、长模式

Anaconda 安装低版本tensorflow

i++与 ++i 的区别

探究make_shared效率

动态规划入门 JS
![[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]](/img/96/a98e8b813a2fd9d0a44d3121aaee6a.png)
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]

QT串口动态实时显示大量数据波形曲线(五)========“最终完美解决版”
随机推荐
基于MATLAB 2021b的机器学习、深度学习
Configure MMdetection environment and train
虚拟机栈帧结构
边境的悍匪—机器学习实战:第六章 决策树
R-GIS: 如何用R语言实现GIS地理空间分析及模型预测
关于map对key自定义排序
Diwen serial screen production (serialization 1) ===== preparation work
华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
CLUE Model Construction Method, Model Validation and Land Use Change Scenario Prediction
openssl 1.1.1编译语句
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及利用TIM输出比较配置PWM】
QT连载4:基于QT和STM32H750的LORA试验平台(3)
动态规划进阶 JS
原创 Acegi 1.03 安全机制
边境的悍匪—机器学习实战:第八章 降维
QT串口和CAN数据动态实时显示最后日志
基于遥感解译与GIS技术环境影响评价图件制作(最新导则)
Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]
求职准备知识点
配置MMdetection环境并训练