//======================================================================
//=============================================================
//维护记录:2009-8-23 v1.0 by xgc
// 2010-1-30 v1.1 by xgc
// 增加函数:Get_Int_Number,Str_To_Int,GPS_GSV_Parse
//
//代码作者: 相广超 xgc94418297.blog.163.com
//=============================================================
#include "GPS.h"
#include
#ifndef __GPS_H_#define __GPS_H_
#define uint unsigned int
typedef struct{ int year; int month; int day; int hour; int minute; int second;}DATE_tiME;
typedef xdata struct{ double latitude; //经度 double longitude; //纬度 int latitude_Degree; //度 int latitude_Cent; //分 int latitude_Second; //秒 int longitude_Degree; //度 int longitude_Cent; //分 int longitude_Second; //秒 float speed; //速度 float direction; //航向 float height_ground; //水平面高度 float height_sea; //海拔高度 int satellite; uchar NS; uchar EW; DATE_TIME D;}GPS_INFO;
//void GPS_Init(void);int GPS_RMC_Parse(char *line,GPS_INFO *GPS);int GPS_GGA_Parse(char *line,GPS_INFO *GPS);int GPS_GSV_Parse(char *line,GPS_INFO *GPS);
void Int_To_Str(int x,char *Str);
static uchar GetComma(uchar num,char* str);
static int Get_Int_Number(char *s);
static double Get_Double_Number(char *s);
static float Get_Float_Number(char *s);
static void UTC2BTC(DATE_TIME *GPS);
//====================================================================//
// 语法格式:int GPS_RMC_Parse(char *line, GPS_INFO *GPS)
// 实现功能:把gps模块的GPRMC信息解析为可识别的数据
// 参 数:存放原始信息字符数组、存储可识别数据的结构体
// 返 回 值:
// 1: 解析GPRMC完毕
// 0: 没有进行解析,或数据无效
//====================================================================//
int GPS_RMC_Parse(char *line,GPS_INFO *GPS)
{
uchar ch, status, tmp;
float lati_cent_tmp, lati_second_tmp;
float long_cent_tmp, long_second_tmp;
float speed_tmp;
char *buf = line;
ch = buf[5];
status = buf[GetComma(2, buf)];
if (ch == 'C') //如果第五个字符是C,($GPRMC)
{
if (status == 'A') //如果数据有效,则分析
{
GPS -> NS = buf[GetComma(4, buf)];
GPS -> EW = buf[GetComma(6, buf)];
GPS->latitude = Get_Double_Number(&buf[GetComma(3, buf)]);
GPS->longitude = Get_Double_Number(&buf[GetComma( 5, buf)]);
GPS->latitude_Degree = (int)GPS->latitude / 100; //分离纬度
lati_cent_tmp = (GPS->latitude - GPS->latitude_Degree * 100);
GPS->latitude_Cent = (int)lati_cent_tmp;
lati_second_tmp = (lati_cent_tmp - GPS->latitude_Cent) * 60;
GPS->latitude_Second = (int)lati_second_tmp;
GPS->longitude_Degree = (int)GPS->longitude / 100; //分离经度
long_cent_tmp = (GPS->longitude - GPS->longitude_Degree * 100);
GPS->longitude_Cent = (int)long_cent_tmp;
long_second_tmp = (long_cent_tmp - GPS->longitude_Cent) * 60;
GPS->longitude_Second = (int)long_second_tmp;
speed_tmp = Get_Float_Number(&buf[GetComma(7, buf)]); //速度(单位:海里/时)
GPS->speed = speed_tmp * 1.85; //1海里=1.85公里
GPS->direction = Get_Float_Number(&buf[GetComma(8, buf)]); //角度
GPS->D.hour = (buf[7] - '0') * 10 + (buf[8] - '0'); //时间
GPS->D.minute = (buf[9] - '0') * 10 + (buf[10] - '0');
GPS->D.second = (buf[11] - '0') * 10 + (buf[12] - '0');
tmp = GetComma(9, buf);
GPS->D.day = (buf[tmp + 0] - '0') * 10 + (buf[tmp + 1] - '0'); //日期
GPS->D.month = (buf[tmp + 2] - '0') * 10 + (buf[tmp + 3] - '0');
GPS->D.year = (buf[tmp + 4] - '0') * 10 + (buf[tmp + 5] - '0')+2000;
UTC2BTC(&GPS->D);
return 1;
}
}
return 0;
}
//====================================================================//
// 语法格式:int GPS_GGA_Parse(char *line, GPS_INFO *GPS)
// 实现功能:把gps模块的GPGGA信息解析为可识别的数据
// 参 数:存放原始信息字符数组、存储可识别数据的结构体
// 返 回 值:
// 1: 解析GPGGA完毕
// 0: 没有进行解析,或数据无效
//====================================================================//
int GPS_GGA_Parse(char *line,GPS_INFO *GPS)
{
uchar ch, status;
char *buf = line;
ch = buf[4];
status = buf[GetComma(2, buf)];
if (ch == 'G') //$GPGGA
{
if (status != ',')
{
GPS->height_sea = Get_Float_Number(&buf[GetComma(9, buf)]);
GPS->height_ground = Get_Float_Number(&buf[GetComma(11, buf)]);
return 1;
}
}
return 0;
}
//====================================================================//
// 语法格式:int GPS_GSV_Parse(char *line, GPS_INFO *GPS)
// 实现功能:把gps模块的GPGSV信息解析为可识别的数据
// 参 数:存放原始信息字符数组、存储可识别数据的结构体
// 返 回 值:
// 1: 解析GPGGA完毕
// 0: 没有进行解析,或数据无效
//====================================================================//
int GPS_GSV_Parse(char *line,GPS_INFO *GPS)
{
uchar ch, status;
char *buf = line;
ch = buf[5];
status = buf[GetComma(2, buf)];
if (ch == 'V') //$GPGSV
{
GPS->satellite = Get_Int_Number(&buf[GetComma(3, buf)]);
return 1;
}
return 0;
}
//====================================================================//
// 语法格式: static int Str_To_Int(char *buf)
// 实现功能: 把一个字符串转化成整数
// 参 数:字符串
// 返 回 值:转化后整数值
//====================================================================//
static int Str_To_Int(char *buf)
{
int rev = 0;
int dat;
char *str = buf;
while(*str != ' |