PDA

View Full Version : QByteArray datas arrive splitted



oazive
2nd May 2017, 15:29
Hi everyone,
i have a probleme while recieving datas in a QByteArray when i use readAll from QSerialPort, i recieve datas like that when i use qDebug << datas.size() << datas;
(datas is a QByteArray)
512 "11010111111101000001010000010100000101000001010000 01010000010100000101000001010000010100000101000001 01000001010000010100000101000001010000010100000101 00000101000001010000010100000101000001010000010100 00010100000101000001010000010100000101000001010000 01010000010100000101000001010000010100000101000001 01000001010000010100000101000001010000010100000101 00000101000001010000010100000101000001010000010100 00010100000101000001010000010100000101000001010000 01010000010100000101000001010000010100000101000001 010000010100"

6 "0001\r\n"

after recieving the frame i extract the different information, but it doesn't come entirely so the data extraction is not correct, it works only if the frame is less than 512 bits

d_stranz
2nd May 2017, 23:25
So is your frame always terminated with CRLF? If so, then you simply keep appending each new QByteArray onto a second, "frame" byte array until you receive a QByteArray of data from readAll that is correctly terminated. At that point, you have a complete frame in the "frame" byte array and can process it.

oazive
3rd May 2017, 08:52
I tried to add a CRLF and i use the indexOf function in a while loop, so it reads data until the function returns anything else than -1 (because the length of the frame is defined in the frame itself, and i cannot know it by another way) . the problem is that the function always return -1 .

Edit :
I found how to use inexOf for the CRLF but it finds it in the "second part" of the frame... it changes anything, maybe I am doing it wrong.

Lesiok
3rd May 2017, 13:39
This is normal symptom. Serial port is just a pipe that knows nothing about the data structure. You have to concatene data after every readAll() with previous and then split them into protocol-compliant records.
The problem was explained many times on the forum.