1、准备
GPS(Global Positioning
System),即全球定位系统,利用24颗GPS卫星的测距和测时功能进行全球定位,在许多系统中,如机场导航系统,出租车辆管理和调度系统、江河流域的灾害信息管理和预测系统中,GPS得到了广泛的应用。本文利用MSCOMM控件实现了GPS数据的采集,可为信息管理和指挥调度等提供定位数据。
本文采用GPS的异步串行传送方式,将GARMIN
12C按NMEA-0183协议输出的数据采集到了微机,并将接收到的地理坐标转换成为直角坐标。
在DELPHI 5.0
IDE中新建一工程,名为GPSReceiver,在主界面上放置四个TEDIT控件,用于显示接收到的地理坐标和转换后的直角坐标,其“name”属性分别为:Editlatitude、Editlongitude、Editxcoord和Editycoord;在四个TEDIT控件下方放置三个按钮,用于数据接收的控制和退出程序,其“Caption”属性分别为“接收”、“断开”和“返回”;在界面上任意位置放置一个Ttimer控件、其属性interval值为“1000”,主要用于每隔一秒接收一次GPS数据;一个TMSCOMM控件。程序运行后的界面如图1所示:
 图1 |
2、编写代码
1) 在FORM的implementation部分添加以下声明
var nn,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); begin mscomm1.CommPort:=1; //默认串口1 mscomm1.InBufferSize:=1024; mscomm1.Settings:='600,n,8,1'; //波特率为600 if not mscomm1.PortOpen then mscomm1.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); var i:integer; tmpstr,strq,strq1,strq2,tmpstr1,tmpstr2:string; latitudestr,longitudestr:string;
begin
i:=0;
gpsstr:=mscomm1.input;
gpsstrcpy:=gpsstr; while strlen(pchar(gpsstr))>0 do begin
tmpstr:=copy(gpsstr,pos(#10,gpsstr),pos(#13,gpsstr)-1); delete(gpsstr,1,pos(#10,gpsstr));
if copy(tmpstr,1,pos(',',tmpstr)-1)='$GPRMC'
then begin
delete(tmpstr,1,pos(',',tmpstr));//,strlen(pchar(tmpstr))-pos(',',tmpstr)+1);
delete(tmpstr,1,pos(',',tmpstr));
if copy(tmpstr,1,pos(',',tmpstr)-1)='A'
then begin
delete(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)为按钮“接收”添加代码:<
br> <
br>
<
tr>
messagebeep(1);
editlatitude.Text:=
'';//接收前先清除显示内容
editlongitude.Text:='';
editycoord.Text:= '';<
br> editxcoord.Text:= '';
timer1.Enabled:= true;
//打开计时器 | <
/table> <
br> 5)为按钮“断开”添中代码:
messagebeep(1);
editlatitude.Text:=''; //清除显示内容
editlongitude.Text:='';
editycoord.Text:=
'';
editxcoord.Text:=
'';
timer1.Enabled:= false;
//关闭计时器
|
6)为按钮“返回”添加代码:
<
tbody><
tr>
procedure Tmainfrm.FormClose(Sender:
TObject; var Action: TCloseAction);
begin
if
application.MessageBox('您真的想退出吗?','GPS输入',mb_okcancel)=idok then
begin
timer1.Enabled:= false;
if mscomm1.PortOpen
then
mscomm1.PortOpen:=
false;<
br> gpsstrlist.Free;
action:=
cafree;
end
else<
br> action:=canone;
end; | <
/table>
3、讨论<
/b>
本文利用MSCOMM控件成功地接收到了GPS的定位数据,效果良好,在实际应用中,由于GPS的数据处理比数据采集速度要慢,微机和GPS的通信有可能阻塞,且在系统中一台微机可能要接收多个GPS接收机的定位数据,所以应当考虑采用多线程机制,以避免资源冲突。<
br>
本程序在Windows
2000(CHN)和DELPHI5.0下通过。
|