GPS C语言程序gisant 发表于 2005-3-18 9:16:57 //#####################gps.c#############################include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<stdlib.h>#include<termios.h>#include<fcntl.h>int quart(int fp); main(){ int j,fp; int max=50; unsigned long tmp; double tt; unsigned char ch[512]; unsigned char cc[32]; struct termios opt; fp=open("/dev/ttyS0",O_RDWR);//以读写方式打开串口1//设置串口:波特率9600,8位数据位,奇校验、1位停止位 tcgetattr(fp,&opt); tcflush(fp,TCIOFLUSH); cfsetispeed(&opt,B9600); cfsetospeed(&opt,B9600); opt.c_iflag=1; opt.c_oflag=0; opt.c_cflag=3261; opt.c_lflag=0; opt.c_line=' '; tcsetattr(fp,TCSANOW,&opt); tcflush(fp,TCIOFLUSH); while(!quart(fp)); //等待帧标志: @@Ha printf("ok!\n"); for(j=0;j<max;j++) //*******读gps M12数据直到收到max个数据******* { read(fp,cc,1); ch[j]=cc[0]; printf("%x",ch[j]); } printf("\n"); ////////////////////////////日期时间//////////////////////// printf("month,day,year:%d,%d,%d\n",ch[0],ch[1],ch[2]*256+ch[3]); printf("hour,minute,sencond:%d,%d,%d\n",ch[4]+8,ch[5],ch[6]); ///////////////////纬度///////////////////////////////////////// tmp=ch[27]*256*256*256+ch[28]*256*256+ch[29]*256+ch[30]; tmp=ch[11]*256*256*256+ch[12]*256*256+ch[13]*256+ch[14]; printf("latitude:%8.3f\n",tmp/324000000.0*90); tt=tmp/324000000.0*90; ///////////////////经度//////////////////////////////////////// tmp=ch[31]*256*256*256+ch[32]*256*256+ch[33]*256+ch[34]; tmp=ch[15]*256*256*256+ch[16]*256*256+ch[17]*256+ch[18]; printf("longitude:%8.3f\n",tmp/648000000.0*180); FILE *ff; struct gps { int day; int month; int year; int hour; int min; int sec; double wd; double jd;}gps; gps.day=ch[1]; gps.month=ch[0]; gps.year=ch[2]*256+ch[3]; gps.hour=ch[4]+8; gps.min=ch[5]; gps.sec=ch[6]; gps.wd=tt; gps.jd=tmp/648000000.0*180;ff=fopen("a1.txt","a+");fprintf(ff,"\t%d %d, %d\n",gps.day,gps.month,gps.year);fprintf(ff,"\t%d:%d:%d\n",gps.hour,gps.min,gps.sec);fprintf(ff,"\t%f\n",gps.wd);fprintf(ff,"\t%f\n\n",gps.jd); fclose(ff);}//main//####################################################int quart(int fp) //fp是IO口句柄,如果收到@@Ha标志,返回1,否则0; { int status=0;char cc[8],tmp; read(fp,cc,1); tmp=cc[0]; if(tmp=='@') { read(fp,cc,1); tmp=cc[0]; if(tmp=='@') { read(fp,cc,1); tmp=cc[0]; if(tmp=='H') { read(fp,cc,1); tmp=cc[0]; if(tmp=='a') status=1; } }}return status;

评论