采用studio处理GPS数据,使用的是sscanf函数,同样的代码VS2019中可以正常解析,使用studio解析参数不完整。
下面是VS2019写的测试代码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define BUF_SIZE 20
typedef struct gps_info
{
char utc_time[BUF_SIZE];
char status;
float latitude_value;
char latitude;
float longtitude_value;
char longtitude;
float speed;
float azimuth_angle;
char utc_data[BUF_SIZE];
}GPS_INFO;
/* parse GPS information /
int main(void)
{
GPS_INFO gps_info = { 0 };
float temp;
int a;
const char s = "$GNRMC,061424.000,A,3604.352623,N,12021.319553,E,0.00,229.83,170820,,,D*74";
a = sscanf(s, "$GNRMC,%[^,],%c,%f,%c,%f,%c,%f,%f,%[^,]", gps_info.utc_time, &gps_info.status, &gps_info.latitude_value, &gps_info.latitude, &gps_info.longtitude_value, &gps_info.longtitude, &gps_info.speed, &gps_info.azimuth_angle, gps_info.utc_data);
printf("a = %d\n", a);
int len = 0;
char buf1[20];
printf("find GPS...\n");
printf("status %c\n", gps_info.status);
gps_info.latitude_value /= 100;
temp = gps_info.latitude_value - (unsigned long)gps_info.latitude_value;
temp = temp * 100 / 60;
temp = (unsigned long)gps_info.latitude_value + temp;
len = sprintf(buf1, "%.6f", temp);
printf("latitude %c:%s, data len %d\n", gps_info.latitude, buf1, len);
gps_info.longtitude_value /= 100;
temp = gps_info.longtitude_value - (unsigned long)gps_info.longtitude_value;
temp = temp * 100 / 60;
temp = (unsigned long)gps_info.longtitude_value + temp;
len = sprintf(buf1, "%.6f", temp);
printf("longtitude %c:%s, data len %d\n", gps_info.longtitude, buf1, len);
system("pause");
return 0;
}
执行结果位:
a = 9
find GPS...
status A
latitude N:36.072544, data len 9
longtitude E:120.355324, data len 10
请按任意键继续. . .
下面的代码是studio上的测试代码
rt_uint32_t rmc_data_parse(const char data)
{
GPS_INFO gps_info = {0};
float temp;
int a;
const char s = "$GNRMC,061424.000,A,3604.352623,N,12021.319553,E,0.00,229.83,170820,,,D74";
a = sscanf(s, "$GNRMC,%[^,],%c,%f,%c,%f,%c,%f,%f,%[^,]", gps_info.utc_time, &gps_info.status, &gps_info.latitude_value, &gps_info.latitude, &gps_info.longtitude_value, &gps_info.longtitude, &gps_info.speed, &gps_info.azimuth_angle, gps_info.utc_data);
LOG_D("a = %d", a);
if (gps_info.status == 'V')
{
}
else
{
int len = 0;
char buf1[20];
LOG_D("find GPS...");
LOG_D("status %c", gps_info.status);
gps_info.latitude_value /= 100;
temp = gps_info.latitude_value - (unsigned long)gps_info.latitude_value;
temp = temp100/60;
temp = (unsigned long)gps_info.latitude_value + temp;
len = sprintf(buf1, "%.6f", temp);
LOG_D("latitude %c:%s, data len %d", gps_info.latitude, buf1, len);
gps_info.longtitude_value /= 100;
temp = gps_info.longtitude_value - (unsigned long)gps_info.longtitude_value;
temp = temp*100/60;
temp = (unsigned long)gps_info.longtitude_value + temp;
len = sprintf(buf1, "%.6f", temp);
LOG_D("longtitude %c:%s, data len %d", gps_info.longtitude, buf1, len);
}
return 0;
}
执行结果为:
a = 2
find GPS...
status A
latitude :, data len -559038737
longtitude :, data len -559038737
看起来只解析了两个参数,且sprintf函数处理的结果也不正常。请问需要其他额外的设置嘛?