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

📄 gsmgps9.cpp

📁 串口接收GPS数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		break;
	case COM2BASE:
		oldvects[1] = getvect(0x0B);
		setvect(0x0B, com2_int);
	default:  break;
	}
}

/* Uninstall our vectors before exiting the program */
void resvects(int comport)
{	switch (comport)
	{case COM1BASE:
		setvect(0x0C, oldvects[0]);
		break;
	case COM2BASE:
		setvect(0x0B, oldvects[1]);
	default:  break;
	}
}

/* Tell modem that we're ready to go */
void serial::comm_on(void)
{   unsigned  char temp, pnum;
	disable();
	temp = inportb(portbase + MCR) | 0x0f;//MCR_INT;
	outportb(portbase + MCR, temp);
	temp = (inportb(portbase + IER)) | IER_RX_INT;//|IER_TX_INT;
	outportb(portbase + IER, temp);
	pnum = (portbase == COM1BASE ? COM1 : COM2);
	temp = inportb(PIC8259_IMR) & (pnum == COM1 ? IRQ4 : IRQ3);
	outportb(PIC8259_IMR, temp);
 //	temp = inportb(portbase + MCR) | MCR_DTR | MCR_RTS;
 //	outportb(portbase + MCR, temp);
	enable();
}

void serial::comm_off(void)
{   unsigned char  temp;   //revised on 03.4.24. the type of the variant is modified.
    disable();
	temp = inportb(PIC8259_IMR) ;
	outportb(PIC8259_IMR, temp| ~IRQ3 | ~IRQ4);
	outportb(portbase + IER, 0);
	outportb(portbase + MCR, 0);
	enable();
}

void serial::init_serial(void)
{
    //endbuf = startbuf = 0;
	int comport;
	comport=portbase;
	setvects(comport);
	comm_on();
}

serial::~serial()
{   int comport; 
	comport=portbase;
    comm_off();
    resvects(comport);
}

/* Set the port number to use */
int serial::SetPort(int Port)
{
    int   Offset, far *RS232_Addr;
    switch (Port)
    { /* Sort out the base address */
      case COM1 : Offset = 0x0000;
		  break;
      case COM2 : Offset = 0x0002;
		  break;
      default   : return (-1);
    }
	RS232_Addr = (int far *)MK_FP(0x0040, Offset);  /* Find out where the port is. */
    if (*RS232_Addr == NULL) return (-1);/* If NULL then port not used. */
    portbase = *RS232_Addr;              /* Otherwise set portbase      */
    return (0);
}

/* This routine sets the speed; will accept funny baud rates. */
/* Setting the speed requires that the DLAB be set on.        */
int serial::SetSpeed(int Speed)
{
    char	c;
    int		divisor;
    if (Speed == 0)            /* Avoid divide by zero */
	return (-1);
    else
	divisor = (int) (115200L/Speed);

    if (portbase == 0)
	return (-1);
    disable();
    c = inportb(portbase + LCR);
    outportb(portbase + LCR, (c | 0x80)); /* Set DLAB */
    outportb(portbase + DLL, (divisor & 0x00FF));
    outportb(portbase + DLH, ((divisor >> 8) & 0x00FF));
    outportb(portbase + LCR, c);          /* Reset DLAB */
    enable();
    return (0);
}

/* Set other communications parameters */
int serial::SetOthers(int Parity, int Bits, int StopBit)
{    int       setting;
    if (portbase == 0)				
		return (-1);
    if (Bits < 5 || Bits > 8)			
		return (-1);
    if (StopBit != 1 && StopBit != 2)		
		return (-1);
    if (Parity !=LCR_NO_PARITY && Parity != LCR_ODD_PARITY && Parity != LCR_EVEN_PARITY)
		return (-1);
    setting  = Bits-5;
    setting |= ((StopBit == 1) ? 0x00 : 0x04);
    setting |= Parity;

    disable();
    outportb(portbase + LCR, setting);
    enable();
    return (0);
}

/* Set up the port */
serial::serial(int Port, int Speed, int Parity, int Bits, int StopBit)
{
	flag = 0;
	if (SetPort(Port))
	  flag = -1;
	if (SetSpeed(Speed))
	  flag = -1;
	if (SetOthers(Parity, Bits, StopBit))
	  flag = -1;
	if (!flag)
	   init_serial();
}

