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

📄 gps9.cpp

📁 非常好的GPS接受显示程序
💻 CPP
字号:
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<string.h>
#include<bios.h>
#include <graphics.h>
#include <io.h>
#include <fcntl.h>
#define BUFFLEN 1024
#include<stdlib.h>
#include<math.h>

void InitCOM();
void OpenPort();
void ClosePOrt();
double LBx(double LL,double BB);
double LBy(double LL,double BB);
void interrupt far asyncint(...);
void interrupt (*asyncoldvect)(...);

void send_char(unsigned char unch);
void LB_XY(int TH,int DK,double LO,double B,double *X,double *Y,int DH);
unsigned char Buffer[BUFFLEN];
unsigned char data[100];
unsigned char gps[100]="GPGGA";
unsigned char packbuffer[BUFFLEN];
int buffin=0;
int buffout=0;
int head=0;
int tail=0;
int sendsum=0;
int rece_count=0;
int rece_flag=0;
int send_flag=0;
unsigned char xx1_1[10],yy1_1[10];
unsigned char wd5_1[10],jd5_1[10],gd_1[10],star_1[10];
unsigned char time6_1[5],time2_1[5],time3_1[5];
int old=0;
	int debug=0;
char temp=0;
int flag;
FILE *fpb,*checkfp;

int convert(char ch,int n)
{
   return((ch>>n)&0x01);
}

int out24hz(int  x,int  y,int  z, int  color,char *p)
{
	unsigned int num,QM,WM;
	int i,j,k;
	long  pointer;
	char Bit[72];
	unsigned char ch[2];

	if((flag=open("hzk24s",O_RDWR|O_BINARY))==1)
	{
		printf("Can't open word storeroom 000.zk!\n");
		return(0);
	}
	while (*p==' ')
	{
		for(i=0;i<24;i++)
		for(j=0;j<24;j++)
		putpixel(x+i,y+j,color);
		x=x+z+24;
		p++;
	}
	pointer=x;
	if(*p==0)
	{
		close(flag);
		fclose(fpb);
		return(pointer);
	}
	while (1)
	{
		ch[0]=*p++;
		ch[1]=*p++;
		if(ch[0]>0xA0)
		{
			QM=(ch[0]-15-0xA1)&0x07F;
			if(ch[1]>0xA0)
			{
				WM=(ch[1]-0xA1)&0x07F;
				pointer=(QM*94+WM)*72L;
				lseek(flag,pointer,SEEK_SET);
				read(flag,Bit,72);
			}
			else
			{
				close(flag);
				outtextxy(x,y,"Didn't fine Chinese word");
				return(-1);
			}
		}
		else
		{
			close(flag);
			outtextxy(x,y,"Didn't fine Chinese word");
			return(-1);
		}
		for(i=0;i<24;i++)
		for(j=0;j<3;j++)
		for(k=0;k<8;k++)

		if(convert(Bit[i*3+j],7-k))
			  putpixel(x+i,y+j*8+k,color);
		x=x+24+z;
		while(*p==' ')
		{
			for(i=0;i<24;i++)
			for(j=0;j<24;j++)
			putpixel(x+i,y+j,color);
			x=x+z+24;
			p++;

		}
		pointer=x;
		if (*p==0)
		{
			close(flag);
			return(pointer);

		}
	}

}

void OpenPort()
{
   unsigned char ucTemp;
   InitCOM();

   asyncoldvect=getvect(0x0B);
   disable();
   inportb(0x2f8);
   inportb(0x2fe);
   inportb(0x2fb);
   inportb(0x2fa);
   outportb(0x2fc,0x08|0x0b);
   outportb(0x2f9,0x01);
   ucTemp=inportb(0x21)&0xf7;
   outportb(0x21,ucTemp);
   setvect(0x0B,asyncint);
   enable();
}

void interrupt far asyncint(...)
{
	Buffer[rece_count] = inportb(0x2f8);
	temp=Buffer[rece_count];
	//fprintf(checkfp,"%c ",temp);

				if(Buffer[rece_count]=='$')
				{
					head=rece_count;
				}
				if(Buffer[rece_count]=='\n')
				{
					tail=rece_count;

						rece_flag++;
				}

				rece_count++;
				if(rece_count>=1023)
					rece_count=0;
				outportb(0x20,0x20);
}

void ClosePort(void)
{
   disable();
   outportb(0x2f9,0x00);
   outportb(0x2fc,0x00);
   outportb(0x21,inportb(0x21)&0x10);
   enable();
   setvect(0x0B,asyncoldvect);
}

