//读取GPS设备信息
//2013-10-29
//yang
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<termios.h>
int main()
{
int fd=0,ret=0;
char *buf=NULL,*hd=NULL,*ed=NULL,*hd2=NULL,*ed2=NULL,chr[250],chr2[250];
struct termios old,new;
fd = open("/dev/ttyUSB0",O_RDONLY);
if(fd == -1)
{
perror("open");
return 1;
}
int g = tcgetattr(fd,&old);
if(g == -1)
{
printf("COM get fail\n");
return 1;
}
new = old;
cfsetispeed(&new,B4800);
cfsetospeed(&new,B4800);
new.c_cflag |= (CLOCAL | CREAD);
new.c_cflag &= ~PARENB;
new.c_cflag &= ~CSTOPB;
new.c_cflag &= ~CSIZE; //取消原有设置
new.c_cflag |= CS8;
tcsetattr(fd,TCSANOW,&new); //立即生效
buf = (char *)malloc(500);
if(buf == NULL)
{
printf("malloc fail\n");
return 1;
}
while(1)
{
sleep(1);
memset(buf,'\0',500);
memset(chr,'\0',250);
memset(chr2,'\0',250);
ret = 0;
while(ret<499)
{
ret += read(fd,buf+ret,499-ret); //读取500个字节
//printf("ret===%d\n",ret);
if(ret <= 0)
{
printf("no fand message exit\n");
return 1;
}
}
hd = strstr(buf,"GGA");
if(hd == NULL)
{
continue;
}
ed = strchr(hd,'*');
if(ed == NULL)
{
continue;
}
hd2 = strstr(buf,"RMC");
if(hd2 == NULL)
{
continue;
}
ed2 = strchr(hd2,'*');
if(ed2 == NULL)
{
continue;
}
memcpy(chr,hd,ed-hd+1);
memcpy(chr2,hd2,ed2-hd2+1);
printf("%s\n",chr);
printf("%s\n",chr2);
out(chr,chr2);
}
free(buf);
}
int out(char *p,char *q)
{
int hour=0,mine=0,socret=0,um=0;
char chr[20],ch[5];
memset(chr,'\0',20);
memset(ch,'\0',5);
sscanf(q,"%4s%2d%2d%2d%3d",chr,&hour,&mine,&socret,&um);
int hours=hour+8;
if(hours >= 24)
hours = hours-24;
printf("\t\tnow time is %02d:%02d:%02d\n",hours,mine,socret);
memset(chr,'\0',20);
char *m=(strchr(q,'N')-10);
memcpy(chr,m,9);
//puts(chr);
sscanf(chr,"%2d%2d%1s%4d",&hour,&mine,ch,&socret);
printf("北纬%02d度%02d分%04d秒\n",hour,mine,socret);
memset(chr,'\0',20);
m=(strchr(q,'E')-11);
memcpy(chr,m,10);
sscanf(chr,"%3d%2d%1s%4d",&hour,&mine,ch,&socret);
//printf("%d\n",hour);
printf("东经%03d度%02d分%04d秒\n",hour,mine,socret);
memset(chr,'\0',20);
m=(strchr(p,'M')-6);
memcpy(chr,m,5);
printf("海拔高度是%.1f\n",atof(chr));
memset(chr,'\0',20);
m=(strrchr(q,'A')-9); //最后一个A的位置减9
memcpy(chr,m,6);
puts(chr);
sscanf(chr,"%2d%2d%2d",&socret,&mine,&hour);
if(hours > 24)
socret += 1;
printf("当前日期是20%d年%d月%d日\n",hour,mine,socret);
}
GPS
最新推荐文章于 2022-06-07 21:59:53 发布