Parsing of strings

This is a discussion on Parsing of strings within the C Programming forums, part of the General Programming Boards category; Hi I am trying to parse the following string: Code: +CMT: "+44phonenumber",,"11/02/16,00:58:16+00" MessageHere The first line and second line are ...

  1. #1
    nsp
    nsp is offline
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Parsing of strings

    Hi

    I am trying to parse the following string:

    Code:
    +CMT: "+44phonenumber",,"11/02/16,00:58:16+00"
    MessageHere
    The first line and second line are separated by a \r and \n.

    Now, each of these messages will be proceeded by either OK or ERROR, as this is from a GSM module.

    Could anyone help me parse out the whole thing and check?

    I've got the following so far:

    Code:
    for(int i=0;i<78;i++)
    			{
    				data[i] = getch();
    				
    				if((data[i] == 'O' || data[i] == 'K') && flag <2)
    				{
    					flag++;
    				}
    				if(flag==2)
    				{
    					break;
    				}
    				
    			
    			}
    			
    			if(strstr(data,"stat")!=NULL)
    			{
    				__delay_ms(1000);
    				printf("AT+CMGF=1\r\n");
    				__delay_ms(1000);
    				printf("AT+CMGS=PHONENUMBERHERE\r\n");
    				__delay_ms(1000);
    				printf("I'm fine!\r\n");
    				__delay_ms(1000);			
    			    printf("\x001A");
    				__delay_ms(15000);
    			
    			}
    This works in practice when everything is on one line, but when OK and the message I'm trying to parse are on separate lines, the check against "stat" being in the string fails.

    Any help with this would be greatly appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    The first thing you need to do is actually tokenization. If GSM message is the only thing you are going to process, your algorithm could even work, but you would better get some open source tokenizer and do the rest on your own.

    Don't use getch(), use getchar() and scanf().
    Last edited by kmdv; 02-16-2011 at 07:26 AM.
    I never put signature, but I decided to make an exception.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could probably use strtok() and/or sscanf() here.

    The way you check for OK is not so good since "Only Kevin", etc, will set it off. Something like:
    Code:
    if (data[i] == 'O' && data[i+1] == 'K') {
    is better. The second condition is only checked if the first one succeeds, so presuming this is a null terminated string you do not have to worry about going out of bounds.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    strtok() can be indeed used, but it omits one or more characters. This means that if you expect something between these two commas, strtok() will fail when there is nothing between them.
    You could also use regular expressions.
    I never put signature, but I decided to make an exception.

  5. #5
    nsp
    nsp is offline
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Thanks guys.

    As I'm using Hitech C compiler (for Microchip PIC's), the getch() function is just reading from the UART buffer.

    I'll give the suggestions a go later on.


    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing strings from an input file
    By bhdavis1978 in forum C Programming
    Replies: 2
    Last Post: 06-01-2010, 01:32 PM
  2. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 09:26 AM
  3. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  4. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 11:11 AM
  5. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 11:18 AM

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