qfedu-c-level/day12/homework/h9.c

119 lines
4.2 KiB
C
Raw Normal View History

2023-07-22 10:37:20 +08:00
/*
GPS相关知识
$GPRMC协议格式
  $GPRMC, 024813.640, A, 3158.4608, N, 11848.3737, E, 10.05, 324.27, 150706, , , A * 50
  0$GPRMCIDRecommended Minimum Specific GPS / TRANSIT DataRMC
  1UTC时间hhmmss.sss格式(UTC时间是世界统一时间UTC + 802 : 48 : 1310 : 48 : 13)
  2A = V =
  3ddmm.mmmm0
  4NS
  5dddmm.mmmm0
  6EW西
  7Knots
  8
  9UTC日期DDMMYY格式
  10000 - 1800
  11E = W = 西
  16
strtok切割sscanf或者strncpyatoiatof
*/
#include <stdio.h>
#include <string.h>
typedef struct
{
char time[10]; // UTC时间格式为hhmmss.sss
char status; // 状态A表示定位V表示未定位
double latitude; // 纬度格式为ddmm.mmmm
char latitude_dir; // 纬度半球N表示北半球S表示南半球
double longitude; // 经度格式为dddmm.mmmm
char longitude_dir; // 经度半球E表示东经W表示西经
double speed; // 速度,以节为单位
double course; // 方位角,以度为单位
char date[7]; // UTC日期格式为DDMMYY
double declination; // 磁偏角,以度为单位
char declination_dir; // 磁偏角方向E表示东偏W表示西偏
} gprmc_t;
int parse_gprmc(const char *str, gprmc_t *gprmc)
{
char *token;
int i = 0;
token = strtok((char *)str, ",");
while (token != NULL)
{
switch (i)
{
case 0:
if (strcmp(token, "$GPRMC") != 0)
{
return -1;
}
break;
case 1:
sscanf(token, "%2s", gprmc->time);
sscanf(token + 2, "%2lf%lf", &gprmc->time[2], &gprmc->time[4]);
sscanf(token + 4, "%lf", &gprmc->time[6]);
break;
case 2:
gprmc->status = *token;
break;
case 3:
sscanf(token, "%lf", &gprmc->latitude);
sscanf(token + 2, "%lf", &gprmc->latitude);
break;
case 4:
gprmc->latitude_dir = *token;
break;
case 5:
sscanf(token, "%lf", &gprmc->longitude);
sscanf(token + 3, "%lf", &gprmc->longitude);
break;
case 6:
gprmc->longitude_dir = *token;
break;
case 7:
sscanf(token, "%lf", &gprmc->speed);
break;
case 8:
sscanf(token, "%lf", &gprmc->course);
break;
case 9:
sscanf(token, "%2s", gprmc->date);
sscanf(token + 2, "%2lf%lf", &gprmc->date[2], &gprmc->date[4]);
sscanf(token + 4, "%lf", &gprmc->date[6]);
break;
case 10:
sscanf(token, "%lf", &gprmc->declination);
break;
case 11:
gprmc->declination_dir = *token;
break;
default:
break;
}
token = strtok(NULL, ",");
i++;
}
return 0;
}
int main()
{
const char *str = "$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50";
gprmc_t gprmc;
if (parse_gprmc(str, &gprmc) == 0)
{
printf("UTC时间%s\n", gprmc.time);
printf("状态:%c\n", gprmc.status);
printf("纬度:%lf %c\n", gprmc.latitude, gprmc.latitude_dir);
printf("经度:%lf %c\n", gprmc.longitude, gprmc.longitude_dir);
printf("速度:%lf\n", gprmc.speed);
printf("方位角:%lf\n", gprmc.course);
printf("UTC日期%s\n", gprmc.date);
printf("磁偏角:%lf %c\n", gprmc.declination, gprmc.declination_dir);
}
return 0;
}