/*  Control-Break interrupt handler */
int c_break(void)
{    int temp;
	disable();
    temp = inportb(PIC8259_IMR) | ~IRQ3 | ~IRQ4;
	outportb(PIC8259_IMR, temp);

	outportb(COM1BASE + IER, 0);
	outportb(COM2BASE+IER,0);

	outportb(COM1BASE + MCR, 0);
    outportb(COM2BASE + MCR, 0);
    enable();
    fprintf(stderr, "\nStill online.\n");
    return(0);
}


void swap(char* x,char* y)
{   	char temp;
	temp=*x;
	*x=*y;
	*y=temp;
}	

int msgsend(char *phoneno,char *outstr)
{       char  c=0;
       unsigned int loop1=0;
	//unsigned long int loop2=0;
	   unsigned int    strglen,strcodelen,outlen;
	   unsigned char    outlenstr[3];
	   unsigned char    outstring[288];
	   unsigned char    strglenstr[3];
	   unsigned char tempchar[2];
	   char phonechar[15];
	      // char sendhead[338]="0891683108200105f011000B813119162416F80004A9";
	      //char sendhead[338]="0891683108200105F011000B813118288033F90004A9";	   
	      // char sendhead[338]="0891683108200105F011000B813118789490F40008A9";
	       char sendhead[338]="0891683108200105F011000B81";
	   char cmdhead[13]="AT+CMGS=";
	   strcpy(phonechar,phoneno);
	   if (strlen(phonechar)>140)
		   return (3);

       {   int i;                            // edcoding the phonenumber.
	   for (i=0;i<5;i++)
		   {
	     swap(&phonechar[2*i],&phonechar[2*i+1]);
		   }
	    phonechar[11]=phonechar[10];
	    phonechar[10]='F';
	    phonechar[12]='\0';
       }
   //       printf("the phoneno is :%s!",phonechar);

	   strglen=strlen(outstr);
	   strcodelen=2*strglen; //strcodelen=4*strglen ,in unicode encoding.
	   outlen=14+strglen;  //outlen=14+2*strglen, in unicode encoding.

	   itoa(outlen,outlenstr,10);
	   strcat(cmdhead,outlenstr);
	   strcat(cmdhead,"\x0D");

	   int i, j;
	   for (i=0;i<=strcodelen;i++)
		  outstring[i]='\0';
	   for (j=0;j<strglen;j++)
	      {             // outstring[4*j]='0';   for unicode encoding programing.
					    // outstring[4*j+1]='0';  for unicode encoding programing.
		    itoa(outstr[j]/16,tempchar,16);
		  outstring[2*j] =tempchar[0]; // outstring[4*j+2] =tempchar[0] ,in unicode encoding programing.
		    itoa(outstr[j]%16,tempchar,16);
		  outstring[2*j+1]=tempchar[0];  // outstring[4*j+2] =tempchar[0], in unicode encoding programing
	       }

	   itoa(strglen,strglenstr,16);       // itoa(strglen*2,strglenstr,16); in unicode encoding programing.
	       if (strglen<8)
		  //if the char is less than 16(10H),a "0" is need in the front of the char.
		  { unsigned char head[3]="0";
		    strcat(head,strglenstr);
		    strcpy(strglenstr,head);
		   }
		strcat(sendhead,phonechar);
		strcat(sendhead,"0004A9");      //0008 for 8bit code ,0004 for UNICODE .
	   strcat(sendhead,strglenstr);
	   strcat(sendhead,outstring);
	   strcat(sendhead,"\x1A");   //ctrl-z.


	printf("the length the string you writed for sent is :%d",strlen(outstr));
	printf("\nthe string is \"%s\"\n",outstr);

       //	delay(100);
	/*In the main loop,a defined message is sent when any key is pressed.
	  you can exit the program once ESC is press.also, you can change the
	  messages you want to send. */

			    // if (!done)   comport<<c;  //send single char with overlay function...
			    //send_char_com(portbase, c);  //seng single char in a commnway....
		   send_string_com(COM2BASE,cmdhead);
			  // send_string_com(portbase,"AT+CMGS=15\x0D");    //for test with fixed chacter.
		   printf("the head is send\n");

		//delay(100);
		do
		{	readch(c);
				loop1++;
		       //	printf("printf c is %c,loop is %d!\n",c,loop1);
				if (kbhit())
				 return(1);
		}while (c!='>'&&c!='E'&&loop1<=65500);
		fprintf(stdout,"%u\n",loop1);
		if (c=='>')
		   {  send_string_com(COM2BASE,sendhead);
			 //send_string_com(portbase,"0891683108200105F011000B813118789490F40000A9014E\x1a");
			  //for test with fixed chacter.
			  return(0);
			}
		else return(2);
	     /*  do
	       {if (c !=-1)     //'-1' is the END signal of a string
		  {   //fputc(c & ASCII, stdout);
		     // fprintf(stdout,"%c\t",c);
		   }
		    readch(c);
		    loop2++;
		    // fprintf(stdout,"%d-",loop2);
	       }while(c!=':');
		 //printf("%d\n",loop2);
	       if (c==':')
		    return(0);
	      else return(-1);  */
}

