nmealib GPS数据解析 设置不同的结束符

一般情况下,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;
}

你可能感兴趣的:(stm32)