GPS

//读取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);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值