⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps_module.c

📁 通过车载终端采集GPS数据
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "gps_collect.h"
#include "gps.h"
#include "str_cmp.h"
#include "uart1.h"
#include "16c554.h"
#include <string.h>
#include <stdio.h>

typedef unsigned char	uchar;
typedef signed char		int8s;
typedef unsigned int	uint;
typedef unsigned long	ulong;


int gps_mod(uchar *gps_buff);
int get_sub_str(uchar *gps_buff, int first_comma, uchar *gps_sub_str);
int gps_chk(uchar *gps_buff);
int get_format(uchar *gps_buff);
int get_time(uchar *gps_buff, uchar *gps_bcd_time);
int get_gps_state(uchar *gps_buff, uchar *gps_state);
int get_date(uchar *gps_buff, uchar *gps_bcd_date);
int get_latitude(uchar *gps_buff, long *gps_latitude);
int get_longitude(uchar *gps_buff, long *gps_longitude);
int get_speed(uchar *gps_buff, uint *gps_speed);
int get_direction(uchar *gps_buff, uint *gps_direction);
int get_altitude(uchar *gps_buff, uint *gps_altitude);
int get_satelt_num(uchar *gps_buff, uchar *satelt_number);
GPS_STRING gps_string;
GPS_DATA gps_struct;
extern unsigned char uart1RxBuffer[];
static char gGps_buff[GPS_DATA_MAXLEN];
extern unsigned char gps_receive_flg;
extern GPS_LINE_INFO gps_8line[8];
extern unsigned char cur_line;
extern unsigned char cur_direction;
extern unsigned char station_key;
extern unsigned char  corner_key;
/********************************************************************************
*                                                                               
*   函数功能:校验GPS一行数据是否正确,正确返回0,错误返回-1                    
*   输入    :缓冲区的一行数据                                                  
*   输出    :返回判断结果,正确返回0,错误返回-1                               
*   数据包 :GPS数据                                                           
*                                                                               
********************************************************************************/

int gps_chk(uchar *gps_buff)
{
    uchar chk=0;
    uchar i=0;
    uchar m=0;
    uchar *pt=NULL;
    pt=gps_buff;
/*找'$'符号:找到'$'符号,正常处理进行校验和计算,找不到'$'符号,一直循环直到遇到'\0',结束;*/ 
   
    while (*pt!='\0')
    {
        if (*pt!='$')
        {
            pt++;                
        }
        else if (*pt=='$')
        {
            pt++;
            while((*pt != '*')&&(i<GPS_DATA_MAXLEN))        /*指向'*'前的字符,且整个GPRMC字符数小于80*/
            {
                chk ^= *pt;             /*对'$'字符后,'*'字符前的所有字符进行异或,得到校验和*/

                pt++;

                i++;
            }

            pt+=1;

            if((*pt>='0')&&(*pt<='9'))  /*如果是字符'0~9',变成数字0~9*/
            {
                m = *pt-0x30;
            }
            else                        /*如果是字符'A~Z',变成数字A~Z*/
            {
                m = *pt-0x41+10;
            }

            pt+=1;                      /*指向'*'后的第二个字符*/

            m = (m<<4) ;                /* 第一个字符左移4位 */

            if((*pt>='0')&&(*pt<='9'))  /*如果是字符'0~9',变成数字0~9*/
            {
                m += *pt-0x30;
            }

            else
            {
                m += *pt-0x41+10;       
            }

            if(m == chk)
            {    
                return GPS_OK;
            } 

            else
            {
                return GPS_ERROR;
            }
        }
    }
}

/********************************************************************************
*                                                                               
*   函数功能:提取缓冲区中的6种格式的数据                                       
*   输入    :缓冲区的数据                                                      
*   输出    :缓冲区的各种帧格式的数                                            
*   数据包 :GPS数据                                                           
*                                                                               
********************************************************************************/

int get_format(uchar *gps_buff)
{
	uchar gps_header[6];
	uchar i=0;

    /*定义协议头*/
    uchar code gps_head[7][6]={"GPRMC","GPGGA","GPTXT","GPGSV","GPGSA","GPGLL","GPZDA"};
	
	/* 取GPS字符串头部,以用于后序GPS报文类型的判断、结果的返回! */
	
    while(*gps_buff!='\0')
    {
        if('$'!=*gps_buff)

            gps_buff++;

        else if ('$'==*gps_buff)    
        {  
            gps_buff+=1;

            for (i=0; i<5; i++)
            {
	            gps_header[i]=*gps_buff;

                gps_buff++;
            }

	        gps_header[5]='\0';

            if (!str_cmp(gps_header,gps_head[0]))
            {
	            return GPRMC;
            } 

            else if (!str_cmp(gps_header, gps_head[1]))
            {
                return GPGGA;
            } 

            else if (!str_cmp(gps_header, gps_head[2]))
            {
                return GPTXT;
            } 

            else if (!str_cmp(gps_header, gps_head[3]))
            {
	            return GPGSV;
            } 

            else if (!str_cmp(gps_header, gps_head[4]))
            {
                return GPGSA;
            }

            else if (!str_cmp(gps_header, gps_head[5]))
            {
                return GPGLL;
            } 
            
            else if (!str_cmp(gps_header, gps_head[6]))
            {
                return GPZDA;
            }

             else
            {
          		return GPS_ERROR;
           	}
        }
    }    
}

