Data structure for storing serial port data in firmware

This is a discussion on Data structure for storing serial port data in firmware within the C Programming forums, part of the General Programming Boards category; Hi all,
I am sending data from a linux application through serial port to an embedded device.
In the current ...

Implement flow control either in hardware using the CTS/RTS signals (as noted) or in software using the XON/XOFF characters. Check if the embedded device supports hardware flow control as it isn't supported by all devices / OSes.

If you are sending data in bursts, you may also consider a larger circular buffer.

Also bear in mind that circular buffers where you use "pos = (pos + 1) % SIZE", if you pick a size that is 2 ^ n, you can use "pos = (pos + 1) & (SIZE-1)", which will be a much faster operation than the divide that the compiler MAY decide to generate for the % operator. The other option is to use an if-statement to modify the position. Also highly likely to be faster than the modulo operator (%). Since this is done twice for every character received, it may help to improve the peroformance.

The other aspect is of course what you are doing to extract from the circular buffer - for example, are you allocating memory when extracting? Are you doing some lengthy work to calculate something, or search for something with the received data - does that prevent incoming data from being accepted. Could you change that?

If you are sending data in bursts, you may also consider a larger circular buffer.

Also bear in mind that circular buffers where you use "pos = (pos + 1) % SIZE", if you pick a size that is 2 ^ n, you can use "pos = (pos + 1) & (SIZE-1)", which will be a much faster operation than the divide that the compiler MAY decide to generate for the % operator. The other option is to use an if-statement to modify the position. Also highly likely to be faster than the modulo operator (%). Since this is done twice for every character received, it may help to improve the peroformance.

The other aspect is of course what you are doing to extract from the circular buffer - for example, are you allocating memory when extracting? Are you doing some lengthy work to calculate something, or search for something with the received data - does that prevent incoming data from being accepted. Could you change that?

--
Mats

In the interrupt routine(background) data is written to the circular buffer. In the foreground I do process the received data right in the circular buffer using a state machine. Data is only copied over if the received data is in the correct format. The modulus is agood point, I will change that. Thanks