Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > UART Input String Parsing

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



UART Input String Parsing
Moderator:
RKRM

Post Reply
Follow this topic



UART Input String Parsing

jram posted on 06 Apr 2012 11:23 PM PST
Top Contributor
31 Forum Posts

I want to be able to interupt my main code when data is recieved via UART. The recieved data string will contain ASCII parameters and parameter values that will need to be parsed and saved into a structure. For example: "rate,10000,gain,4" would be saved as data.rate=10000 and data.gain=4. The incoming string will be less than 256 bytes.

I was able to setup an ISR to set a flag and interupt the main code using the "RX - On Byte Received" interupt on the UART. I however don't know how to handle the incoming string. Can someone help me with getting the data from UART to a structure?





Re: UART Input String Parsing

Gautam Das posted on 06 Apr 2012 12:38 AM PST
Cypress Employee
742 Forum Posts

Hi jram,

 

This project can be implemented in the following manner:

1) Declare a structure with the elements whose values will be received via UART.

2) The UART Rx interrupt must be used to get the ASCII characters. There should be two arrays, one to store the element name like "rate", "gain" etc and another to store the corresponding numerical values in ASCII such as "10000", "4", etc

3) If the ASCII character received is not a numerical number (between 0x30 and 0x39), then store it in the NAME array. If the received ASCII character represents a number, that is, between 0x30 and 0x39, then store it in the NUMBER array.

4) Once a pair of NAME and NUMBER array is received, then the number received is mapped to the element of the structure to which it belongs.

For this two operations is to be done in the main. The NAME array should be checked if it is equal to "rate" or "gain". When one of them matches, the corresponding number should be assigned to the element of the structure.

The received numerals in ASCII format should be converted to a number. Example, "10000" will be stored as {0x31, 0x30, 0x30, 0x30, 0x30}. A function should be written to convert it to hexadecimal format. This number should be assigned to the element of the structure, like example.rate = 10000, example.gain = 4.

 

How many elements are there in the structure? And what is the baud rate of the UART used?

All the conversion and assignment operations should be done in the main before the next pair of data is received.



Re: UART Input String Parsing

Bob Marlowe posted on 06 Apr 2012 01:32 AM PST
Top Contributor
1768 Forum Posts

Problems like yours are traditionally solved with a program named "Parser". It reads an input-stream and divides ti into "Tokens". To do that there must be a (or more) "Delimiter" between the tokens for instance a comma, Cr, and so on.

Data received from UART are put into a buffer until a delimiter is found and it is signaled (setting a flag) that a token has arrived.

The controlling program reads and acts upon the token and resets the flag (good to double-buffer tokens so that they do not get overwritten)

The controlling program in your case sounds like a simple state-machine (waiting for name, process name, waiting for Value, process value,...)

This approach gives the most flexibiliy and can easily be re-used in other programs with similar problems, so it is worth the effort to implement it.

Bob



Re: UART Input String Parsing

H L posted on 06 Apr 2012 03:54 AM PST
Top Contributor
679 Forum Posts

 If your message is of fixed format ie, the first 5 byte is rate, the next is 1 byte of gain. Then you don't need to send the 'rate' and 'gain' string. You just read the value and convert it to a long of a byte and copy it to you structure. Unless your incomming message can be of any order.

 



Re: UART Input String Parsing

jram posted on 06 Apr 2012 11:45 AM PST
Top Contributor
31 Forum Posts

My biggest concern is how do I handle the incoming data stream? I understand that I need to parse the string and put it into a structure. If this was simply a C-code program I could figure it out, but the UART is causing my primary confusion. How do I properly get the data from UART into a string? I also know how to read bytes/char from the UART, but I am uncertain how to setup the memory array and transfer the data from UART to the memory. Should the memory be in Flash or SRAM, etc?



Re: UART Input String Parsing

Bob Marlowe posted on 06 Apr 2012 01:14 PM PST
Top Contributor
1768 Forum Posts

Oh, that's easy! I would suggest to put the incoming data into SRAM, preferrably into an array of chars

char CharBuffer[20];

uint8 ii = 0;

if(UART_ReadRxStatus() & UART_RX_STS_FIFO_NOTEMPTY) CharBuffer[ii++] = UART_ReadRxData();

 

Have a look at the APIs in the datasheet.

Flash-memory is something that must be "programmed" and it will keep its contents even when the power to the chip is turned off.

If you are getting a bit more experienced I would suggest you to write a "Circular Buffer" and feed it interrupt driven with the incoming UART-Data.

 

Happy coding

Bob

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS 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. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

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 and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers 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.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20