void InitCOM()
{
  outportb(0x2fb,0x80);

  outportb(0x2f8,0x18);
  outportb(0x2f9,0x00);

  outportb(0x2fb,0x03);

  outportb(0x2fc,0x08|0x0b);
  outportb(0x2f9,0x01);


outportb(0x3fb,0x80);
  outportb(0x3f8,0x03);
  outportb(0x3f9,0x00);

  outportb(0x3fb,0x03);
  outportb(0x3fc,0x08|0x0b);
  outportb(0x3f9,0x01);
}

int pack_data(unsigned char *p)
{
	unsigned char tempsent[30];
	int i,j;
	int checksum=0;
	for(i=0;i<30;i++)
		tempsent[i]=0;
	tempsent[0]=0x09;
	tempsent[1]=0x1a;
	tempsent[2]=0x00;
	tempsent[3]=0x00;
	tempsent[4]=0x00;
	tempsent[5]=0x63;
	tempsent[6]=0x00;
	tempsent[7]=0x00;
	tempsent[8]=0x00;
	tempsent[9]=0x61;
	for(i=0;i<15;i++)
	{
		tempsent[10+i]=p[i];
	}
	for(i=0;i<25;i++)
	{
	checksum+=tempsent[i];
   // printf("%x ",tempsent[i]);
	}

	tempsent[25]=(checksum&0xff00)>>8;
	tempsent[26]=(checksum&0x00ff);

	packbuffer[0]=0xc0;
	j=1;
	for(i=0;i<(tempsent[1]+2);i++)
	{
		if(tempsent[i]==0xc0)
		{
			packbuffer[j]=0xdb;
			j++;
			packbuffer[j]=0xdc;
		}
		else
		{
			if(tempsent[i]==0xdb)
			{
				packbuffer[j]=0xdb;
				j++;
				packbuffer[j]=0xdd;
			}
			else
				packbuffer[j]=tempsent[i];
		}
		j++;
	}
	packbuffer[j]=0xc0;
	j++;
	sendsum=j;
	return 1;
}

