一般情況下,GPS模塊發送的數據是以\r\n
結束的,但是有時候經過我們的串口助手保存,可能就會被處理成了\n
結束,修改文件裏的每一行需要藉助notepad++等工具,比較麻煩,這裏直接修改代碼的11行和30行處,適配\n
結束的nmea數據包。
/**
* \brief Find tail of packet ("\r\n") in buffer and check control sum (CRC).
* @param buff a constant character pointer of packets buffer.
* @param buff_sz buffer size.
* @param res_crc a integer pointer for return CRC of packet (must be defined).
* @return Number of bytes to packet tail.
*/
int nmea_find_tail(const char *buff, int buff_sz, int *res_crc)
{
//static const int tail_sz = 3 /* *[CRC] */ + 2 /* \r\n */;
static const int tail_sz = 3 /* *[CRC] */ + 1 /* \n */;
const char *end_buff = buff + buff_sz;
int nread = 0;
int crc = 0;
NMEA_ASSERT(buff && res_crc);
*res_crc = -1;
for (; buff < end_buff; ++buff, ++nread)
{
if (('$' == *buff) && nread)
{
buff = 0;
break;
}
else if ('*' == *buff)
{
// if(buff + tail_sz <= end_buff && '\r' == buff[3] && '\n' == buff[4])
if (buff + tail_sz <= end_buff && '\n' == buff[3])
{
*res_crc = nmea_atoi(buff + 1, 2, 16);
nread = buff_sz - (int)(end_buff - (buff + tail_sz));
if (*res_crc != crc)
{
*res_crc = -1;
buff = 0;
}
}
break;
}
else if (nread)
crc ^= (int) * buff;
}
if (*res_crc < 0 && buff)
nread = 0;
return nread;
}