/********************************************************************************
*                                                                               
*   函数功能:将时间数据压缩成BCD编码                                           
*   输入    :原始时间数据                                                      
*   输出    :时间的压缩BCD编码                                               
*   数据包 :GPRMC                                                             
*                                                                               
********************************************************************************/

int get_time(uchar *gps_buff, uchar *gps_bcd_time)
{
    uchar comma=1;
    uchar i=0;    
    uchar time_array[20];
    uchar *gps_time_data=time_array;
    
    if(GPS_OK==get_sub_str(gps_buff, comma, gps_time_data))
    {
        for (i=0; i<3; i++)
        {
            gps_bcd_time[i]=((*gps_time_data)-'0')*10+*(gps_time_data+1)-'0';

            gps_time_data+=2;
        }

        return GPS_OK;        
    }
    else
    {        
        return GPS_ERROR;
    }
}
/********************************************************************************
*                                                                               
*   函数功能:提取缓冲区中的GPS状态                                             
*   输入    :缓冲区中的字符串                                                  
*   输出    :GPS状态                                                           
*   数据包 :GPRMC                                                             
*                                                                               
********************************************************************************/
int get_gps_state(uchar *gps_buff, uchar *gps_state)
{
    uchar comma=2;
    uchar i=0;
    uchar state_array[5];
    uchar *gps_state_data=state_array;
   
    if(GPS_OK==get_sub_str(gps_buff, comma, gps_state_data))
    {
        *gps_state=*gps_state_data;
        return GPS_OK;

    }
    else
    {
        return GPS_ERROR;
    }
}

/********************************************************************************
*                                                                               
*   函数功能:将日期数据变成压缩BCD编码                                           
*   输入    :原始日期数据                                                     
*   输出    :日期的压缩BCD编码                                               
*   数据包 :GPRMC                                                             
*                                                                               
********************************************************************************/

int get_date(uchar *gps_buff, uchar *gps_bcd_date)
{
    uchar comma=9;
    uchar i=0;
    uchar date_array[10];
    uchar *gps_date_data=date_array;
   
    if(GPS_OK==get_sub_str(gps_buff, comma, gps_date_data))
    {
        for (i=0;i<3;i++)
        {
            gps_bcd_date[i]=((*gps_date_data)-'0')*10+*(gps_date_data+1)-'0';

            gps_date_data+=2;
        }

        return GPS_OK;
    }

    else
    {
        return GPS_ERROR;
    }
}
/******************************************************************************
*
*函数功能:取出GPS天线的短路、开路状态
*
******************************************************************************/

/********************************************************************************
*                                                                             
*   函数功能:将纬度数据转换成以‘分’为单位的数值                            
*   输入    :纬度字符串                                                      
*   输出    :以‘分’为单位的数值,乘以10000后,并去除小数点后的一位       
*   数据包 :GPRMC                                                            
*                                                                               
********************************************************************************/

/********************************************************************************
*                                                                              
*   函数功能:将经度数据转换成以‘分’为单位的数值                         
*   输入    :经度字符串                                                        
*   输出    :以‘分’为单位的数值,乘以10000后,并去除小数点后的一位       
*   数据包 :GPRMC                                                             
*                                                                              
********************************************************************************/

/********************************************************************************
*                                                                               
*   函数功能:根据GPS数据判断当前方向值                                         
*   输入    :GPS方向数据                                                       
*   输出    :方向值                                                            
*   数据包 :GPRMC                                                             
*                                                                               
********************************************************************************/

int get_direction(uchar *gps_buff, uint *gps_direction)
{
    uchar comma=8;
    uchar i=0;
    int point=0;
    uchar n=0;    
    uchar direction_array[10];
    uchar *gps_direction_data=direction_array;

    if(GPS_OK==get_sub_str(gps_buff, comma, gps_direction_data))

    { 
        if(*gps_direction_data==0)   /*当两个逗号之间是空时,赋值为0,防止赋随机值*/

        {
            *gps_direction='\0';            
        }

        else                         /*当两个逗号之间是正确数据时,正常处理*/

        {     
            for(i=0;i<6;i++)
	        {
                if (*gps_direction_data=='.')
                {
                    point=1;

                    gps_direction_data++;

                    continue; 
                }

                if (!point)
                {
                    *gps_direction=((*gps_direction)*10)+((*gps_direction_data)-0x30);
                }

                else
                {
                    if (n<1)
                    {
                        *gps_direction=((*gps_direction)*10)+((*gps_direction_data)-0x30);

                        n++;
                    }
                }

                gps_direction_data++;
            }
        }

        return GPS_OK;    
    }

    else

    {
        return GPS_ERROR;
    }
}

/********************************************************************************
*                                                                               
*   函数功能:输出高度数值                                                      
*   输入    :GPS高度数据                                                       
*   输出    :高度值                                                            
*   数据包 :GPGGA                                                             
*                                                                               
********************************************************************************/

int get_altitude(uchar *gps_buff, uint *gps_altitude)
{
    uchar comma=9;
    uchar i=0;
    int point=0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -