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

📄 gsmgps9.cpp

📁 串口接收GPS数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:

//read gps in com1;
//read gsm in com2

#include <dos.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include "gserial2.h"

//#define VERSION 0x0101
#define FALSE           0
#define TRUE            1
#define NO_ERROR         0       /* No error               */
#define BUF_OVFL         1       /* Buffer overflowed      */


//#define ESC             0x1B    /* ASCII Escape character */
//#define ASCII           0x007F  /* Mask ASCII characters  */
//#define SBUFSIZ         0x4000  /* Serial buffer size     */
//#define AKEY            0x41

//int SError=NO_ERROR;
int selfmadegps=TRUE;
int gpspro=FALSE;

char STRGPGGA[100];
char STRGPRMC[100];
int gpscount=0;

//static   char  ccbuf[SBUFSIZ];
//unsigned int   startbuf        = 0;
//unsigned int   endbuf          = 0;
//char c;
//char   value1[50];
char GpsBuf[100];
char tempchar[3]="0";

//int            portbase        = 0;
int test;

void clearstring(char* a,int l)
{ int i;
  for(i=0;i<=l-1;i++)
  a[i]='\0';
}

void   interrupt(*oldvects[2])(...);

struct PA
{ double lon;
  double lat;
// int gpsstate;
// long timenow;
  double freq;
  double output;
  double hu;
	 char slon[30];
	 char slat[30];
	 char gpsstatus;
	 char gpstime[12];
 } mypa;

/* Handle communications interrupts and put them in ccbuf */
void interrupt com1_int(...)
{    disable();
     char c;
     static int EOT_flag=1;//end of a fram flag,set to 1 to indicate a end of frame
	 if ((inportb(COM1BASE + IIR) & IIR_MASK) == IIR_RX_ID)
    {    c= inportb(COM1BASE + RXR);
		 if (c=='$' /*&& EOT_flag*/)
		  {      EOT_flag=0;
			*GpsBuf='\0';
//			 clearstring(tread,45);
		  }
		  tempchar[0]=c;
		  strcat(GpsBuf,tempchar);
		 if (c=='\n'&&EOT_flag==0)
		  {
		     if ((strstr(GpsBuf,"GPGGA")-GpsBuf)==1)
			{ *STRGPGGA='\0';
//			  clearstring(STRGPGGA,100);
			  strcpy(STRGPGGA,GpsBuf);
			  //fprintf (stdout,"interrupt");
			  //gpsworkflag=1;
			  //GpsShow=1;
	    //		  printf("gps is here");
			  gpscount++;
			  EOT_flag=1;
			}
/*			if (strstr(tread,"GPRMC")!=NULL)
		  { clearstring(STRGPRMC,100);
			 strcpy(STRGPRMC,tread);
			// gpsworkflag=1;
			// GpsShow=1;
			 EOT_flag=1;
		       }*/
		 }
    }
    /* Signal end of hardware interrupt */
    outportb(PIC8259_ICR, PIC8259_EOI);
    enable();
}

void interrupt com2_int(...)
{
 int temp;
	disable();
	temp = (inportb(COM2BASE+IIR)) & IIR_MASK;				// why interrupt was called
	switch(temp)
	{   case 0x00:  // modem status changed
			inportb(COM2BASE+MSR);   // read in useless char
			break;
		/*case 0x02:  // Request To Send char
			if (outhead != outtail)							// there's a char to send
			{
				outportb(portbase+TXR,outbuf[outhead++]);	// send the character
				if (outhead == OBUF_LEN)
					outhead=0;								// if at end of buffer, reset pointer
			}
			break;*/
		case 0x04:  // character ready to be read in
			inbuf[inhead++] = inportb(COM2BASE+RXR);// read character into inbuffer
			if (inhead == IBUF_LEN) // if at end of buffer
				inhead=0;           // reset pointer
			break;
		case 0x06:  // line status has changed
			inportb(COM2BASE+LSR);     // read in useless char
			break;
		default:
			break;
	}
	outportb(PIC8259_ICR, PIC8259_EOI);	// Signal end of hardware interrupt
	enable();				// reenable interrupts at the end of the handler
}


 char* stringcopy(char* a,int start,int end)
   {static char temp[20];
     char s;
     int i;
//     clearstring(temp,50);
     for (i=start;i<=end;i++)
     {temp[i-start]=a[i];
     }
  temp[end-start+1]='\0';
   // strcpy(a,temp);
  return temp;
   }

double getvalue(char* a,int i)
{  char temp[20];
   char temp2[20];
   char temp1[20];
   double tempa;
   double tempb;
   double tempc;
   clearstring(temp,20);
   clearstring(temp1,20);
//down is main swich to set different value
  switch(i)
  { case 1:
	  if (selfmadegps)
	  {
	   strcpy(temp,stringcopy(a,14,22));
	   strcpy(temp2,temp);
      //	   printf("\nprint the temp of gpgga lat:%s",STRGPGGA);
	   strcpy(temp1,stringcopy(temp,0,1));
    //	    printf("\nprint the string of temp:%s",temp1);
	   tempa=atof(temp1);
    //  	   printf("\nthe value of tempa is :%s",temp1);
	   strcpy(temp1,stringcopy(temp2,2,8));
	   tempb=atof(temp1);
	   tempc=tempa+tempb/60.0;
    //	   printf("\nprint lat:%lf",tempc);
	   }
    /*	   if (gpspro)
	   {strcpy(temp,stringcopy(a,17,26));
	   strcpy(temp2,temp);
	   strcpy(temp1,stringcopy(temp,0,1));
	   tempa=atof(temp1);
	   strcpy(temp1,stringcopy(temp2,2,9));
	   tempb=atof(temp1);
	   tempc=tempa+tempb/60.0;
	   }*/
	   break;
    case 2:
	  if (selfmadegps)
	  {
	  strcpy(temp,stringcopy(a,26,35));
	  strcpy(temp2,temp);
	  strcpy(temp1,stringcopy(temp,0,2));
	  tempa=atof(temp1);
	  strcpy(temp1,stringcopy(temp2,3,9));
	  tempb=atof(temp1);
	  tempc=tempa+tempb/60.0;
	  }
	 /* if (gpspro)
	  { strcpy(temp,stringcopy(a,30,40));
	    strcpy(temp2,temp);
	    strcpy(temp1,stringcopy(temp,0,2));
	    tempa=atof(temp1);
	    strcpy(temp1,stringcopy(temp2,3,10));
	    tempb=atof(temp1);
	    tempc=tempa+tempb/60.0;
	  }*/
	  break;
/*   case 3:strcpy(temp,stringcopy(a,39,39));
	  tempc=atof(temp);
	  break;
   case 4:
	 strcpy(temp8,stringcopy(a,7,12));
	  tempc=atof(temp8);*/
   }
return tempc;
}

 char getgpsstate(char* a)
  { char temp[3];
   strcpy(temp,stringcopy(a,39,39));
   return temp[0];
   }

  char* getgpstime(char* a)
  {static  char temp[10];
   char temp1[3]="0";
   unsigned int hour;
   strcpy(temp,stringcopy(a,7,8));
      //  printf("the temp is :%s\n",temp);
   hour=atoi(temp);
   if (hour<=8)
    { hour+=8;
    itoa(hour,temp,10);}
     else
    { hour-=16;
      itoa(hour,temp,10);
      strcat(temp1,temp);
      strcpy(temp,temp1);
      }
   strcat(temp,":");
   strcat(temp,stringcopy(a,9,10));
   strcat(temp,":");
   strcat(temp,stringcopy(a,11,12));
      //  printf("the string is :%s\n",temp);
   return temp;
  }


void readgpsstring()
{
  double value1,value2;   //for longitude.
  long value3,value4;
  char temps1[20],temps4[20],temps2[20],temps3[20];
  value1=mypa.lon;
  value1=value1*1000000;
  value3=(long)value1;
  value4=value3;
  ltoa(value3,temps1,10);
  ltoa(value4,temps4,10);

  strcpy(temps2,stringcopy(temps1,0,2));
  temps2[3]='.';
  temps2[4]='\0';
  strcpy(temps3,stringcopy(temps4,3,8));
  temps3[6]='\0';
  strcat(temps2,temps3);
  strcpy(mypa.slon,temps2);

  value1=mypa.lat;         //for latitude.
//  printf("\nprint mypa.lat:%lf\n",mypa.lat);
  value1=value1*1000000;
  value3=(long)value1;
  value4=value3;
  ltoa(value3,temps1,10);
  ltoa(value4,temps4,10);

  strcpy(temps2,stringcopy(temps1,0,1));
  temps2[2]='.';
  temps2[3]='\0';
  strcpy(temps3,stringcopy(temps4,2,7));
  temps3[6]='\0';
  strcat(temps2,temps3);
  strcpy(mypa.slat,temps2);
 }

void readgps()
{
	mypa.lon=getvalue(STRGPGGA,2);
	mypa.lat=getvalue(STRGPGGA,1);
	mypa.gpsstatus=getgpsstate(STRGPGGA);
	strcpy(mypa.gpstime,getgpstime(STRGPGGA));
	     //mypa.gpsstate=int(getvalue(STRGPGGA,3));
	    //	mypa.timenow=(long)getvalue(STRGPGGA,4);
	    //	mypa.timenow=long(getvalue(STRGPRMC,4));
	readgpsstring();
	      //	 printf("the string of gpstime is:%s",mypa.gpstime);
	    // printf("the status of GPS is :%c",mypa.gpsstatus);
}


/*  get status of the port  */
int read_status_com(int portn)
{  return(inp(portn+5));
}

/*  send one valid char from the port  */
void send_char_com(int portn,unsigned char cc)
{  while ((read_status_com(portn) & 0x40) == 0);
   outportb(portn,cc);
}

/*  send one string from the port  */
void send_string_com(int portn,unsigned char *buf) //revised on 03.4.23.
{ 	int k;	k=0;
	int strlength=strlen(buf);
   do { send_char_com(portn,*(buf + k));
		k++;
	  } while ((k < strlength));
}

void readch(char &ch)
{  if (inhead != intail)     // there is a character
	{	disable();                          // disable irqs while getting char
		ch = inbuf[intail++];               // get character from buffer
		if (intail == IBUF_LEN)				// if at end of in buffer
			intail=0;						// reset pointer
		enable();				   // re-enable interrupt	                            
	}
       else	ch = -1;	
}

/* Install our functions to handle communications */
void setvects(int comport)
{  	switch (comport)
	{case COM1BASE:
		oldvects[0] = getvect(0x0C);
	    setvect(0x0C, com1_int);

⌨️ 快捷键说明

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