//void analysedata()
void analysedata(unsigned char *p)
{
	if(rece_flag>0)

	{

				int i=0,j=0,dh=0,k=0,w=0,g=0,t=0,s=0;

		unsigned char jd[50],wd[50],time[20],time1[5],wd1[15],wd2[30],jd1[15],jd2[30];
		unsigned int time4,time5;
		double wd3,wd4,wd6;
		double jd3,jd4,jd6;

		unsigned char time2[5],time3[5],time6[5];
		unsigned char xx1[10],yy1[10];
		unsigned char wd5[10],jd5[10],gd[10],star[10];
			  int able=0;
		long int x1=0,y1=0;
		int gd1=0; // fa song cheng xu

		for(i=0;i<100;i++)
			p[i]=0;
	   //	strcpy(Buffer,"$GPGGA,123806,3924.1049,N,11914.7684,E,1,05,4.3,822.2,M,-20.0,M,,*6A");
	   //	head=0;
	   // tail=strlen(Buffer);
		for(i=head+1,j=0;i<tail-1;i++,j++)
			p[j]=Buffer[i];
			  //	p="GPGGA,123806,3750.8796,N,11230.1536,E,1,05,4.3,822.2,M,-20.0,M,,*6A";

		head=0;
		tail=0;
		if(strncmp(p,gps,5)==0)
		{ debug++;
                // printf("%s\n",p);
		memset(wd,0,50);
		memset(wd1,0,15);
		memset(wd2,0,30);

		memset(jd,0,50);
		memset(jd1,0,15);
		memset(jd2,0,30);

		memset(gd,0,10);
		memset(time,0,20);
		memset(time1,0,5);
		memset(time2,0,5);
		memset(time3,0,5);
		memset(time6,0,5);
		memset(star,0,6);

			for(i=0;i<j;i++)
			{
				if(p[i]==',')
					dh++;
				if((dh==1)&&(p[i]!=','))
				{
				  time[t]=p[i];
					t++;

				}
				if((dh==2)&&(p[i]!=','))

				{
				  wd[w]=p[i];
				  w++;
				}
				if((dh==4)&&(p[i]!=','))
				{
					jd[k]=p[i];
					 k++;
				}
							   if((dh==6)&&(p[i]!=','))
							   {   char aa[2];
								   aa[0]=p[i];
									 able=atoi(aa);
							   }
				   if((dh==7)&&(p[i]!=','))
							   {
				   star[s]=p[i];
				   s++;
				}
				if((dh==9)&&(p[i]!=','))
				{
					gd[g]=p[i];
					g++;
				}
			 }
		   /*	 if(debug==10)
			 gd="";*/
	 /*		 if(available=='0')
			 debug--; */
			time1[0]=time[0];
			time1[1]=time[1];
			time2[0]=time[2];
			time2[1]=time[3];
			time3[0]=time[4];
			time3[1]=time[5];
			time4=atoi(time1);
			time5=(time4+8)%24;
			itoa(time5,time6,10);

			wd1[0]=wd[0];
			wd1[1]=wd[1];
			wd2[0]=wd[2];
			wd2[1]=wd[3];
			wd2[2]=wd[4];
			wd2[3]=wd[5];
			wd2[4]=wd[6];
			wd2[5]=wd[7];
			wd2[6]=wd[8];
			wd6=atof(wd1);
			wd3=atof(wd2);
			wd4=wd3/60+wd6;
			gcvt(wd4,7,wd5);
			wd5[7]='\0';

			jd1[0]=jd[0];
			jd1[1]=jd[1];
			jd1[2]=jd[2];
			jd2[0]=jd[3];
			jd2[1]=jd[4];
			jd2[2]=jd[5];
			jd2[3]=jd[6];
			jd2[4]=jd[7];
			jd2[5]=jd[8];
			jd2[6]=jd[9];
			jd6=atof(jd1);
			jd3=atof(jd2);
			jd4=jd3/60+jd6;
			gcvt(jd4,7,jd5);
						double xxx2,yyy2;
					   //	wd4=39.4017;
					   // jd4=119.24614;
						LB_XY(0,6,jd4,wd4,&xxx2,&yyy2,0);
						xxx2/=1000.0;
						yyy2/=1000.0;
				gcvt(xxx2,7,xx1);
			gcvt(yyy2,8,yy1);
			//gcvt(LBx(jd4,wd4),7,xx1);
			//gcvt(LBy(jd4,wd4),8,yy1);
			double xxx1,yyy1;
		   //	xxx1=LBx(jd4,wd4);
		   xxx1=xxx2;
			xxx1*=1000;
		   //	yyy1=LBy(jd4,wd4);
		   yyy1=yyy2;
			yyy1*=1000;
			if(strncmp(wd5,wd5_1,10)!=0)
			{
			setcolor(9);
			outtextxy(115,205,wd5_1);
			}

			if(strncmp(jd5,jd5_1,10)!=0)
			{
			setcolor(9);
			outtextxy(115,245,jd5_1);
			}

			if(strncmp(gd,gd_1,10)!=0)
			{
			setcolor(9);
			outtextxy(140,285,gd_1);
			outtextxy(420+20,285,gd_1);
			}

			if(strncmp(star,star_1,10)!=0)
			{
			setcolor(9);
			outtextxy(170,325,star_1);

			 }
			if(strncmp(xx1,xx1_1,10)!=0)
			{
			setcolor(9);
			outtextxy(410,205,xx1_1);
			}

			if(strncmp(yy1,yy1_1,10)!=0)
			{
			setcolor(9);
			outtextxy(410,245,yy1_1);
			}

			if(strncmp(time3,time3_1,5)!=0)
			{
			setcolor(9);
			outtextxy(230,165,time3_1);
			}

			if(strncmp(time2,time2_1,5)!=0)
			{
			setcolor(9);
			outtextxy(180,165,time2_1);
			}

			if(strncmp(time6,time6_1,5)!=0)
			{
			setcolor(9);
			outtextxy(130,165,time6_1);
			}
			if(old!=able)
			{
			setcolor(9);
			outtextxy(500+10,325+6,"*");
			}

			setcolor(10);
			outtextxy(140,285,gd);
		   outtextxy(420+20,285,gd);
			outtextxy(170,325,star);
			outtextxy(115,205,wd5);
			outtextxy(115,245,jd5);
			outtextxy(130,165,time6);
			outtextxy(180,165,time2);
			outtextxy(230,165,time3);
			outtextxy(164,165,":");
			outtextxy(213,165,":");
			outtextxy(410,205,xx1);
			outtextxy(410,245,yy1);
//			outtextxy(500,325+6,"[");
//			outtextxy(500+20,325+6,"]");
			if((able==1)||(able==6))
			outtextxy(500+10,325+6,"*");


			x1=xxx1; //fa song cheng xu
			y1=yyy1; //fa song cheng xu

			gd1=atoi(gd);

			strcpy(gd_1, gd);
			strcpy(star_1,star);
			strcpy(wd5_1,wd5);
			strcpy(jd5_1,jd5);
			strcpy(time6_1,time6);
			strcpy(time2_1,time2);
			strcpy(time3_1,time3);
			strcpy(xx1_1,xx1);
			strcpy(yy1_1,yy1);
			old=able;


  //	int x1=4191,y1=19632,gd1=812;
	int x2=0,x3=0,x4=0,x5=0,x6=0,x7=0,x8=0,x9=0,x10=0,x11=0;
	int x12=0,x13=0,x14=0,x15=0;


	x2=x1/16777216;
	x3=(x1%16777216)/65536;
	x4=((x1%16777216)%65536)/256;
	x5=((x1%16777216)%65536)%256;

	x6=y1/16777216;
	x7=(y1%16777216)/65536;
	x8=((y1%16777216)%65536)/256;
	x9=((y1%16777216)%65536)%256;

	x10=gd1/256;
	x11=gd1%256;
	 unsigned char x[15];
	x[0]=0xfe,x[1]=0xf1;
	x[2]=x2,x[3]=x3,x[4]=x4,x[5]=x5;
	x[6]=x6,x[7]=x7,x[8]=x8,x[9]=x9;
	x[10]=x10,x[11]=x11;

	struct time t;
	int t1,t2,t3,t4;
	gettime(&t);
	t1=t.ti_hour;
	t2=t.ti_min;
	t3=t.ti_sec;
	t4=t.ti_hund;
	   if(debug==300)
	   {
		 t.ti_hour=time5;
		 t.ti_min=atoi(time2);
		 t.ti_sec=atoi(time3);
		 settime(&t);
		 debug=0;
		}
	x12=t1%256;
	x13=t2%256;
	x14=t3%256;
	x15=t4%256;

	x[12]=x12,x[13]=x13,x[14]=x14,x[15]=x15;
   pack_data(x);

	send_flag=1;
		}
	rece_flag--;

	}


}
void sendpack()
{
	unsigned char unChar;
	int b=0;
  for(b=0;b<sendsum;b++)
	 {
	  unChar=packbuffer[b];
	send_char(unChar);
	 }
	sendsum=0;
}

