对于我的学校项目,我需要编程一个gps模块。该模块通过UART向我发送ASCII,我已经将它们组合成一个大字符串。
gps模块发送了一条非常大的消息,但我只需要其中一部分:
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGLL,4841.9388,N,01600.3418,E,000016.015,V,N*48
$GPZDA,000016.015,22,08,1999,,*55
$GPGSV,1,1,01,28,-90,000,33*66
$PERDCR,11,0,0,0.00,0.00,0.00,0.00,0.0,0,0,0.0,0,0,255,0,F,999*43
$PERDCR,1,32540,0,817,16,0,52,0,91,0,0,0,33,0x00,*41
$GPRMC,000017.015,V,4841.9388,N,01600.3418,E,0.00,0.00,220899,,,N*76
**$GPGGA,000017.015,4841.9388,N,01600.3418,E,0,00,0.0,277.9,M,0.0,M,,*6E** <-this part
粗体部分可以在此程序包中的任何位置。我已将包裹保存在
char data[507];
我如何从$ GPGGA-* 6E获得数据?
注意:数据会更改,GPGGA软件包的位置也会更改。唯一不变的两个参数是起始字符串:“ $ GPGGA”和GPGGA消息的长度,在这种情况下为70
最佳答案
您可以使用strstr查找字符串的第一个匹配项。您也可以使用strstr查找结尾,但是由于您知道长度始终为70,因此无需搜索该结尾。
// Proof of concept (uncompiled)
char* start = strstr(data, "$GPGGA");
strncpy(message, start, 70);
参考:strncpy/strncpy_s