分类 全球导航卫星系统 下的文章

NMEA的校验和

NMEA语句的校验和为'$'和'*'之间所有字符的异或和.

以下代码计算NMEA语句的校验和.

/*
 * Calculate nmea sentence's checksum
 * @buf:nmea sentence
 * @return: checksum
 */
unsigned char nmea_calc_checksum(const char *buf)
{
const char *p = buf;
unsigned char chs = 0;
​
while (*p == '$')   // skip '$'
p++;
while (*p != '*' && *p != 0)
chs ^= *p++;
​
return chs;
}

NMEA语句的校验和是否正确

/*
 * Check nmea sentence's checksum
 * @buf:buffer contains nmea sentence
 * @return: if checksum is okay return 1, else return 0
 */
int check_nmea(const char *buf) {
unsigned char chk = 0;
const char *p = buf;
char tmp[4];
​
while (*p == '$')
p++;
​
while (*p != '*' && *p != '\0')
chk ^= *p++;
​
if (*p == '*') {
sprintf(tmp, "%02X", chk);
return 0 == memcmp(tmp, p + 1, 2);
​
}
return 0;
}

NMEA中的经纬度

NMEA中经纬度格式为: ddmm.mmmm.
例如: 12023.4323, 表示120度23.4323分.
以下代码将NMEA经纬度字符串转化为度.

/*
 * Convert ddmm.mmmm format string into degrees
 * @s:  ddmm.mmmm format string in NMEA
 * @return: degree value 
 */
double convert_from_ddmm(const char *s)
{
double val = strtod(s, NULL);
int degrees = (int)val / 100;
double minutes = val - degrees * 100.0;
return degrees + minutes / 60.0;
}