void send_char(unsigned char unch)
 {
  while( ((inp(0x3f8+5)&0x40)==0));
   outportb(0x3f8,unch);
// delay(1);

 }

double LBx(double LL,double BB)
{
	double P=206264.80625;
	double E2=0.006693421623;
	double A=6378245.00;
	double E12=0.006738525415;
	double m2,n,t,L11,xb;
	int L0,i,dhlb=0;
	double xx;

  i=(int)LL/6+1;
  if(dhlb==i-1)
	i=i-1;
  else if(dhlb==i+1)
	i=i+1;
  L0=i*6-3;
  L11=(LL-L0)*3600;
  BB=BB*3.1415926/180;
  t=tan(BB);
  m2=E12*pow(cos(BB),2);
  n=A/sqrt(1-E2*pow(sin(BB),2));
  xb=111134.8611*BB/3.1415926*180;
  xb=xb-sin(BB)*cos(BB)*(32005.7799+133.9238*pow(sin(BB),2)+0.6973*pow(sin(BB),4)+0.0039*pow(sin(BB),6));
  xx=(n*L11*L11*sin(BB)*cos(BB)/(2*P*P))*(1+pow(cos(BB)*L11/P,2));
  xx=xb+xx*(5-t*t+9*m2+4*pow(m2,2))/12+pow(cos(BB)*L11/P,4)*(61-58*t*t+pow(t,4)/360);
  xx=xx/1000;
  return xx;
}

double LBy(double LL,double BB)
{
   double P=206264.80625;
   double E2=0.006693421623;
   double A=6378245.00;
   double E12=0.006738525415;
   double m2,n,t,L11,xb;
  int L0,i,dhlb=0;
  double yy;
  i=(int)LL/6+1;
  if(dhlb==i-1)
	i=i-1;
  else if(dhlb==i+1)
	i=i+1;
  L0=i*6-3;
  L11=(LL-L0)*3600;
  BB=BB*3.1415926/180;
  t=tan(BB);
  m2=E12*pow(cos(BB),2);
  n=A/sqrt(1-E2*pow(sin(BB),2));
  yy=500000+n*cos(BB)*L11/P*(1+pow(cos(BB)*L11/P,2)*(1-t*t+m2)/6+
	 pow(cos(BB)*L11/P,4)*(5-18*t*t+pow(t,4)+14*m2-58*m2*t*t)/120);
  yy=(yy+i*1000000)/1000;
  return yy;
}

