C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-13-2009, 01:55 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 4
Data structure for storing serial port data in firmware

Hi all,

I am sending data from a linux application through serial port to an embedded device.

In the current implementation a byte circular buffer is used in the firmware. (Nothing but an array with a read and write pointer)
As the bytes come in, it is written to the circular bufffer.

Now the PC application appears to be sending the data too fast for the firmware to handle. Bytes are missed resulting in the firmware returning WRONG_INPUT too mant times.

I think baud rate (115200) is not the issue. A more efficient data structure at the firmware side might help. Any suggestions on choice of data structure?

Thanks
james457 is offline   Reply With Quote
Old 06-13-2009, 02:09 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,680
<help withdrawn due to abuse from OP>
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 06-13-2009, 02:57 AM   #3
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,442
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.

Last edited by itCbitC; 06-13-2009 at 03:01 AM.
itCbitC is offline   Reply With Quote
Old 06-13-2009, 07:14 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 06-15-2009, 09:28 AM   #5
Registered User
 
Join Date: Jun 2009
Posts: 4
Quote:
Originally Posted by matsp View Post
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
james457 is offline   Reply With Quote
Reply

Tags
c programming, data structure, linux, serial port

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Count number of 1 in a binary data trame through the serial port DB9 minhdung_hoang C Programming 5 01-28-2009 04:34 PM
HELP with storing serial port data into txt file inmaterichard C Programming 2 04-02-2008 02:20 AM
brace-enclosed error jdc18 C++ Programming 53 05-03-2007 05:49 PM
HUGE fps jump DavidP Game Programming 23 07-01-2004 10:36 AM
Serial Communications in C ExDigit Windows Programming 7 01-09-2002 10:52 AM


All times are GMT -6. The time now is 08:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22