• Join
  • Sign In with my.TI Login
Texas Instruments
  • Products
  • Applications
  • Tools & Software
  • Support & Community
  • Sample & Buy
  • About TI
Sample & Purchase Cart Sample & Purchase Cart
  • Search
  • Advanced
TI E2E™ Community
  • Support Forums
  • Blogs
  • Groups
  • Videos
  • 简体中文
  • More ...
TI Home » TI E2E Community » Support Forums » Microcontrollers » Stellaris® ARM® Microcontrollers » Stellaris® ARM® LM3S Microcontrollers Forum » Reading UART data stream
Share
Stellaris® ARM® Microcontrollers
  • Forum
Options
  • Subscribe via RSS
Helpful Stellaris® LM4F Series Links
  • LM4F Series
  • Stellaris PinMux Utility
  • Stellaris® LM4F120 LaunchPad
  • LM4F MCU Applications
  • LM4F MCU Video
  • ARM Cortex-M4F Whitepaper
  • Stellaris MCU Brochure
  • LM4F232 Eval Kit
  • Reading UART data stream

    Reading UART data stream

    This question is not answered
    Ryan Lush67904
    Posted by Ryan Lush67904
    on May 09 2012 08:55 AM
    Intellectual460 points

    I have a device which spits out an ASCII stream of comma separated values which starts with a ! and ends with a # at 115200 baud. I'm trying to read the values with an EK-LM3S9B96 but I can't figure out the best way to do it. Currently this is what I am doing

        TMainState mainState;
        int iBuff = 0;
        char commandBuff[32];
        char delim[] = {","};

        memset(commandBuff, 0, sizeof(commandBuff));
        mainState = STATE_IDLE;
        while(1){
            switch(mainState)
            {
                case STATE_IDLE:
                    if(UARTCharsAvail(UART1_BASE)){
                        commandBuff[iBuff] = UARTCharGetNonBlocking(UART1_BASE);
                        if(commandBuff[iBuff] == '!'){
                            mainState = STATE_CMD_RECEIVING;
                            break;
                        }
                        break;
                    }
                    break;
                case STATE_CMD_RECEIVING:
                    if(UARTCharsAvail(UART1_BASE)){
                        commandBuff[iBuff] = UARTCharGetNonBlocking(UART1_BASE);
                        if(commandBuff[iBuff] == '#'){
                            mainState = STATE_CMD_RECEIVED;
                            commandBuff[iBuff] = 0;
                            break;
                        }
                        if(commandBuff[iBuff] == '!'){  // We missed a stop bit
                            mainState = STATE_IDLE;
                            iBuff = 0;
                            break;
                        }
                        if(iBuff++ >= 32) iBuff = 0;
                    }
                    break;
                case STATE_CMD_RECEIVED:    
                    mainState = STATE_IDLE;
                    memset(commandBuff, 0, sizeof(commandBuff));
                    iBuff = 0;
                    break;
            }
        }

    Even as I was writing it, it didn't feel right. The problem is if I miss a character everything is out of sync and I seem to be missing quite a few. That part doesn't make sense. I'm not doing anything else and I enabled the FIFO's on the UART. I'm only sending about 25 bytes every 100ms. Any suggestions on the missing bytes?

    I could really use some suggestions on a better way to handle ASCII data streams in an efficient manor. This problem seems to come up quite often and this is always my solution. Even when I try to brainstorm and think I have come up with something new, it turns out to be this and I remember it's the exact same thing I did last time.

    UART ASCII DATA STREAM PARSE
    Report Abuse
    • Reply
    You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    All Replies
    • Craig Giglio94760
      Posted by Craig Giglio94760
      on May 09 2012 09:33 AM
      Intellectual860 points

      Hello Ryan,

      Have you seen the command line example in the StellarisWare directories?  If you go to the install location and look in Stellarisware\utils there is a cmd.c and cmd.h example that parses a command string.  Will that help you?

      Regards,

      Craig

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Ryan Lush67904
      Posted by Ryan Lush67904
      on May 09 2012 10:33 AM
      Intellectual460 points

      It helps a little bit but it's not the same in that it doesn't need to worry about pulling a string out of larger buffer of strings. My secondary device sends

      "!ANG:,123.45,678.90,123.45#" (Euler angles)

      every 100ms so it's not like at any point I know that the next character is the first character in the command. I do if I received the stop byte but sometimes I miss it or some of the other bytes which throws everything else off. I have to wait for the '!' before I know it's the start of the string where as the cmdline.c just assumes it is being passed a string which starts with the start byte. The code is helpful although I can't really use it until I find a reliable way to pull the right 27 characters out of a stream of data.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • Ryan Lush67904
      Posted by Ryan Lush67904
      on May 09 2012 13:15 PM
      Intellectual460 points

      uartstdio.c looks a little more promising.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    • marc_rir
      Posted by marc_rir
      on May 09 2012 14:21 PM
      Intellectual885 points

      I would use a ring buffer big enough for one command line and let the UART ‘receive’ interrupt store the received bytes into this buffer, this way you should never miss any incoming bytes. 

      Then from in the main function, just poll the ring buffer byte count.

      Another thing I would add is a CRC check, gives a way to check if the data is valid.

      Report Abuse
      • Reply
      You have posted to a forum that requires a moderator to approve posts before they are publicly available.
    TI E2E™ Community
    • Support Forums
    • Blogs
    • Videos
    • Groups
    • Site Support & Feedback
    • Settings
    TI E2E™ Community Groups
    • TI University Program
    • Make the Switch
    • Microcontroller Projects
    • Motor Drive & Control
    Other Communities
    • Deyisupport
    • Designsomething.org
    • beagleboard.org
    • TI on Element 14
    • TI on TechXchangeSM
    Other Technical & Support Resources
    • WEBENCH® Design Center
    • Product Information Centers
    • Technical Documents
    • TI Design Network
    • TI Technical Articles
    • TI Training

    All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.

    Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms of Use of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms of Use of this site. TI, its suppliers and providers of content reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

    Follow Us Texas Instruments on Facebook Texas Instruments on Twitter Texas Instruments on LinkedIn Texas Instruments on Google+
    TI Worldwide | Contact Us | my.TI Login | Site Map | Corporate Citizenship | mobile m.ti.com (Mobile Version)

    TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and
    embedded processors, along with software, tools and the industry’s largest sales/support staff.

    © Copyright 1995-2013 Texas Instruments Incorporated. All rights reserved.
    Trademarks | Privacy Policy | Terms of Use