
Originally Posted by
Lesiok
Yes, and now you have to analyze whether the data are complete or not.
This is normal. Serial port knows nothing about packets. Serial port is a stream of bytes. You have to divide the stream into packets and handle them. It should look something like this
void MyObject::toReadThePort()
{
if(serial->isReadable())
{
ArrayBuffer.append(serial->readAll());
while(ArrayBuffer.size() >= ExpectedPacketLength)
{
QByteArray one_packet
= ArrayBuffer.
left(ExpectedPacketLength
);
// Here processing of one_packet
ArrayBuffer.remove(0,ExpectedPacketLength);//delete processed packet
}
receiveData();
}
}
void MyObject::toReadThePort()
{
if(serial->isReadable())
{
ArrayBuffer.append(serial->readAll());
while(ArrayBuffer.size() >= ExpectedPacketLength)
{
QByteArray one_packet = ArrayBuffer.left(ExpectedPacketLength);
// Here processing of one_packet
ArrayBuffer.remove(0,ExpectedPacketLength);//delete processed packet
}
receiveData();
}
}
To copy to clipboard, switch view to plain text mode
Of course I not tried to compile it - it is only a sketch.
I did what you said. And it became more stable. Actually it is working at lower frequency. But i need 2.5kHz. It's working at 500 Hz now.
I changed my code like this :
while(ArrayBuffer.size() >= ExpectedPacketLength)
{
indexOfType = ArrayBuffer.indexOf('s');
if(indexOfType >= 1)
{
ArrayBuffer.remove(0,indexOfType);
}
QByteArray one_packet
= ArrayBuffer.
left(ExpectedPacketLength
);
ArrayBuffer.remove(0,ExpectedPacketLength);
if(one_packet[0] == 's') // 's' buldur kodu yaz dene!
{
for(dummyCounter=0;dummyCounter<ExpectedPacketLength;dummyCounter++)
{
dummyArray[dummyCounter] = one_packet[dummyCounter];
shortArray[dummyCounter] = one_packet[dummyCounter];
}
CRCValue = crcsum(&dummyArray[0],ExpectedPacketLength,CRC_INIT); // Just Checking the CRCValue. not gonna use anywhere.
ReceiveIsSuccessful = crcverify(&dummyArray[0],ExpectedPacketLength);
if(ReceiveIsSuccessful == 1) // Checking if CRC is OK or not
{ .. parse and logging process is here..}
while(ArrayBuffer.size() >= ExpectedPacketLength)
{
indexOfType = ArrayBuffer.indexOf('s');
if(indexOfType >= 1)
{
ArrayBuffer.remove(0,indexOfType);
}
QByteArray one_packet = ArrayBuffer.left(ExpectedPacketLength);
ArrayBuffer.remove(0,ExpectedPacketLength);
if(one_packet[0] == 's') // 's' buldur kodu yaz dene!
{
for(dummyCounter=0;dummyCounter<ExpectedPacketLength;dummyCounter++)
{
dummyArray[dummyCounter] = one_packet[dummyCounter];
shortArray[dummyCounter] = one_packet[dummyCounter];
}
CRCValue = crcsum(&dummyArray[0],ExpectedPacketLength,CRC_INIT); // Just Checking the CRCValue. not gonna use anywhere.
ReceiveIsSuccessful = crcverify(&dummyArray[0],ExpectedPacketLength);
if(ReceiveIsSuccessful == 1) // Checking if CRC is OK or not
{ .. parse and logging process is here..}
To copy to clipboard, switch view to plain text mode
Now here is the other problem:
When i'm trying to get data at 2.5kHz terminal goes like this :
Everything is okay to some point. But when any error occurs (like CRC can be wrong) QT cant keep the data instaneously at that time.
https://postimg.org/image/ilb3tg0d5/
So i added to my code this :
if(ArrayBuffer.size() >= 20000){
.. then the code is the same i wrote above..
if(ArrayBuffer.size() >= 20000){
.. then the code is the same i wrote above..
To copy to clipboard, switch view to plain text mode
Now it's getting better:
https://postimg.org/image/iml1mv26x/
But as we can see suddenly data are escaping from the buffer after its full. When i increment the 20000 to 100000 or much far away its getting better. However board will always send data constantly.
How can i solve this ? I tried to clear the ArrayBuffer after a chunk of data (20000) have been processed. But it didnt fix it. Any idea about this ?
Because i'm thinking QSerialPort cant handle the high frequency communication. Maybe internal read buffer size isn't enough ?
Thanks for help.
Bookmarks