我正在使用结构来处理 NMEA 消息,但我不知道是什么,在处理它时出了点问题。所以,我有 NMEA_parse.h:
/* GPRMC */
#define TIME 2U
#define LAT 4U
#define LON 6U
#define SPD 8U
#define ANG 9U
#define DATE 10U
extern struct gprmc{
char time[10];
char latitude[10];
char longitude[10];
char speed[10];
char angle[10];
char date[10];
}gprmc_datas;
NMEA_parse.c:
#include "NMEA_parse.h"
struct gprmc gprmc_datas;
static void fill_data (char* param_in)
{
uint8_t i = 0U;
char* trunk;
char trunk_datas[20U][10U];
trunk = strtok(param_in, ",");
while(trunk != NULL)
{
i++;
if(i > 20) { i = 0; }
strcpy(trunk_datas[i],trunk);
trunk = strtok (NULL, ",");
}
if(memcmp(trunk_datas[1U],"GPRMC",6U) == 0U)
{
strcpy(gprmc_datas.time,trunk_datas[TIME]);
strcpy(gprmc_datas.latitude,trunk_datas[LAT]);
strcpy(gprmc_datas.longitude,trunk_datas[LON]);
strcpy(gprmc_datas.date,trunk_datas[DATE]);
strcpy(gprmc_datas.time,trunk_datas[TIME]);
}
}
主.c:
#include <stdio.h>
#include "NMEA_parse.h"
int main(void)
{
char *message = "$GPRMC,182127.00,A,4753.47678,N,02022.20259,E,0.837,,161019,,,A*7C\r\n";
char *mes = "$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A";
proc_sentence(message);
printf("\ntime: %s\n", gprmc_datas.time);
printf("latitude: %s\n", gprmc_datas.latitude);
printf("longitude: %s\n", gprmc_datas.longitude);
}
proc_sentence 函数将数据传递给 fill_data(),如果消息有效(校验和等) 当我使用mes作为输入时,一切都是正确的,但是当我切换到message时,出现了一些异常,因为结果如下:
时间:182127.00
纬度:4753.4767802022.2025E
经度:02022.2025E
你知道哪里出了问题吗?
最佳答案
如果将struct中的latitude[10]改成latitude[12],字符串“message”也可以使用。
关于c++ - C - 外部结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58475945/