GSMGPS9.CPP
上传用户:b2s168
上传日期:2007-10-24
资源大小:9k
文件大小:18k
- //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]=' ';
- }
- 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=' ';
- // clearstring(tread,45);
- }
- tempchar[0]=c;
- strcat(GpsBuf,tempchar);
- if (c=='n'&&EOT_flag==0)
- {
- if ((strstr(GpsBuf,"GPGGA")-GpsBuf)==1)
- { *STRGPGGA=' ';
- // 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]=' ';
- // 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 :%sn",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 :%sn",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]=' ';
- strcpy(temps3,stringcopy(temps4,3,8));
- temps3[6]=' ';
- strcat(temps2,temps3);
- strcpy(mypa.slon,temps2);
- value1=mypa.lat; //for latitude.
- // printf("nprint mypa.lat:%lfn",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]=' ';
- strcpy(temps3,stringcopy(temps4,2,7));
- temps3[6]=' ';
- 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);
- 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]=' ';
- }
- // 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]=' ';
- 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=15x0D"); //for test with fixed chacter.
- printf("the head is sendn");
- //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,"%un",loop1);
- if (c=='>')
- { send_string_com(COM2BASE,sendhead);
- //send_string_com(portbase,"0891683108200105F011000B813118789490F40000A9014Ex1a");
- //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,"%ct",c);
- }
- readch(c);
- loop2++;
- // fprintf(stdout,"%d-",loop2);
- }while(c!=':');
- //printf("%dn",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...nn"
- "press any KEY to send defined message!n"
- "press [ESC] to quit...nnn");
- gettime(&t); getdate(&d);
- printf("nthe current time is--%d.%d.%d,%02d:%02d:%02d:%02dn",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 :%cn",mypa.gpsstatus);
- gsmtest=msgsend(phonenumber,GsmOutstr);
- gpscount=0;
- //gettime(&t);getdate(&d);
- //printf("the current time is--%d.%d.%d,%02d:%02d:%02d:%02dnn",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:%02dn",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 = %dn", SError);
- return (99);
- }
- }