QByteArray datas arrive splitted
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 "1101011111110100000101000001010000010100000101000 00101000001010000010100000101000001010000010100000 10100000101000001010000010100000101000001010000010 10000010100000101000001010000010100000101000001010 00001010000010100000101000001010000010100000101000 00101000001010000010100000101000001010000010100000 10100000101000001010000010100000101000001010000010 10000010100000101000001010000010100000101000001010 00001010000010100000101000001010000010100000101000 00101000001010000010100000101000001010000010100000 1010000010100"
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
Re: QByteArray datas arrive splitted
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.
Re: QByteArray datas arrive splitted
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.
Re: QByteArray datas arrive splitted
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.