【发布时间】:2020-02-16 22:22:07
【问题描述】:
我正在使用结构来处理 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]);
}
}
main.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
你知道出了什么问题吗?
【问题讨论】:
-
4753.47678 是 10 个字符,并且您的字段中没有额外的 '\0' 字节。
-
02022.20259是 11 个字符(甚至不包括额外的字节),因此完全不适合 10 个字符的数组。你所有的数组都应该是 64 字节。在字符串文字上使用strtok会导致未定义的行为。char *message和char *mes应该是char message[]和char mes[] -
感谢帮助,我没有考虑结束字节('\0')。增加数组的存储解决了这个问题
标签: c++ c string struct extern