if(buff[0]=='$'&&buff_num>0){//一个GPS信号接收已经完成,处理接受的GPS信息(打印)
//清除缓存和缓存计数,继续接受GPS信号
buff_num=0;
memset(buff,0,sizeof(buff));
}
buff[buff_num++]=read_buf[0];//记录GPS信息开始符
goto delay; //延时1秒钟
}
else{ //接收到非$字符
if(buff[0]=='$')//已经存在首字符,则添加后续字符
buff[buff_num++]=read_buf[0];
continue;//如果开头的$不存在,放弃该字符
}
}
else{
//printf("Receive....\n");
continue;
}
delay:
sleep(1);
}
}
int speed_arr[] = {B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed){
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
=================采集数据举例===========================
Receive....
$GPGGA,003841.000,3604.9074,N,10427.3777,E,2,06,1.5,107.1,M,-14.7,M,2.8,0000*50
$GPRMC,003841.000,A,3604.9074,N,10427.3777,E,0.00,201.43,180211,,,D*65
$GPVTG,201.43,T,,M,0.00,N,0.0,K,D*0C
$GPGGA,003842.000,3604.9074,N,10427.3777,E,2,06,1.5,107.1,M,-14.7,M,1.8,0000*50
$GPGSV,3,1,12,04,69,352,19,10,61,194,23,17,51,116,25,02,45,285,15*7F
$GPRMC,003842.000,A,3604.9074,N,10427.3777,E,0.00,201.43,180211,,,D*66