基于C语言的NMEA-0183解析程序(一)

        基于C语言对NMEA-0183命令进行解析,实现从TXT文档中按数据包读入数据并进行解析,之后根据用户输入定位是否有效,速度阈值,定位卫星数目,卫星信号强度等条件进行筛选输出。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Type.h"
#include"Operation.h"
#include"Condition.h"
#include"Translate.h"
#include"Package.h"
#include"Analysis.h"
#include"Printout.h"

int main(void)
{
	char avhemi;
	float speed;
	int svnum, sn;

	printf("******************************\n");
	printf("GPS Loading->NMEA-0183 Analyze\n");
	printf("******************************\n");
	avhemi = Condition_Locate();
	speed = Condition_Speed();
	svnum = Condition_Svnum();
	sn = Condition_Sn();
	printf("******************************\n");
	printf("avc:%c,speed:%.3f,svnum=%d,sn=%d\n", avhemi, speed, svnum, sn);
	printf("******************************\n");

	Translate(avhemi, speed, svnum, sn);

	return 0;
}

解析引用声明

#ifndef _ANALYSIS_H_
#define _ANALYSIS_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Type.h"
#include"Operation.h"
#include"NMEA_GPZDA_Analysis.h"
#include"NMEA_GPGGA_Analysis.h"
#include"NMEA_GPGLL_Analysis.h"
#include"NMEA_GPVTG_Analysis.h"
#include"NMEA_GPGSA_Analysis.h"
#include"NMEA_GPGSV_Analysis.h"
#include"NMEA_GPRMC_Analysis.h"

#endif // _ANALYSIS_H_

GPZDA解析

