Buffer and parse management for data from serial
-
I am reading data from serial port in qt.
1 package size is a minimum of 7 and a maximum of 16.
And these incoming data can come not only as 1 packet, but also 2-3 packets on top of each other.
so i decided to use buffer in my code.
I'm reading all the data until the 512-byte buffer is full. then I find the package head in a for loop to parse this buffer and start the processes. but in the meantime, my software is getting stuck because the data is still coming in.What are your suggestions for such a problem?
-
I don't think that parsing 512bytes take so long that it will stuck your application. Fix your parsing :)
-
@Christian-Ehrlicher When the serial is ReadyRead, I take the data from the running function and transfer it to the buffer. Again when the buffer is full I parse the data in the same function. Is that why I'm in trouble?
-
Hi, the buffer should be greater than a serial packet, depends on the protocol you are implementing. When something is come from the serial port you should search in you buffer if the byte that rapresent the end of the packet is come. It should be 0x03, \n, depend on your protocol. If you have, you can check you buffer to extract the packet.
-
@KARMA
Not sure why you have selected a 512-byte buffer, or indeed any particular limited size, since you can just e.g. allow aQByteArray
to grow with append. Nothing should "get stuck" in any case.If you do your parsing --- and more particularly whatever you do with the result of the parsing --- from the
readyRead
signal's slot then your code would "get stuck" if your parsing gets stuck or what you do with it takes forever.readyRead
s should not "lose" data because you don't read that data fro a while. But you might pick up the next 7/16 bytes from the buffer and raise your own "complete packet received" signal which the outside world connects to.