现在能够读到完整的GPRMC的数据
GPS数据解析文件gps.c 主要帮我看看
[mw_shl_code=c,true]u8 GPS_RMC_Parse(u8 *line,GPS_INFO *GPS)函数中定义的数据类型对不对?
现在主程序调用
[mw_shl_code=c,true]GPS_DispTime()发到串口的解析数据都是0[/mw_shl_code]
[/mw_shl_code]
[mw_shl_code=c,true]#include "stm8s_it.h"
#include "string.h"
#include "gps.h"
#include "uart1.h"
GPS_INFO GPS;
u8 GPS_RMC_Parse(u8 *line,GPS_INFO *GPS)
{
u8 ch, status, tmp;
float lati_cent_tmp, lati_second_tmp;
float long_cent_tmp, long_second_tmp;
float speed_tmp;
u8 *buf = line;
ch = buf[5];
status = buf[GetComma(2, buf)];
if (ch == 'C')
{
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 = (u8)GPS->latitude / 100;
lati_cent_tmp = (GPS->latitude - GPS->latitude_Degree * 100);
GPS->latitude_Cent = (u8)lati_cent_tmp;
lati_second_tmp = (lati_cent_tmp - GPS->latitude_Cent) * 60;
GPS->latitude_Second = (u8)lati_second_tmp;
GPS->longitude_Degree = (u8)GPS->longitude / 100;
long_cent_tmp = (GPS->longitude - GPS->longitude_Degree * 100);
GPS->longitude_Cent = (u8)long_cent_tmp;
long_second_tmp = (long_cent_tmp - GPS->longitude_Cent) * 60;
GPS->longitude_Second = (u8)long_second_tmp;
speed_tmp = Get_Float_Number(&buf[GetComma(7, buf)]);
GPS->speed = speed_tmp * 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 0;
}
}
return 1;
}
/*语法格式:GPS_GSV_Parse(char *line, GPS_INFO *GPS)
实现功能:从GPGSV的信息中解析出当前可视卫星数
参数:*line 存放原始信息的数组 *GPS采集信息的结构体
返回值:0解析成功或1数据无效*/
u8 GPS_GSV_Parse(u8 *line,GPS_INFO *GPS)
{
u8 ch;
u8 *buf = line;
ch = buf[5];
/*status = buf[GetComma(2, buf)];*/
if (ch == 'V') //$GPGSV
{
GPS->satellite = Get_Int_Number(&buf[GetComma(3, buf)]);
return 0;
}
return 1;
}
/*语法格式:static uchar GetComma(uchar num,char *str)
static uchar GetComma(uchar num,char *str)
实现功能:计算字符串中各个逗号的位置
参数:num 查找第num个逗号在字符串中的位置 *str需要查找的字符串
返回值:所要查找的到的位置或1位置溢出*/
static u8 GetComma(u8 num,u8 *str)
{
u8 i,j = 0;
u8 len=strlen((const char*)str);
for(i = 0;i < len;i ++)
{
if(str == ',')
j++;
if(j == num)
return i + 1;
}
return 1;
}
/*语法格式:static u8 Str_To_Int(u8 *buf) static int Str_To_Int(char *buf)
实现功能:字符串转换为整型*/
static u8 Str_To_Int(u8 *buf)
{
u8 rev = 0;
u8 dat;
u8 *str = buf;
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;
}
rev = rev * 10 + dat;
str ++;
}
return rev;
}
/*语法格式:static u8 Get_Int_Number(u8 *s) static int Get_Int_Number(char *s)*/
static u8 Get_Int_Number(u8 *s)
{
u8 buf[10];
u8 i;
u8 rev;
i=GetComma(1, s);
i = i - 1;
strncpy((char*)buf, (const char*)s, i);
buf = 0;
rev=Str_To_Int(buf);
return rev;
}
/*语法格式:static u8 Get_Int_Number(u8 *s) static int Get_Int_Number(char *s)
实现功能:字符串转换为单精度*/
static float Str_To_Float(u8*buf)
{
float rev = 0;
float dat;
u8 integer = 1;
u8 *str = buf;
u8 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 float Get_Float_Number(u8 *s) static float Get_Float_Number(char *s)*/
static float Get_Float_Number(u8 *s)
{
u8 buf[10];
u8 i;
float rev;
i=GetComma(1, s);
i = i - 1;
strncpy((char*)buf, (const char*)s, i);
buf = 0;
rev=Str_To_Float(buf);
return rev;
}
/*语法格式:static double Str_To_Double(u8*buf) static double Str_To_Double(char *buf)
实现功能:字符串转换为双精度*/
static double Str_To_Double(u8 *buf)
{
double rev = 0;
double dat;
u8 integer = 1;
u8 *str = buf;
u8 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(u8 *s) static double Get_Double_Number(u8 *s)*/
static double Get_Double_Number(u8 *s)
{
u8 buf[10];
u8 i;
double rev;
i=GetComma(1, s);
i = i - 1;
strncpy((char*)buf, (const char*)s, i);
buf = 0;
rev=Str_To_Double(buf);
return rev;
}
static void UTC2BTC(DATE_TIME *GPS)
{
GPS->second ++;
if(GPS->second > 59)
{
GPS->second = 0;
GPS->minute ++;
if(GPS->minute > 59)
{
GPS->minute = 0;
GPS->hour ++;
}
}
GPS->hour = GPS->hour + 8;
if(GPS->hour > 23)
{
GPS->hour -= 24;
GPS->day += 1;
if(GPS->month == 2 ||
GPS->month == 4 ||
GPS->month == 6 ||
GPS->month == 9 ||
GPS->month == 11 )
{
if(GPS->day > 30)
{
GPS->day = 1;
GPS->month++;
}
}
else
{
if(GPS->day > 31)
{
GPS->day = 1;
GPS->month ++;
}
}
if(GPS->year % 4 == 0 )
{
if(GPS->day > 29 && GPS->month == 2)
{
GPS->day = 1;
GPS->month ++;
}
}
else
{
if(GPS->day > 28 &&GPS->month == 2)
{
GPS->day = 1;
GPS->month ++;
}
}
if(GPS->month > 12)
{
GPS->month -= 12;
GPS->year ++;
}
}
}
void Int_To_Str(u8 x,u8 *Str)
{
u8 t;
u8 *Ptr,Buf[5];
s8 i = 0;/*如果定义为u8即unsigned char(unsigned int) 那i>=0必然成立,所以定义为s8即char(unsigned int)*/
Ptr = Str;
if(x < 10)
{
*Ptr ++ = '0';
*Ptr ++ = x+0x30;
}
else
{
while(x > 0)
{
t = x % 10;
x = x / 10;
Buf[i++] = t+0x30;
}
i -- ;
for(;i>=0;i --)
{
*(Ptr++) = Buf;
}
}
*Ptr = '\0';
}
void GPS_DispTime(void)
{
u8 i = 0;
u8 ch;
u8 time[5];
u8 len;
UART1_printf("日期:");
Int_To_Str(GPS.D.year,time);
UART1_printf(time);
/*while(time != '\0')
{
ch = time[i++];
UART1_SendByte(ch);
}*/
len=strlen((char const*)time);
for(i=0;i<len;i++)
{
ch = time[i++];
UART1_SendByte(ch);
}
UART1_printf("年");
Int_To_Str(GPS.D.month,time);
i = 0;
while(time != '\0')
{
ch = time[i++];
UART1_SendByte(ch);
}
UART1_printf("月");
Int_To_Str(GPS.D.day,time);
i = 0;
while(time != '\0')
{
ch = time[i++];
UART1_SendByte(ch);
}
UART1_printf("日");
UART1_printf("\r\n");
UART1_printf("时间:");
Int_To_Str(GPS.D.hour,time);
i=0;
while(time != '\0')
{
ch = time[i++];
UART1_SendByte(ch);
}
UART1_SendByte(' ');
UART1_SendByte(':');
Int_To_Str(GPS.D.minute,time);
i = 0;
while(time != '\0')
{
ch = time[i++];
UART1_SendByte(ch);
}
UART1_SendByte(' ');
UART1_SendByte(':');
Int_To_Str(GPS.D.second,time);
i = 0;
while(time != '\0')
{
ch = time[i++];
UART1_SendByte(ch);
}
UART1_printf("\r\n");
}
[/mw_shl_code]
[mw_shl_code=c,true]GPS_INFO结构体定义在头文件gps.h中
[mw_shl_code=c,true]#ifndef __GPS_H_
#define __GPS_H_
typedef struct{
u8 year;
u8 month;
u8 day;
u8 hour;
u8 minute;
u8 second;
}DATE_TIME;
typedef struct{
double latitude;
double longitude;
u8 latitude_Degree;
u8 latitude_Cent;
u8 latitude_Second;
u8 longitude_Degree;
u8 longitude_Cent;
u8 longitude_Second;
float speed;
float direction;
float height_ground;
float height_sea;
u8 satellite;
u8 NS;
u8 EW;
DATE_TIME D;
}GPS_INFO;
u8 GPS_RMC_Parse(u8 *line,GPS_INFO *GPS);
u8 GPS_GGA_Parse(u8 *line,GPS_INFO *GPS);
u8 GPS_GSV_Parse(u8 *line,GPS_INFO *GPS);
static double Get_Double_Number(u8 *s);
static double Str_To_Double(u8 *buf);
static float Get_Float_Number(u8 *s);
static float Str_To_Float(u8*buf);
static u8 Get_Int_Number(u8 *s);
static u8 Str_To_Int(u8 *buf);
static u8 GetComma(u8 num,u8 *str);
static void UTC2BTC(DATE_TIME *GPS);
void Int_To_Str(u8 x,u8 *Str);
void GPS_DispTime(void);
#endif //__GPS_H_[/mw_shl_code]
[/mw_shl_code]
|