#ifndef _NMEA_GPZDA_Analysis_H_
#define _NMEA_GPZDA_Analysis_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPZDA Analysis
 *
 *  Purpose     -  GPZDA命令解析
 *
 *  Description -  当前时间信息
 *                 $GPZDA,(1),(2),(3),(4),(5),(6)*hh(CR)(LF)
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-20,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPZDA_Analysis(Nmea_msg *gpsx,u8 *buf)
{
	u8 *p1, *node;
    u8 posx;
    u32 temp;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPZDA", 6);
	if (p1 == NULL)
	{
		return buf;
	}
	posx = NMEA_Comma_Pos(p1, 1);/*UTC时间*/
    if(posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx) / NMEA_Pow(10, 2);/*UTC时间,hhmmss.ss去掉ms*/
		gpsx->utc.hour = temp / 10000;
		gpsx->utc.mint = (temp / 100) % 100;
		gpsx->utc.sec = temp % 100;
    }
	posx = NMEA_Comma_Pos(p1, 2);
	if (posx != 0XFF)
		gpsx->utc.date = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 3);
	if (posx != 0XFF)
		gpsx->utc.month = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 4);
	if (posx != 0XFF)
		gpsx->utc.year = NMEA_Str2num(p1 + posx);

	posx = NMEA_End_Pos(p1);/*6*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPZDA_Analysis_H_

 GPGGA解析

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPGGA Analysis
 *
 *  Purpose     -  GPGGA命令解析
 *
 *  Description -  GPS定位信息
 *                 $GPGGA,(1),(2),(3),(4),(5),(6),(7),(8),(9),M,(10),M,(11),(12)*hh(CR)(LF)
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-13,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPGGA_Analysis(Nmea_msg *gpsx,u8 *buf)
{
	u8 *p1, *node;
    u8 posx;
	u32 temp;
	float rs;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPGGA", 6);
	if (p1 == NULL)
	{
		return buf;
	}
	posx = NMEA_Comma_Pos(p1, 1);/*UTC时间*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx) / NMEA_Pow(10, 2);/*UTC时间,hhmmss.ss去掉ms*/
		gpsx->utc.hour = temp / 10000;
		gpsx->utc.mint = (temp / 100) % 100;
		gpsx->utc.sec = temp % 100;
    }
	posx = NMEA_Comma_Pos(p1, 2);/*纬度 ddmm.mmmmm(度分)*/
	if (posx != 0XFF)
	{
		temp = NMEA_Str2num(p1 + posx);           /*ddmm.mmmmm*/
		gpsx->latitude = temp / NMEA_Pow(10, 7);  /*得到°*/
		rs = (temp / 10000) % 100;                /*得到'*/
		gpsx->latitude = gpsx->latitude + rs / 60;/*转换为°*/
	}
	posx = NMEA_Comma_Pos(p1, 3);/*南北纬*/
	if (posx != 0XFF)gpsx->nshemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 4);/*经度*/
	if (posx != 0XFF)
	{
		temp = NMEA_Str2num(p1 + posx);             /*dddmm.mmmmm*/
		gpsx->longitude = temp / NMEA_Pow(10, 7);   /*得到°*/
		rs = (temp / 10000) % 100;                  /*得到'*/
		gpsx->longitude = gpsx->longitude + rs / 60;/*转换为°*/
	}
	posx = NMEA_Comma_Pos(p1, 5);/*东西经*/
	if (posx != 0XFF)gpsx->ewhemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 6);/*得到GPS状态*/
	if (posx != 0XFF)gpsx->gpssta = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 7);/*用于定位的卫星数*/
	if (posx != 0XFF)gpsx->posslnum = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 8);/*HDOP位置精度因子*/
	if (posx != 0XFF)gpsx->hdop = (float)NMEA_Str2num(p1 + posx) / 100;
	posx = NMEA_Comma_Pos(p1, 9);/*海拔高度*/
	if (posx != 0XFF)gpsx->altitude = (float)NMEA_Str2num(p1 + posx) / 10;

	posx = NMEA_End_Pos(p1);/*14*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPGGA_Analysis_H_

 GPGLL解析

#ifndef _NMEA_GPGLL_Analysis_H_
#define _NMEA_GPGLL_Analysis_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPGLL Analysis
 *
 *  Purpose     -  GPGLL命令解析
 *
 *  Description -  大地坐标信息
 *                 $GPGLL,(1),(2),(3),(4),(5),(6),(7)*hh(CR)(LF)
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-14,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPGLL_Analysis(Nmea_msg *gpsx, u8 *buf)
{
	u8 *p1, *node;
    u8 posx;
    u32 temp;
    float rs;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPGLL", 6);
	if (p1 == NULL)
	{
		return buf;
	}
	posx = NMEA_Comma_Pos(p1, 1);/*纬度 ddmm.mmmmm(度分)*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);           /*ddmm.mmmmm*/
		gpsx->latitude = temp / NMEA_Pow(10, 7);  /*得到°*/
		rs = (temp / 10000) % 100;                /*得到'*/
		gpsx->latitude = gpsx->latitude + rs / 60;/*转换为°*/
    }
	posx = NMEA_Comma_Pos(p1, 2);/*南北纬*/
	if (posx != 0XFF)gpsx->nshemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 3);/*经度*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);             /*dddmm.mmmmm*/
		gpsx->longitude = temp / NMEA_Pow(10, 7);   /*得到°*/
		rs = (temp / 10000) % 100;                  /*得到'*/
		gpsx->longitude = gpsx->longitude + rs / 60;/*转换为°*/
    }
	posx = NMEA_Comma_Pos(p1, 4);/*东西经*/
	if (posx != 0XFF)gpsx->ewhemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 5);/*UTC时间*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx) / NMEA_Pow(10, 2);/*UTC时间,hhmmss.ss去掉ms*/
		gpsx->utc.hour = temp / 10000;
		gpsx->utc.mint = (temp / 100) % 100;
		gpsx->utc.sec = temp % 100;
    }
	posx = NMEA_Comma_Pos(p1, 6);/*有无效用定位*/
	if (posx != 0XFF)gpsx->avhemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 7);/*定位模式指示*/
	if (posx != 0XFF)gpsx->pattern = *(p1 + posx);

	posx = NMEA_End_Pos(p1);/*7*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPGLL_Analysis_H_

 GPVTG解析

#ifndef _NMEA_GPVTG_Analysis_H_
#define _NMEA_GPVTG_Analysis_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPVTG Analysis
 *
 *  Purpose     -  GPVTG命令解析
 *
 *  Description -  地面速率信息
 *                 $GPVTG,(1),T,(2),M,(3),N,(4),K,(5)*hh(CR)(LF)
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-19,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPVTG_Analysis(Nmea_msg *gpsx,u8 *buf)
{
	u8 *p1, *node;
    u8 posx;
	u32 temp;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPVTG", 6);
	if (p1 == NULL)
	{
		return buf;
	}
	posx = NMEA_Comma_Pos(p1, 1);/*地北航向角度*/
	if (posx != 0XFF)gpsx->entude = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 3);/*磁北航向角度*/
	if (posx != 0XFF)gpsx->mntude = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 5);/*地面速率(航节)*/
	if (posx != 0XFF)
	{
		temp = NMEA_Str2num(p1 + posx);
		gpsx->speed = (float)temp / 1000 * 1.852;
	}
	posx = NMEA_Comma_Pos(p1, 7);/*地面速率(km/h)*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);
		gpsx->speed = (float)temp / 1000;
    }
	posx = NMEA_Comma_Pos(p1, 9);/*定位模式指示*/
	if (posx != 0XFF)gpsx->pattern = *(p1 + posx);

	posx = NMEA_End_Pos(p1);/*9*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPVTG_Analysis_H_

 GPGSA解析

#ifndef _NMEA_GPGSA_Analysis_H_
#define _NMEA_GPGSA_Analysis_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPGSA Analysis
 *
 *  Purpose     -  GPGSA命令解析
 *
 *  Description -  当前卫星信息
 *                 $GPGSA,(1),(2),(3),(3),(3),(3),(3),(3),(3),(3),(3),(3),(3),(3),(4),(5),(6)*hh(CR)(LF)
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-15,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPGSA_Analysis(Nmea_msg *gpsx,u8 *buf)
{
	u8 *p1, *node;
	u8 posx;
    u8 i;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPGSA", 6);
	if (p1 == NULL)
	{
		return buf;
	}
	posx = NMEA_Comma_Pos(p1, 1);/*手自模式*/
	if (posx != 0XFF)gpsx->mode = *(p1 + posx);/*手动自动模式*/
	posx = NMEA_Comma_Pos(p1, 2);/*定位类型*/
	if (posx != 0XFF)gpsx->fixmode = NMEA_Str2num(p1 + posx);/*确定定位类型*/
    for (i=0;i<32;i++)
    {
		posx = NMEA_Comma_Pos(p1, 3 + i);/*定位卫星编号*/
		if (posx != 0XFF)gpsx->possl[i] = NMEA_Str2num(p1 + posx);
		else break;
    }
	posx = NMEA_Comma_Pos(p1, 15);/*PDOP位置精度因子*/
	if (posx != 0XFF)gpsx->pdop = (float)NMEA_Str2num(p1 + posx) / 100;
	posx = NMEA_Comma_Pos(p1, 16);/*HDOP位置精度因子*/
	if (posx != 0XFF)gpsx->hdop = (float)NMEA_Str2num(p1 + posx) / 100;
	posx = NMEA_Comma_Pos(p1, 17);/*VDOP位置精度因子*/
	if (posx != 0XFF)gpsx->vdop = (float)NMEA_Str2num(p1 + posx) / 100;

	posx = NMEA_End_Pos(p1);/*17*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPGSA_Analysis_H_

GPGSV解析 

#ifndef _NMEA_GPGSV_Analysis_H_
#define _NMEA_GPGSV_Analysis_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPGSV Analysis
 *
 *  Purpose     -  GPGSV命令解析
 *
 *  Description -  可见卫星信息
 *                 $GPGSV, (1),(2),(3),(4),(5),(6),(7),(4),(5),(6),(7),(4),(5),(6),(7),(4),(5),(6),(7)*hh(CR)(LF)
 *                 可见卫星数 卫星编号[信号强度]
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-17,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPGSV_Analysis(Nmea_msg *gpsx,u8 *buf)
{
	u8 *p1, *node;
	u8 i, j;
    u8 posx;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPGSV", 6);
	if (p1 == NULL)
	{
		return buf;
	}
    i = p1[9] - '0';/*GPGSV编号*/
    i = i - 1;
	posx = NMEA_Comma_Pos(p1, 3);/*得到可见卫星总数*/
	if (posx != 0XFF)
		gpsx->svnum = NMEA_Str2num(p1 + posx);
    for(j=0;j<4;j++)
    {
        posx = NMEA_Comma_Pos(p1, 4 + j * 4);/*卫星编号*/
        if (posx != 0XFF)
        {
            gpsx->slmsg[4 * i + j].num = NMEA_Str2num(p1 + posx);
        }
        else break;
        posx = NMEA_Comma_Pos(p1, 5 + j * 4);/*卫星仰角*/
        if (posx != 0XFF)
        {
            gpsx->slmsg[4 * i + j].eledeg = NMEA_Str2num(p1 + posx);
        }
        else break;
        posx = NMEA_Comma_Pos(p1, 6 + j * 4);/*卫星方位角*/
        if (posx != 0XFF)
        {
            gpsx->slmsg[4 * i + j].azideg = NMEA_Str2num(p1 + posx);
        }
        else break;
        posx = NMEA_Comma_Pos(p1, 7 + j * 4);/*卫星信号强度*/
        if (posx != 0XFF)
        {
            gpsx->slmsg[4 * i + j].sn = NMEA_Str2num(p1 + posx);
        }
        else break;
    }
	posx = NMEA_End_Pos(p1);/*19*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPGSV_Analysis_H_

GPRMC解析 

#ifndef _NMEA_GPRMC_Analysis_H_
#define _NMEA_GPRMC_Analysis_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#include"Operation.h"
#include"Type.h"
/******************************************************************************
 *  Function    -   NMEA GPRMC Analysis
 *
 *  Purpose     -  GPRMC命令解析
 *
 *  Description -  推荐定位信息
 *                 $GPRMC,(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)*hh(CR)(LF)
 *                 日期 时间 有无效定位 东西经 经度 南北纬 纬度 地面速率
 *
 * modification history
 * ----------------------------------------
 * v1.0  , 2018-12-18,  LynxChang  written
 * ----------------------------------------
 ******************************************************************************/
