#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
int main()
{
char *lptr = NULL;
char *tptr = NULL;
char tmp[30] = {0};
char *GBS_USART0_receive_buf = "0,29.489428,106.639084,550,2020/6/17,9:34:16";
double D_latitude = 0.0;
double D_longitude = 0.0;
uint64_t latitude = 0;
uint64_t longitude = 0;
uint8_t size = 0;
uint8_t year = 0;
uint8_t month = 0;
uint8_t day = 0;
uint8_t hour = 0;
uint8_t minute = 0;
uint8_t second = 0;
uint8_t gps_time_arr[8] = {0x00, 0x00};
uint32_t gps_time = 0;
lptr=strstr((const char*)GBS_USART0_receive_buf,",");
if(lptr==NULL) return 0;
lptr++;
tptr = strstr((const char*)lptr,",");
if(tptr==NULL) return 0;
size = tptr - lptr;
strncpy(tmp, lptr, size);
D_latitude = atof(tmp);
printf("D_latitude = %lf\n", D_latitude);
lptr = tptr + 1;
tptr = strstr((const char*)lptr,",");
if(tptr==NULL) return 0;
size = tptr - lptr;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, size);
D_longitude = atof(tmp);
printf("D_longitude = %lf\n", D_longitude);
tptr = lptr + 1;
lptr=strstr((const char*)tptr,",20");
lptr = lptr + 3;
tptr = strstr((const char*)lptr,"/");
if(tptr==NULL) return 0;
size = tptr - lptr;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, size);
year = atoi(tmp);
printf("year = %d\n", year);
lptr = tptr+1;
tptr = strstr((const char*)lptr,"/");
if(tptr==NULL) return 0;
size = tptr - lptr;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, size);
month = atoi(tmp);
printf("month = %d\n", month);
lptr = tptr + 1;
tptr = strstr((const char*)lptr,",");
if(tptr==NULL) return 0;
size = tptr - lptr;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, size);
day = atoi(tmp);
printf("day = %d\n", day);
lptr = tptr + 1;
tptr = strstr((const char*)lptr,":");
if(tptr==NULL) return 0;
size = tptr - lptr;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, size);
hour = atoi(tmp);
printf("hour = %d\n", hour);
lptr = tptr + 1;
tptr = strstr((const char*)lptr,":");
if(tptr==NULL) return 0;
size = tptr - lptr;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, size);
minute = atoi(tmp);
printf("minute = %d\n", minute);
lptr = tptr + 1;
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, lptr, 2);
second = atoi(tmp);
printf("second = %d\n", second);
latitude = D_latitude * 10000000;
longitude = D_longitude * 10000000;
printf("latitude = %ld\n", latitude);
printf("longitude = %ld\n", longitude);
return 0;
}
程序运行结果:
D_latitude = 29.489428
D_longitude = 106.639084
year = 20
month = 6
day = 17
hour = 9
minute = 34
second = 16
latitude = 294894280
longitude = 1066390840