📄 yanjun.cpp
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*********************************************************1*/
struct GPS01 //卫星结构体
{
int tai; //状态
int count; //卫星个数;
};
struct GPS02 //经纬度结构体
{
char jindu[15]; //经度
char weidu[15]; //纬度
float sulv; //速率
};
struct Time //时间结构体
{
int year,mouth,daty,hh,ff,ss;
};
/********************************************************************
$GPGGA,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,M,<10>,M,<11>,<12>*hh
$GPGGA,032746.222,3415.0004,N,10859.0967,E,1,03,6.6,28.4,M,-28.4,M,,0000*4E
<1> UTC时间,hhmmss(时分秒)格式
<2> 纬度ddmm.mmmm(度分)格式(前面的0也将被传输)
<3> 纬度半球N(北半球)或S(南半球)
<4> 经度dddmm.mmmm(度分)格式(前面的0也将被传输)
<5> 经度半球E(东经)或W(西经)
<6> GPS状态:0=未定位,1=非差分定位,2=差分定位,6=正在估算
<7> 正在使用解算位置的卫星数量(00~12)(前面的0也将被传输)
<8> HDOP水平精度因子(0.5~99.9)
<9> 海拔高度(-9999.9~99999.9)
<10> 地球椭球面相对大地水准面的高度
<11> 差分时间(从最近一次接收到差分信号开始的秒数,如果不是差分定位将为空)
<12> 差分站ID号0000~1023(前面的0也将被传输,如果不是差分定位将为空)
**************************************************************************
$GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>,<12>*hh
$GPRMC,032745.222,A,3414.9997,N,10859.0971,E,0.08,12.16,250507,,,A*54
<1> UTC时间,hhmmss(时分秒)格式
<2> 定位状态,A=有效定位,V=无效定位
<3> 纬度ddmm.mmmm(度分)格式(前面的0也将被传输)
<4> 纬度半球N(北半球)或S(南半球)
<5> 经度dddmm.mmmm(度分)格式(前面的0也将被传输)
<6> 经度半球E(东经)或W(西经)
<7> 地面速率(000.0~999.9节,前面的0也将被传输)
<8> 地面航向(000.0~359.9度,以真北为参考基准,前面的0也将被传输)
<9> UTC日期,ddmmyy(日月年)格式
<10> 磁偏角(000.0~180.0度,前面的0也将被传输)
<11> 磁偏角方向,E(东)或W(西)
<12> 模式指示(仅NMEA0183 3.00版本输出,A=自主定位,D=差分,E=估算,N=数据无效)
*********************************************************************************/
/**************************************************************2*/
char * dd() //打开文件
{
FILE *fp;
char *buf=(char *)malloc(5000);
char buf1;
char *p=buf;
int i=0;
if((fp = fopen("11.txt","r")) == NULL)
{
printf("cannot open the file!\n");
exit(0);
}
for(i = 0;(buf1 = fgetc(fp)) != EOF;i ++)
{
*p= buf1;
p++;
}
*p='\0';
fclose(fp);
return buf;
}
/***************************************************************3*/
char * g_GPGGA(char * buf) //读取GPGGA数据
{
char buff[7];
char * GPGGA=(char *)malloc(75);
char *p=buf;
char * p1;
int i,j;
while(*p!='\0')
{
if(*p=='$')
{ p1=p;
for(i=0;i<6;i++)
{
buff[i]=*p1;
p1++;
}
buff[i]='\0';
if(strcmp(buff,"$GPGGA")==0)
{
for(j=0;(j<75)&&(*(p+1)!='$');j++)
{
GPGGA[j]=*p;
p++;
}
}
}
p++;
}
return GPGGA;
}
/****************************************************************4*/
char * g_GPRMC(char * buf) //读取GPRMC数据
{
char buff[7];
char * GPRMC=(char *)malloc(75);
char *p=buf;
char * p1;
int i,j;
while(*p!='\0')
{
if(*p=='$')
{ p1=p;
for(i=0;i<6;i++)
{
buff[i]=*p1;
p1++;
}
buff[i]='\0';
if(strcmp(buff,"$GPRMC")==0)
{
for(j=0;(j<75)&&(*(p+1)!='$');j++)
{
GPRMC[j]=*p;
p++;
}
}
}
p++;
}
return GPRMC;
}
/************************************************************************5*/
struct GPS01 g_rGPGGA(char * str) //分析GPGGA数据,卫星的状态和个数
{
struct GPS01 GP;
char * p=str;
int i,num=0;
for(i=0;*p!='\0';i++)
{
p++;
if(*p==',')
{
num++;
}
if(num==6)
break;
}
p++;
sscanf(p,"%d",&GP.tai);
p+=2;
sscanf(p,"%2d",&GP.count);
return GP;
}
/************************************************************************6*/
struct GPS02 g_rGPRMC(char * str) //分析GPRMC数据,经度,纬度和速率
{
struct GPS02 GP;
char *p,*p2,*p3,*p4;
p=str;
int i;
int n;
int j=0;
int k=0;
int num=0;
int h=0;
char cha[5],cha1[5],cha2[6],cha3[5];
for(i=0;*p!='\0';i++)
{
p++;
if(*p==',')
{
num++;
switch(num)
{
case 3:
p++;
p2=p;
for(n=0;n<2;n++)
{
cha[n]=*p2;
p2++;
}
cha[n]='\0';
strcat(cha,"°");
strcpy(GP.weidu,cha);
for(j=0;j<2;j++)
{
cha1[j]=*p2;
*p2++;
}
cha1[j]='\0';
strcat(GP.weidu,cha1);
strcat(GP.weidu,"′");
break;
case 5:
p++;
p3=p;
for(k=0;k<3;k++)
{
cha2[k]=*p3;
*p3++;
}
cha2[k]='\0';
strcat(cha2,"°");
strcpy(GP.jindu,cha2);
for(h=0;h<2;h++)
{
cha3[h]=*p3;
*p3++;
}
cha3[h]='\0';
strcat(GP.jindu,cha3);
strcat(GP.jindu,"′");
break;
case 7:
p++;
p4=p;
sscanf(p4,"%4f",&GP.sulv);
break;
default :
break;
}
}
}
return GP;
}
/*********************************************************************7*/
struct Time g_rtime(char *str) //分析GPRMC中的时间和日期
{
struct Time TP;
char *p,*p1,*p2;
int i;
p=str;
int num=0;
for(i=0;*p!='\0';i++)
{
p++;
if(*p==',')
{
num++;
switch(num)
{
case 1:
p++;
p1=p;
sscanf(p1,"%2d",&TP.hh);
p1+=2;
sscanf(p1,"%2d",&TP.ff);
p1+=2;
sscanf(p1,"%2d",&TP.ss);
break;
case 9:
p++;
p2=p;
sscanf(p2,"%2d",&TP.daty);
p2+=2;
sscanf(p2,"%2d",&TP.mouth);
p2+=2;
sscanf(p2,"%2d",&TP.year);
default:
break;
}
}
}
return TP;
}
/*************************************************************************8*/
void UTCTime2nativeTime( struct Time s) //时间转换方法
{
if((s.mouth==1||s.mouth==3||s.mouth==5||s.mouth==7||s.mouth==8||s.mouth==10||s.mouth==12)&&s.daty==31)
{
if(s.hh+8>24)
{
s.hh=s.hh-16;
s.hh=1;
s.mouth=s.mouth+1;}
}
if((s.mouth==4||s.mouth==6||s.mouth==9||s.mouth==11)&&s.daty==30)
{
if(s.hh+8>24)
{
s.hh=s.hh-16;
s.daty=1;
s.mouth=s.mouth+1;}
}
if(s.mouth==2)
{
if (s.year%4==0&&s.year%100!=0||s.year%400==0)
{
if(s.daty==29)
if(s.hh+8>24)
{
s.hh=s.hh-16;
s.daty=1;
s.mouth=s.mouth+1;
}
}
else
{
if(s.daty==28)
if(s.hh+8>16)
{
s.hh=s.hh-24;
s.daty=1;
s.mouth=s.mouth+1;
}
}
}
if(s.mouth==12&&s.daty==31&&s.hh+8>24)
{
if(s.hh+8>24)
{
s.hh=s.hh-16;
s.daty=1;
s.mouth=1;
s.year=s.year+1;
}
}
if(s.hh>16)
{
s.hh=s.hh-16; s.daty=s.daty+1;}
else
{
s.hh=s.hh+8;
}
printf(" 北京时间:%02d年%02d月-%02d日-%02d时-%02d分-%02d秒\n",s.year,s.mouth,s.daty,s.hh,s.ff,s.ss);
}
/***********************************************************/
void main()
{
struct GPS01 g; //调结构体
struct GPS02 h;
struct Time t;
char * bufa;
char * bufc;
bufa=g_GPGGA(dd()); //从文件中读数据
bufc=g_GPRMC(dd());
g=g_rGPGGA(bufa);
h=g_rGPRMC(bufc);
t=g_rtime(bufc);
printf(" 卫星个数: %d\n",g.count);
printf("[卫星状态:0=未定位,1=非差分定位,2=差分定位,6=正在估算]\n 此时卫星状态: %d\n",g.tai);
printf(" 经 度:%s\n",h.jindu);
printf(" 纬 度:%s\n",h.weidu);
printf(" 速 率:%4.2f\n",h.sulv);
UTCTime2nativeTime(t); //调用时间方法
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -