As we said earlier : collect data into the buffer and then analyze them searching for frame start tag (bytes AA AA). Remember that the frame is 11 bytes no 9.
As we said earlier : collect data into the buffer and then analyze them searching for frame start tag (bytes AA AA). Remember that the frame is 11 bytes no 9.
AUDI_ENG (7th July 2014)
I used this code :
Foo::readyRead(){
static const int expectedPacketSize = 11; // your 11 bytes
if (port->bytesAvailable() < expectedPacketSize)
return; // do nothing
QByteArray data = port->read(expectedPacketSize); // do domething processing of your packet
}
so I have 11 bytes in the data buffer but how I can detect the start of the packet , in the same time when I read data ?
I can't analyse my packet for detecting data after reading, and if I collect all data in the same buffer, its very heavy for application
You should declar variable dataBuffer in class Foo and then do something like this :
Qt Code:
Foo::readyRead(){ int frameLen = 11; dataBuffer.append(port->readAll()); if( dataBuffer.size() < frameLen ) return;// data to short int indexOfFrame = dataBuffer.indexOf("\xAA\xAA"); if( indexOfFrame < 0) return;// frame marker not found QByteArray oneFrame = dataBuffer.mid(indexOfFrame,frameLen);// we have one frame, do something with this dataBuffer = dataBuffer.right(indexOfFrame,frameLen);//remove processed data from the buffer }To copy to clipboard, switch view to plain text mode
AUDI_ENG (7th July 2014)
I'm writing this code before your code, because with yours I'm losing data
Qt Code:
if(Port->bytesAvailable() < lenFram){ return; } dataBuffer.append(port->read(lenFram)); // I change your line to thisTo copy to clipboard, switch view to plain text mode
When I'm adding this before your code I have :
bytesreceives = "0aaaaa0c1209080a090908"
index TAG = 1
nb data read = 10 bytes = "aaaa0c1209080a090908"
bytesreceives = "0aaaaa0d0a09090a090909"
index TAG = 1
nb data read = 10 bytes = "aaaa0d0a09090a090909"
bytesreceives = "0aaaaa0e0e09080a080909"
index TAG = 1
nb data read = 10 bytes = "aaaa0e0e09080a080909"
_________________________________________________
with your code (with readAll() ) : I have this :
nb data read = 11 bytes = "aaaa070a09080a0908090a"
nb data read = 11 bytes = "aaaa080a08090a0909080a"
nb data read = 11 bytes = "aaaa0a1709090a0809090a"
nb data read = 11 bytes = "08090aaaaa0d0a08090a09" --> AA is not in the beginning
nb data read = 11 bytes = "09080aaaaa0f1709090a08" --> AA is not in the beginning
nb data read = 11 bytes = "09080aaaaa020a08090a09" --> AA is not in the beginning
nb data read = 11 bytes = "aaaa031209090a0908090a"
nb data read = 11 bytes = "aaaa041209090a0908090a" --> ID = 4
nb data read = 11 bytes = "08090aaaaa070e08090a09" --> AA is not in the beginning (ID = 7) ---> I miss data with ID = 5 and 6
nb data read = 11 bytes = "08090aaaaa091209080a09" --> AA is not in the beginning
nb data read = 11 bytes = "09090aaaaa0b0a09080a09" --> AA is not in the beginning
I have still the problem with the beginning of frames, sometimes, it is AAAA and sometimes other bytes of frames
Last edited by AUDI_ENG; 7th July 2014 at 14:23.
Between lines 8 and 9 of my code should be :Line 10 sholud be :Qt Code:
if( (dataBuffer.size() - indexOfFrame) < frameLen ) return; // frame is not completeTo copy to clipboard, switch view to plain text modeI have not tested my code. This is just an example of how something should look.Qt Code:
dataBuffer = dataBuffer.right(indexOfFrame+frameLen);//remove processed data from the bufferTo copy to clipboard, switch view to plain text mode
Last edited by AUDI_ENG; 8th July 2014 at 09:51.
The amount of data in the buffer depends on how quickly you process them. Remember that the serial port operates completely asynchronously and it can happen that during the processing of one frame to the computer reaches a few more.
AUDI_ENG (8th July 2014)
Thank you so much for your replies, for your patience, for your ideas and your important information
I like your motivation to help all beginners
Thank you again
Last edited by AUDI_ENG; 8th July 2014 at 10:09.
Bookmarks