collect GPS data using micro controller XAG-49 C code
时间:03-30
整理:3721RD
点击:
Hello
I have c code which collect the GPS data using XAG-49 micro-controller but it gives some garbage values after SGPRMC string. It shows till last 'W' and than send me some garbage values. Can any one tell me what is the problem in my code?
#include <REGXAG49.H> // call the library function for the microcontroler to define the register
void function(); //function declaration
void uart_init(); //function declaration
void uart_sendbyte(unsigned char *messageOne); //function declaration
void uart_send(unsigned char ch); //function declaration
int uart_readbyte(); //function declaration
int uart_read(); //function declaration
unsigned char mess[80]; // global variable
const char CR = 0x0D; //carriage return
const char LF = 0x0A; // line feed (New line)
const char BB = 0x22; // symbol "
const char CTRZ= 0x1A; // control Z
sbit P10=P1^0; //RTS signal
unsigned char buffer1[70]; // global variable
unsigned char position[70]; // global variable
unsigned char gpsdata[70]; // global variable
void gpscatch(); //function declaration
void uart1_init(); //function declaration
int readbyte();
int read();
int gpsfilter();
void function() // Modem function start
{
uart_sendbyte("AT~"); // send string AT
uart_send(CR); //carriage return
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
uart_sendbyte("AT+CMGF=1~"); // send string AT+CMGF=1 (set modem in text mode)
uart_send(CR); //carriage return
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
uart_sendbyte("AT+CMGS=~"); // send string AT+CMGS( for send message)
uart_send(BB); //send symbol "
uart_sendbyte("+447412067332~"); // send phone number +447412067332
uart_send(BB); // send symbol "
uart_send(CR); //carriage return
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
uart_sendbyte(gpsdata); // send message buffer
uart_send(CTRZ); // send to the modem control Z
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
}
void uart_init() // baud rate function
{
SCON = 0x53; // serial register read and write
TMOD = 0x20; // timer 1 in mode 2
RTL1 = 238; // transmit speed 9600 bps
TL1 = 238; // transmit speed 9600 bps
TR1 = 1; // start timer 1
}
void uart_sendbyte(unsigned char *messageOne) // send stirng function
{
int i=0; // local variable declaration
while(messageOne[i]!='~') // compare the value of the buffer to '~'
{
uart_send(messageOne[i]); // call send function
i++; // increase the value of the buffer
}
}
void uart_send(unsigned char ch) // uart send function
{
S0BUF = ch; // send character
while(!TI0); // wait until the flag bit TI0 is set
TI0=0; // clear the flag
}
int uart_readbyte() // uart store modem answer
{
unsigned int buffer[30]; // buffer for the answer
signed int b=-1; // local variable declaration
do{ // loop do
b++; // increase local variable
buffer[b]=uart_read(); // call uart read function and store answer
if(buffer[b]==62) // control the buffer value >
{
return 0;
break;
} // end if
}while(buffer[b]!=10); // control the buffer value LF
return 0; // end function
}
int uart_read() // uart read function
{
int k=1; // local varibale declaration
while(!RI0); // wait till the RI0 bit is set
RI0=0; // clear the flag bit RI0
k=S0BUF; // read out the receive buffer
return k; // return the buffer value
}
void gpscatch() //start GPS catch function
{
uart1_init(); //define transfer speed
readbyte(); //call function readbyte
}
void uart1_init()
{
S1CON = 0x53; //define serial mode 1
T2MOD = 0x30; // Timer 2 define as 16 bit timer
T2CAPH = 0xFF; //value for the Higherbyte FF
T2CAPL = 0xDC; // value for lowbyte DC transfer speed 4800 bps
TR2 = 1; // start timer 2
}
int readbyte() //start readbyte function
{
signed int b=-1, j=0, m,a=0; //local variable declaration
for(m=0; m<=70;m++)
{
buffer1[m]=0;
position[m]=0;
}
do{
b++;
buffer1[b]=read();
if((buffer1[b-2]=='R') && (buffer1[b-1]=='M') && (buffer1[b]=='C'))
{
position[j]=buffer1[b-5];
position[j+1]=buffer1[b-4];
position[j+2]=buffer1[b-3];
position[j+3]=buffer1[b-2];
position[j+4]=buffer1[b-1];
position[j+5]=buffer1[b];
for(j=6; j<68; j++)
{
position[j]=read();
P2=position[j];
if(position[j]=='E')
{
a++;
if(a==1)
{
j=0;
return 0;
}
}
}
}
}while(1);
return 0;
}
int read()
{
int k=1;
while(!RI1);
RI1=0;
k=S1BUF;
return k;
}
int gpsfilter()
{
int z=0, x=0, i,a=0;
P10=0;
for(i=0; i<70; i++)
{
gpsdata[i]=0;
}
while(1)
{
while((position[x-4]=='S')&&(position[x-1]=='R')&&(position[x]=='M'))
{
gpsdata[z]=position[x-4];
gpsdata[z+1]=position[x-3];
gpsdata[z+2]=position[x-2];
gpsdata[z+3]=position[x-1];
for(z=4;z<=70;z++)
{
gpsdata[z]=position[x];
if(position[x]=='E')
{
a++;
if(a==1)
{
gpsdata[68]='0';
gpsdata[69]='0';
gpsdata[70]='\0';
return 0;
}
}
x++;
}
}
x++;
}
return 0;
}
void main () // the execution function ( main function) no return value cause (void)
{
WDCON = 0; // watchdog off
WFEED1 = 0xA5;
WFEED2 = 0x5A;
gpscatch();
gpsfilter();
uart_init();
while(1)
{
function();
}
}
I have c code which collect the GPS data using XAG-49 micro-controller but it gives some garbage values after SGPRMC string. It shows till last 'W' and than send me some garbage values. Can any one tell me what is the problem in my code?
#include <REGXAG49.H> // call the library function for the microcontroler to define the register
void function(); //function declaration
void uart_init(); //function declaration
void uart_sendbyte(unsigned char *messageOne); //function declaration
void uart_send(unsigned char ch); //function declaration
int uart_readbyte(); //function declaration
int uart_read(); //function declaration
unsigned char mess[80]; // global variable
const char CR = 0x0D; //carriage return
const char LF = 0x0A; // line feed (New line)
const char BB = 0x22; // symbol "
const char CTRZ= 0x1A; // control Z
sbit P10=P1^0; //RTS signal
unsigned char buffer1[70]; // global variable
unsigned char position[70]; // global variable
unsigned char gpsdata[70]; // global variable
void gpscatch(); //function declaration
void uart1_init(); //function declaration
int readbyte();
int read();
int gpsfilter();
void function() // Modem function start
{
uart_sendbyte("AT~"); // send string AT
uart_send(CR); //carriage return
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
uart_sendbyte("AT+CMGF=1~"); // send string AT+CMGF=1 (set modem in text mode)
uart_send(CR); //carriage return
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
uart_sendbyte("AT+CMGS=~"); // send string AT+CMGS( for send message)
uart_send(BB); //send symbol "
uart_sendbyte("+447412067332~"); // send phone number +447412067332
uart_send(BB); // send symbol "
uart_send(CR); //carriage return
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
uart_sendbyte(gpsdata); // send message buffer
uart_send(CTRZ); // send to the modem control Z
uart_send(LF); // line feed (New line)
uart_readbyte(); // read modem answer (response)
}
void uart_init() // baud rate function
{
SCON = 0x53; // serial register read and write
TMOD = 0x20; // timer 1 in mode 2
RTL1 = 238; // transmit speed 9600 bps
TL1 = 238; // transmit speed 9600 bps
TR1 = 1; // start timer 1
}
void uart_sendbyte(unsigned char *messageOne) // send stirng function
{
int i=0; // local variable declaration
while(messageOne[i]!='~') // compare the value of the buffer to '~'
{
uart_send(messageOne[i]); // call send function
i++; // increase the value of the buffer
}
}
void uart_send(unsigned char ch) // uart send function
{
S0BUF = ch; // send character
while(!TI0); // wait until the flag bit TI0 is set
TI0=0; // clear the flag
}
int uart_readbyte() // uart store modem answer
{
unsigned int buffer[30]; // buffer for the answer
signed int b=-1; // local variable declaration
do{ // loop do
b++; // increase local variable
buffer[b]=uart_read(); // call uart read function and store answer
if(buffer[b]==62) // control the buffer value >
{
return 0;
break;
} // end if
}while(buffer[b]!=10); // control the buffer value LF
return 0; // end function
}
int uart_read() // uart read function
{
int k=1; // local varibale declaration
while(!RI0); // wait till the RI0 bit is set
RI0=0; // clear the flag bit RI0
k=S0BUF; // read out the receive buffer
return k; // return the buffer value
}
void gpscatch() //start GPS catch function
{
uart1_init(); //define transfer speed
readbyte(); //call function readbyte
}
void uart1_init()
{
S1CON = 0x53; //define serial mode 1
T2MOD = 0x30; // Timer 2 define as 16 bit timer
T2CAPH = 0xFF; //value for the Higherbyte FF
T2CAPL = 0xDC; // value for lowbyte DC transfer speed 4800 bps
TR2 = 1; // start timer 2
}
int readbyte() //start readbyte function
{
signed int b=-1, j=0, m,a=0; //local variable declaration
for(m=0; m<=70;m++)
{
buffer1[m]=0;
position[m]=0;
}
do{
b++;
buffer1[b]=read();
if((buffer1[b-2]=='R') && (buffer1[b-1]=='M') && (buffer1[b]=='C'))
{
position[j]=buffer1[b-5];
position[j+1]=buffer1[b-4];
position[j+2]=buffer1[b-3];
position[j+3]=buffer1[b-2];
position[j+4]=buffer1[b-1];
position[j+5]=buffer1[b];
for(j=6; j<68; j++)
{
position[j]=read();
P2=position[j];
if(position[j]=='E')
{
a++;
if(a==1)
{
j=0;
return 0;
}
}
}
}
}while(1);
return 0;
}
int read()
{
int k=1;
while(!RI1);
RI1=0;
k=S1BUF;
return k;
}
int gpsfilter()
{
int z=0, x=0, i,a=0;
P10=0;
for(i=0; i<70; i++)
{
gpsdata[i]=0;
}
while(1)
{
while((position[x-4]=='S')&&(position[x-1]=='R')&&(position[x]=='M'))
{
gpsdata[z]=position[x-4];
gpsdata[z+1]=position[x-3];
gpsdata[z+2]=position[x-2];
gpsdata[z+3]=position[x-1];
for(z=4;z<=70;z++)
{
gpsdata[z]=position[x];
if(position[x]=='E')
{
a++;
if(a==1)
{
gpsdata[68]='0';
gpsdata[69]='0';
gpsdata[70]='\0';
return 0;
}
}
x++;
}
}
x++;
}
return 0;
}
void main () // the execution function ( main function) no return value cause (void)
{
WDCON = 0; // watchdog off
WFEED1 = 0xA5;
WFEED2 = 0x5A;
gpscatch();
gpsfilter();
uart_init();
while(1)
{
function();
}
}
hrough whether ... the GPS data is sent to the server?
and in the form of whether the data transmitted?
you need to change the gps data hex format to the coordinates before being sent to the server...