测试板子使用ARDUINO MEGA2560
#include <TimerOne.h>
#define GPSSerial Serial1
#define DebugSerial Serial
const unsigned int bufferLength = 600;
char RxBuffer[bufferLength];
unsigned int ii = 0;
char EER_GPS_Buffer[24] = {"$GPRMC,,V,,,,,,,,,,N*7F"};
struct
{
unsigned char CMD;
unsigned int LengthA;
char fixed[20];
unsigned char id_length;
char id[20];
unsigned char LengthB1;
unsigned char LengthB2;
char api_key[50];
} login;
struct
{
unsigned char CMD;
unsigned char LengthA;
unsigned char mark;
unsigned char type;
unsigned char LengthB1;
unsigned char LengthB2;
char streamA[24];
char id[20];
char streamB[27];
char GPS_Buffer[150];
char streamC[7];
} Save_Data;
void setup(){
GPSSerial.begin(9600);
DebugSerial.begin(9600);
Timer1.initialize(1000);
Timer1.attachInterrupt(Timer1_handler);
DebugSerial.println("setup end!");
}
void loop(){
DebugSerial.println("loop start!");
getGPSData();
DebugSerial.println("loop end!");
}
unsigned long Time_Cont = 0;
void Timer1_handler(void)
{
Time_Cont++;
}
void getGPSData(){
char *GPS_BufferHead, *GPS_BufferTail;
clrRxBuffer();
Time_Cont = 0;
while (Time_Cont < 2000)
{
readBuffer();
if ((GPS_BufferHead = strstr(RxBuffer, "$GPRMC,")) != NULL)
{
if ((GPS_BufferTail = strstr(GPS_BufferHead, "
")) != NULL)
{
if(GPS_BufferTail > GPS_BufferHead)
{
memcpy(Save_Data.GPS_Buffer, GPS_BufferHead, GPS_BufferTail - GPS_BufferHead);
DebugSerial.println("-----1------");
DebugSerial.println(Save_Data.GPS_Buffer);
break;
clrRxBuffer();
}
}
}
}
if(Time_Cont >= 2000)
{
memcpy(Save_Data.GPS_Buffer, EER_GPS_Buffer, 24); //输出错误信息
}
DebugSerial.println("-----2------");
DebugSerial.println(Save_Data.GPS_Buffer);
memset(Save_Data.GPS_Buffer, 0, sizeof Save_Data.GPS_Buffer); //使用后清空数据
DebugSerial.println("-----3------");
DebugSerial.println(Save_Data.GPS_Buffer);
clrRxBuffer();
}
void readBuffer(){
while (GPSSerial.available())
{
RxBuffer[ii++] = GPSSerial.read();
if (ii == bufferLength)clrRxBuffer();
}
}
void clrRxBuffer(void)
{
memset(RxBuffer, 0, bufferLength); //清空
ii = 0;
}