图1
2、编写代码
1) 在FORM的implementation部分添加以下声明
varnn,x,y,sm,n,weidud,jinchad,firstpxl,secondpxl,a,b,longitude,longitudemargin,latitude:double;weidustr,weidustrcpy,longitudestr1cpy,longitudestrccpy,weidustr1,weidustr2,jinchastr,jinchastr1,jinchastr2,longitudestr1,longitudestr11,longitudestr12,longitudestrc,longitudestrc1,longitudestrc2:string;gpsstrlist:tstringlist;gpsstr,gpsstrcpy:string;gpsstrlen:integer;
2)在FORM的FORMSHOW事件中添加如下代码:
procedure Tmainfrm.FormShow(Sender: TObject);beginmscomm1.CommPort:=1; //默认串口1mscomm1.InBufferSize:=1024;mscomm1.Settings:='600,n,8,1'; //波特率为600if not mscomm1.PortOpen thenmscomm1.PortOpen:=true; //打开串口mscomm1.InBufferCount:=0;mscomm1.RThreshold:=512;mscomm1.InputLen:=0;timer1.Enabled:=false; //关闭定时器a:=6378245.0; b:=6356863.0; //参考椭球的长短轴firstpxl:=(a*a-b*b)/a/a; //第一偏心率secondpxl:=(a*a-b*b)/b/b; //第二偏心率end;
3)为定时器Timer1添加OnTimer事件添加如下代码:
procedure Tmainfrm.Timer1Timer(Sender: TObject);vari:integer;tmpstr,strq,strq1,strq2,tmpstr1,tmpstr2:string;latitudestr,longitudestr:string;begini:=0;gpsstr:=mscomm1.input;gpsstrcpy:=gpsstr;while strlen(pchar(gpsstr))0 do begintmpstr:=copy(gpsstr,pos(#10,gpsstr),pos(#13,gpsstr)-1);delete(gpsstr,1,pos(#10,gpsstr));if copy(tmpstr,1,pos(',',tmpstr)-1)='$GPRMC' then begindelete(tmpstr,1,pos(',',tmpstr));//,strlen(pchar(tmpstr))-pos(',',tmpstr)+1);delete(tmpstr,1,pos(',',tmpstr));if copy(tmpstr,1,pos(',',tmpstr)-1)='A' then begindelete(tmpstr,1,pos(',',tmpstr));latitudestr:=copy(tmpstr,1,pos(',',tmpstr)-1);editlatitude.Text:='';strq1:=copy(latitudestr,1,2); strq2:=copy(latitudestr,3,6);editlatitude.Text:=strq1+'°'+strq2+'''';weidud:=strtofloat(copy(latitudestr,1,2))+strtofloat(copy(latitudestr,3,6))/60;weidud:=weidud*3.141592654/180;delete(tmpstr,1,pos(',',tmpstr));delete(tmpstr,1,pos(',',tmpstr));longitudestr:=copy(tmpstr,1,pos(',',tmpstr)-1);longitude:=strtofloat(copy(longitudestr,1,3))+strtofloat(copy(longitudestr,4,6))/60;editlongitude.Text:='';strq1:=copy(longitudestr,1,3); strq2:=copy(longitudestr,4,6);editlongitude.Text:=strq1+'°'+strq2+'''';jinchad:=abs(longitude-strtoint(centerlongitudestr));jinchad:=jinchad*3.141592654/180;end;break;end;end;n:=a/sqrt(1-firstpxl*sin(weidud)*sin(weidud));sm:=6367558.496*weidud-16036.48*sin(2*weidud)+16.828* sin(4*weidud)-0.022*sin(6*weidud)+0.00003*sin(8*weidud);nn:=sqrt(firstpxl)*cos(weidud);x:=sm+n*sin(weidud)*cos(weidud)*jinchad*jinchad/2+n*jinchad* jinchad*jinchad*jinchad*sin(weidud)*cos(weidud)*cos(weidud)*cos(weidud)*(5- tan(weidud)*tan(weidud)+9*nn*nn+4*nn*nn*nn*nn)/24+n*power(jinchad,6)* sin(weidud)*power(cos(weidud),5)*(61-58*tan(weidud)*tan(weidud)+tan(weidud) *tan(weidud)*tan(weidud)*tan(weidud)+270*nn*nn*nn*nn-330*nn*nn*tan(weidud)*tan(weidud))/270; //纵坐标y:=n*jinchad*cos(weidud)+n*jinchad*jinchad*jinchad*cos(weidud)*cos(weidud)*cos(weidud)*(1-tan(weidud)*tan(weidud)+nn*nn)/6+n*power(jinchad,5)*power(cos(weidud),5)*(5-18*tan(weidud)*tan(weidud)+tan(weidud)*tan(weidud)*tan(weidud)*tan(weidud)+14*nn*nn-58*nn*nn*tan(weidud)*tan(weidud))/120; //横坐标editxcoord.Text:=formatfloat('00000.00',x);editycoord.Text:=formatfloat('00000.00',y);end;
4)为按钮接收添加代码:
messagebeep(1);editlatitude.Text:=''; //接收前先清除显示内容editlongitude.Text:='';editycoord.Text:='';editxcoord.Text:='';timer1.Enabled:= true; //打开计时器
5)为按钮断开添中代码:
messagebeep(1);editlatitude.Text:=''; //清除显示内容editlongitude.Text:='';editycoord.Text:='';editxcoord.Text:='';timer1.Enabled:= false; //关闭计时器
6)为按钮返回添加代码:
procedure Tmainfrm.FormClose(Sender: TObject; var Action: TCloseAction);beginif application.MessageBox('您真的想退出吗?','GPS输入',mb_okcancel)=idok then begintimer1.Enabled:=false;if mscomm1.PortOpen thenmscomm1.PortOpen:=false;gpsstrlist.Free;action:=cafree;endelseaction:=canone;end;
3、讨论
本文利用MSCOMM控件成功地接收到了GPS的定位数据,效果良好,在实际应用中,由于GPS的数据处理比数据采集速度要慢,微机和GPS的通信有可能阻塞,且在系统中一台微机可能要接收多个GPS接收机的定位数据,所以应当考虑采用多线程机制,以避免资源冲突。
本程序在Windows 2000(CHN)和DELPHI 5.0下通过。
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)