//高斯投影正算公式
//输入量:DK=6 表示六度带,DK=3表示三度带
//        TH:参考椭球号:TH=0表示克拉索夫斯基椭球,TH=1表示IUGG-75椭球
//        LO:已知点的经度值
//        B:已知点的纬度值
//        DH:带号,缺省为0,由程序内计算
//输出量:X,Y:高斯投影的平面直角坐标值
#define PPP 57.2957795
void LB_XY(int TH,int DK,double LO,double B,double *X,double *Y,int DH)
{
	  double L0,L,T,E,V,N,Q1,Q2,Q3,Q4,S,X0,X1,Y1,c;

	  if(DK==6)
	  {
		 if(DH==0)DH=(int)(LO/DK)+1;
		 L0=DH*DK-3;
		 }
	  else
	  {
		 if(DH==0)DH=int((LO-1.5)/DK)+1;//INT-->int
		 L0=DH*DK;
	  }

	  L =LO-L0;
	  S =sin(B/PPP);
	  T =tan(B/PPP);
	  if(TH==0){
		X0=111134.8611*B-S*cos(B/PPP)*(32005.7799+133.9238*S*S+0.6973*S*S*S*S+0.0039*S*S*S*S*S*S);
		E=0.00673852541468;//e'^2
		c=6399698.9018;
	  }
	  else{
		X0=111133.0047*B-S*cos(B/PPP)*(32009.8575+133.9602*S*S+0.6976*S*S*S*S+0.0039*S*S*S*S*S*S);
		E=0.006739501819473;//e'^2
		c=6399596.651988;
	  }
	  E =sqrt(E)*cos(B/PPP);//e'*cosB
	  V =sqrt(1+E*E);  //sqrt(1+(e'^2*cosB^2)
	  N =c/V;  //N=c/V

	  Q1=0.5+(5-T*T+9*E*E+4*E*E*E*E)*(cos(B/PPP)*L/PPP)*(cos(B/PPP)*L/PPP)/24;
	  Q2=(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))/720*(61-58*T*T+T*T*T*T);
	  X1=X0+N*T*(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(Q1+Q2);
	  Q3=1+(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(1-T*T+E*E)/6;
	  Q4=(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(L/PPP*cos(B/PPP))*(5-18*T*T+T*T*T*T+14*E*E-58*E*E*T*T)/120;
	  Y1=N*cos(B/PPP)*L/PPP*(Q3+Q4);

	  *X =X1;
	  *Y =Y1+DH*1000000+500000;
}

void main()
{
	int graphdriver=VGA;
	int graphmode=VGAHI;

	initgraph(&graphdriver,&graphmode,"");

	cleardevice();
  /*if ((checkfp=fopen("check.txt","w"))==NULL)
	{
		printf("Can't open file check.txt!\n");
		exit(0);

	}*/
		clearviewport();
		setcolor(10);
		setfillstyle(1,9);
		setlinestyle(0,0,3);
		rectangle(0,0,639,479);
		floodfill(10,10,12);
		settextstyle(0,0,2);

		setfillstyle(SOLID_FILL,LIGHTRED);
		bar(1,100,900,102); //zhong heng
		bar(1,0,900,4);  //shang
		bar(1,475,900,479);     //xia

		bar(320,100,322,900); //zhong shu
		bar(0,0,4,900);
		bar(635,0,639,900);

		setcolor(14);
		outtextxy(240,50,"GPS");
		out24hz(290,45,2,14,"数据显示");

		out24hz(50,160,2,14,"时间");
		out24hz(50,200,2,14,"纬度");
		out24hz(50,240,2,14,"经度");
		out24hz(50,280,2,14,"高度");
		out24hz(50,320,2,14,"卫星数");

		out24hz(250,200,2,14,"度");
		out24hz(250,240,2,14,"度");
		out24hz(250,280,2,14,"米");
		out24hz(250,320,2,14,"颗");

		out24hz(400,160,2,14,"大地直角坐标");
		outtextxy(370,205,"X:");
		outtextxy(370,245,"Y:");
		outtextxy(370,285,"Z:");
		outtextxy(500-10,325+6,"[");
			outtextxy(500+30+3,325+6,"]");

			out24hz(370,325,2,14,"数据有效");

		out24hz(580-15,202,2,14,"千米");
		out24hz(580-15,242,2,14,"千米");
//		out24hz(580-15,282,2,14,"千米");
		out24hz(580-15,282,2,14,"米");

		int k=0;
		memset(wd5_1,0,10);
		memset(jd5_1,0,10);
		memset(gd_1,0,10);
		memset(star_1,0,10);

  OpenPort();

  do
  { analysedata(data);

		 if(send_flag==1)
	 {
	   sendpack();

		send_flag=0;
		}
	  if(bioskey(1)!=0)
	  k=bioskey(0);
  }while(k!=0x011b);

  //	fclose(checkfp);
	ClosePort();
	closegraph();

}

⌨️ 快捷键说明

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