CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Parsing a string

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Parsing a string
PostPosted: Thu Jun 21, 2007 12:42 pm     Reply with quote

What I want todo is receive a string by rs232, and parse it but Im not sure how todo this with CCS (easy todo is PHP, but thats different).

Strings like...
DA=1500<CR>
DB=40<CR>

So I'm looking to read what is before and after the =
The parameter after the = is always a int and stored, where as DA and DB would be to refer to a function. Is there existing code out there?
Guest








PostPosted: Thu Jun 21, 2007 12:46 pm     Reply with quote

Quote:
Is there existing code out there?
Yes. Use the search function of this forum and you will find several examples.
Guest








PostPosted: Thu Jun 21, 2007 6:28 pm     Reply with quote

Ok I have the code part done but I cannot get the string before the = to compare properly. Stuff after the = is a number so atol() works fine.

Code:

char data_string[20], compare_string[20];
   for(i=0; i<20; i++) data_string[i] = 0x00; //blank it
   for(i=0; i<20; i++) {
        tmp_char = usb_cdc_getc();
        if (tmp_char == '\n') break;
        if (tmp_char == '\r') break;
        if (tmp_char == '=') { foundsep=1; break; } //found seperator
        data_string[i] = tmp_char;
   }
strcpy(compare_string,"set");
if (strcmp(data_string,compare_string)) printf("strings match");
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jun 21, 2007 6:50 pm     Reply with quote

Strcmp returns:
A zero value when both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

Change
Code:
if (strcmp(data_string,compare_string)) printf("strings match");
to
Code:
if (strcmp(data_string,compare_string) == 0) printf("strings match");
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group