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 ...

  1. #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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    <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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    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 04:01 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-28-2009, 04:34 PM
  2. HELP with storing serial port data into txt file
    By inmaterichard in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 03:20 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

Tags for this Thread


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