View previous topic :: View next topic |
Author |
Message |
Guest
|
Parsing a string |
Posted: Thu Jun 21, 2007 12:42 pm |
|
|
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
|
|
Posted: Thu Jun 21, 2007 12:46 pm |
|
|
Quote: | Is there existing code out there? | Yes. Use the search function of this forum and you will find several examples. |
|
|
Guest
|
|
Posted: Thu Jun 21, 2007 6:28 pm |
|
|
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
|
|
Posted: Thu Jun 21, 2007 6:50 pm |
|
|
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"); |
|
|
|
|