main()
{       int        port1    =COM1;
	int        port2    = COM2;
	int        speed    = 9600;
	int        parity   =LCR_NO_PARITY;
	int        bits     = 8;
	int        stopbits = 1;
	int        gsmtest;
	char phonenumber[15]="13818749094";
//	int        done  = FALSE;
	clrscr();
	serial comport1(port1, speed, parity, bits, stopbits);
	serial comport2(port2, speed, parity, bits, stopbits);
	ctrlbrk(c_break);

	struct date d;struct time t;
	fprintf(stdout, "C++ program for GSM commnication!-----8bit coding programing...\n\n"
			"press any KEY to send defined message!\n"
			"press [ESC] to quit...\n\n\n");
      gettime(&t); getdate(&d);
      printf("\nthe current time is--%d.%d.%d,%02d:%02d:%02d:%02d\n",d.da_year,d.da_mon,d.da_day,t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);
      while(1)
	{ 	if (gpscount==20)
		{  	readgps();
			 //  delay(100);
			char  GsmOutstr[142]="GSMGPS test--GPS real-time signal(SENDED by GSM)--ONCE per 20sec:Lat--";
			 strcat(GsmOutstr,mypa.slat);
			 strcat(GsmOutstr,";Lon--");
			 strcat(GsmOutstr,mypa.slon);
			   strcat(GsmOutstr,";GMTtime(Transfered by gpstime)--");
			 strcat(GsmOutstr,mypa.gpstime);

      //			 printf("the time-defined gpsstring:%s!\n",GsmOutstr);
			 printf("the gps status is :%c\n",mypa.gpsstatus);

		gsmtest=msgsend(phonenumber,GsmOutstr);
			 gpscount=0;
			 //gettime(&t);getdate(&d);
			//printf("the current time is--%d.%d.%d,%02d:%02d:%02d:%02d\n\n",d.da_year,d.da_mon,d.da_day,t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);
			//	break;
		}
		//if (gpscount==3)
		if (kbhit())
		    break;
	}
	      gettime(&t); getdate(&d);
	      printf("the current time is--%d.%d.%d,%02d:%02d:%02d:%02d\n",d.da_year,d.da_mon,d.da_day,t.ti_hour,t.ti_min,t.ti_sec,t.ti_hund);
	switch ( gsmtest)
	{
	 case 0: printf("msg is send successfully!");break;
	 case 1:printf ("msg sending is halted"); break;
	 case 2:printf ("command is  not acknowedged");break;
	 case 3:printf ("the message writed is too long!");break;
	 case -1:printf("error happened when string sending!");break;
	}

	/* Check for errors */    //there is no usage.
	switch (SError)
	{     	case NO_ERROR: fprintf(stderr, "\nbye.\n");
					  return (0);
		case BUF_OVFL: fprintf(stderr, "\nBuffer Overflow.\n");
					  return (99);
		default:      fprintf(stderr, "\nUnknown Error, SError = %d\n", SError);
					  return (99);
	}

}

⌨️ 快捷键说明

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