u8 *NMEA_GPRMC_Analysis(Nmea_msg *gpsx,u8 *buf)
{
	u8 *p1, *node;
    u8 posx;
    u32 temp;/*32位变量读取时间数据*/
    float rs;
    if(strlen((char*)buf) == 0)
    {
        return buf;
    }
	p1 = (u8*)strnstr((char *)buf, "$GPRMC", 6);
	if (p1 == NULL)
	{
		return buf;
	}
	posx = NMEA_Comma_Pos(p1, 1);/*UTC时间*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx) / NMEA_Pow(10, 2);/*UTC时间,hhmmss.ss去掉ms*/
		gpsx->utc.hour = temp / 10000;
		gpsx->utc.mint = (temp / 100) % 100;
		gpsx->utc.sec = temp % 100;
    }
	posx = NMEA_Comma_Pos(p1, 2);/*有无效用定位*/
	if (posx != 0XFF)gpsx->avhemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 3);/*纬度 ddmm.mmmm(度分)*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);           /*ddmm.mmmmm*/
		gpsx->latitude = temp / NMEA_Pow(10, 7);  /*得到°*/
		rs = (temp / 10000) % 100;                /*得到'*/
		gpsx->latitude = gpsx->latitude + rs / 60;/*转换为°*/
    }
	posx = NMEA_Comma_Pos(p1, 4);/*南北纬*/
	if (posx != 0XFF)gpsx->nshemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 5);/*经度*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);             /*dddmm.mmmmm*/
		gpsx->longitude = temp / NMEA_Pow(10, 7);   /*得到°*/
		rs = (temp / 10000) % 100;                  /*得到'*/
		gpsx->longitude = gpsx->longitude + rs / 60;/*转换为°*/
    }
	posx = NMEA_Comma_Pos(p1, 6);/*东西经*/
	if (posx != 0XFF)gpsx->ewhemi = *(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 7);/*地面速率(航节)*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);
		gpsx->speed = (float)temp / 1000 * 1.852;
    }
	posx = NMEA_Comma_Pos(p1, 8);/*地北航向角度*/
	if (posx != 0XFF)gpsx->entude = NMEA_Str2num(p1 + posx);
	posx = NMEA_Comma_Pos(p1, 9);/*UTC日期*/
    if (posx != 0XFF)
    {
		temp = NMEA_Str2num(p1 + posx);
		gpsx->utc.date = temp / 10000;
		gpsx->utc.month = (temp / 100) % 100;
		gpsx->utc.year = 2000 + temp % 100;
    }

	posx = NMEA_End_Pos(p1);/*12*/
	node = p1 + posx + 1;
	return node;
}

#endif // _NMEA_GPRMC_Analysis_H_

猜你喜欢

转载自blog.csdn.net/Dark_knights/article/details/86501634