#include<reg52.h>
#include <string.h>
#include "GPS.h"
#define uint unsigned int
#define uchar unsigned char
extern bit GPSParse; //GPS解析标志 1 正在解析 0解析完成
extern uchar num;//GPS接收数据数量
uchar GetComma(uchar num,char *str);
extern bit GPSDataEffect;
//====================================================================//
// 语法格式: static double Str_To_Double(char *buf)
// 实现功能: 把一个字符串转化成浮点数
// 参 数:字符串
// 返 回 值:转化后双精度值
//====================================================================//
double Str_To_Double(char *buf)
{
double rev = 0;
double dat;
int integer = 1;
char *str = buf;
int i;
while(*str != '\0')
{
switch(*str)
{
case '0':
dat = 0;
break;
case '1':
dat = 1;
break;
case '2':
dat = 2;
break;
case '3':
dat = 3;
break;
case '4':
dat = 4;
break;
case '5':
dat = 5;
break;
case '6':
dat = 6;
break;
case '7':
dat = 7;
break;
case '8':
dat = 8;
break;
case '9':
dat = 9;
break;
case '.':
dat = '.';
break;
}
if(dat == '.')
{
integer = 0;
i = 1;
str ++;
continue;
}
if( integer == 1 )
{
rev = rev * 10 + dat;
}
else
{
rev = rev + dat / (10 * i);
i = i * 10 ;
}
str ++;
}
return rev;
}
//====================================================================//
// 语法格式: static double Get_Double_Number(char *s)
// 实现功能:把给定字符串第一个逗号之前的字符转化成双精度型
// 参 数:字符串
// 返 回 值:转化后双精度值
//====================================================================//
double Get_Double_Number(char *s)
{
char buf[10];
uchar i;
double rev;
i=GetComma(1, s);
i = i - 1;
strncpy(buf, s, i);
buf[i] = 0;
rev=Str_To_Double(buf);
return rev;
}
//====================================================================//
// 语法格式:static uchar GetComma(uchar num,char *str)
// 实现功能:计算字符串中各个逗号的位置
// 参 数:查找的逗号是第几个的个数,需要查找的字符串
// 返 回 值:0
//====================================================================//
uchar GetComma(uchar num,char *str)
{
uchar i,j = 0;
int len=strlen(str);
for(i = 0;i < len;i ++)
{
if(str[i] == ',')
j++;
if(j == num)
return i + 1;
}
return 0;
}
/***************************************
运输定位数据$GPRMC
****************************************/
uchar Praes_GPRMC(uchar *buf,GPS_INFO *GPSinfo)//全球定位数据
{
uchar GPSDataEffect; //********************
uchar status;
double cent_tmp, second_tmp;
if (buf[5] == 'C') //如果第五个字符是C,($GPRMC)
{
status = buf[GetComma(2, buf)];
if (status == 'A') //如果数据有效,则分析
{
GPSinfo -> NS = buf[GetComma(4, buf)];
GPSinfo -> EW = buf[GetComma(6, buf)];
GPSinfo->latitude = Get_Double_Number(&buf[GetComma(3, buf)]); //纬度原始 数据
GPSinfo->longitude = Get_Double_Number(&buf[GetComma( 5, buf)]); //经度原始数据
GPSinfo->latitude_Degree = (uchar)(GPSinfo->latitude / 100); //分离度
cent_tmp = (GPSinfo->latitude - GPSinfo->latitude_Degree * 100); //分离 分
GPSinfo->latitude_Cent = (uchar)cent_tmp;
second_tmp = (cent_tmp - GPSinfo->latitude_Cent) * 60; //分离秒
GPSinfo->latitude_Second = second_tmp;
////////////////////////////////////////////////////////
GPSinfo->longitude_Degree = (uchar)(GPSinfo->longitude / 100); //分离经度
cent_tmp = (GPSinfo->longitude - GPSinfo->longitude_Degree * 100); //分离 分
GPSinfo->longitude_Cent = (uchar)cent_tmp;
second_tmp = (cent_tmp - GPSinfo->longitude_Cent) * 60; //分离秒
GPSinfo->longitude_Second = second_tmp;
GPSinfo->speed= Get_Double_Number(&buf[GetComma(7, buf)]);
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
GPSDataEffect=1;
// return 1;
}
else //if(status == 'V')
{
//无效
GPSDataEffect=0;
}
}
else //+++++++++++++++
{
GPSDataEffect=0;
}
return GPSDataEffect;
}