Currently I have a GPS connected to my Arduino chip which outputs a few lines every second. I want to extract specific info from certain lines.
$ÇÐÇÇÁ,175341.458,3355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,,0000*57
(Take note of the characters)
If I read this line into a char[]
, is it possible to extract 3355.7870
and 01852.4251
from it? (Well obviously it is, but how?)
Would I need to count the commas and then after comma 2 start putting the number together and stop at comma 3 and do the same for second number or is there another way? A way to split up the array?
The other problem with this is identifying this line because of the strange characters at it's beginning - how do I check them, because their not normal and behaves strangely?
The data I want is always in form xxxx.xxxx
and yyyyy.yyyy
and are unique in that form, meaning I could maybe search trough all the data not caring about which line it's on and extract that data. Almost like a preg-match, but I have no idea how to do that with a char[]
.
Any tips or ideas?
You can tokenize (split) the string on the comma using strtok , and then parse the numbers using sscanf .
Edit: C example:
void main() {
char * input = "$ÇÐÇÇÁ,175341.458,3355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,,0000*57";
char * garbage = strtok(input, ",");
char * firstNumber = strtok(NULL, ",");
char * secondNumber = strtok(NULL, ",");
double firstDouble;
sscanf(firstNumber, "%lf", &firstDouble);
printf("%f\n", firstDouble);
}
If you have strange characters at the beginning of the string, then you should start parsing it from the end:
char* input = get_input_from_gps();
// lets assume you dont need any error checking
int comma_pos = input.strrchr(',');
char* token_to_the_right = input + comma_pos;
input[comma_pos] = '\0';
// next strrchr will check from the end of the part to the left of extracted token
// next token will be delimited by \0, so you can safely run sscanf on it
// to extract actual number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.