Home > Mobile >  QT serial port receiving problem of poor real-time performance
QT serial port receiving problem of poor real-time performance

Time:09-17

Question:
QT wrote a computer program, through a serial port receive real-time under a machine with the data of 200 hz, baud rate to 460800, the data size is 21 bytes, the question now is, I have a count on the bytes of the data, so can judge data reception is correct, but every time when received a piece of data (about 190), after receiving a thread will delay> 900 ms, will receive the next data, lead to poor real-time display,
Established under a machine sending frequency and sending data no problem,

Application process:
in the MainWindow:
On the serial port to connect (comPort, SIGNAL (readyRead ()), and this, SLOT (readyReadComData ()));

Void MainWindow: : readyReadComData ()
{
Emit readyReadData comPort (*);
}

Void MainWindow: : comReceiveTask ()
{
//a serial port receives the data connection
ComRecWorker=new receiveComData;
QObject: : connect (this, SIGNAL (readyReadData (QSerialPort&) ), comRecWorker, SLOT (receiveComDataSlot (QSerialPort&) ), Qt: : BlockingQueuedConnection);
ComRecThread=new QThread;
ComRecWorker - & gt; MoveToThread (comRecThread);
}

receiveComData receiving threads:
ReceiveComData: : receiveComData (QObject * parent) : QObject (parent)
{
RecDataBuf=new QByteArray.
RecDataBuf - & gt; The resize (2000);
}

Void receiveComData: : receiveComDataSlot (QSerialPort & amp; ComPort)
{
Bool breakFlag=false, continueFlag=false;

RecDataBuf - & gt; Append (comPort. ReadAll ());

While (recDataBuf - & gt; The size () & gt;=21)//total number of bytes for at least 1 frame
{
IndexHead=recDataBuf - & gt; IndexOf (currentProtocol: : curProtocol. FrameHeadBuf);//frame head
If (indexHead==1)
{
RecDataBuf - & gt; The clear ();
break;
}
The else
{
Int selectTailIndex=indexHead;

While (1)
{//judgment frame tail
IndexTail=recDataBuf - & gt; IndexOf (currentProtocol: : curProtocol frameTailBuf, selectTailIndex + 1);
If (indexTail==1)
{
BreakFlag=true;
break;
}

//frame to find end
If ((indexTail - indexHead + currentProtocol: : curProtocol frameTailBytesLen) & lt; CurrentProtocol: : curProtocol frameTotalLength)
{//check to frame the tail, the number of bytes don't complete
SelectTailIndex=indexTail;
}
Else if ((indexTail - indexHead + currentProtocol: : curProtocol frameTailBytesLen) & gt; CurrentProtocol: : curProtocol frameTotalLength)
{//check to frame the tail, the number of bytes than
RecDataBuf - & gt; Remove (0, indexTail);
ContinueFlag=true;
break;
}
The else
{//check to the correct frame tail and bytes
break;
}
}

If (breakFlag)
{
BreakFlag=false;
break;
}
If (continueFlag)
{
ContinueFlag=false;
continue;
}

//count on the byte
Int tempPos=indexHead + currentProtocol: : curProtocol. FrameHeadBytesLen + currentProtocol: : curProtocol. FrameIDBytesLen;
CurrentProtocol: : curProtocol frameCountBuf=recDataBuf - & gt; Mid (tempPos currentProtocol: : curProtocol frameCountBytesLen);

RecDataBuf - & gt; Remove (0, indexHead + currentProtocol: : curProtocol frameTotalLength);


//the debug debugging
Bool ok;
The static long long recSaveTime=0;
QString timestamp=QString: : number (QDateTime: : currentMSecsSinceEpoch ());
Long long timeTemp=timestamp. ToLongLong ();
Int showTime=timeTemp - recSaveTime;
If (showTime & gt; 10)
{
QDebug () & lt; <"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "& lt; QDebug () & lt; <"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "& lt; }
//the debug is shown as: recTask - + + "current milliseconds" frame and current frame milliseconds difference + data count on the byte
QDebug () & lt; <"RecTask -" & lt; RecSaveTime=timeTemp;
}
}
}

debugging results:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

RecTask - "1587352390467", 942, 65

RecTask - "1587352390477" 10 66

RecTask - "1587352390477" 0, 67

RecTask - "1587352390477" 0, 68

RecTask - "1587352390477" 0, 69

RecTask - "1587352390477" 0, 70

.

RecTask - "1587352390481" 0, 249

RecTask - "1587352390481" 0, 250

RecTask - "1587352390481" 0, 251

RecTask - "1587352390482" 1 252

RecTask - "1587352390482" 0, 253

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

RecTask - "1587352391412", 930, 254

RecTask - "1587352391412" 0, 255

RecTask - "1587352391412" 0 0

RecTask - "1587352391412" 0 1

RecTask - "1587352391412" 0 2

RecTask - "1587352391412" 0 3

RecTask - "1587352391412" 0 4

RecTask - "1587352391412" 0 5

.

RecTask - "1587352391416" 0, 184

RecTask - "1587352391416" 0, 185

RecTask - "1587352391416" 0, 186

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • Qt
  • Related