Delphi中利用MSCOMM控制項進行GPS資料獲取 |
|
jackkcg
站務副站長 發表:891 回覆:1050 積分:848 註冊:2002-03-23 發送簡訊給我 |
http://www.yesky.com/20020516/1611460.shtml
Delphi中利用MSCOMM控制項進行GPS資料獲取
2002-05-16· ·楊輝 薛向鋒··yesky 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)爲按鈕“接收”添加代碼: 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);
begin
if application.MessageBox('您真的想退出嗎?','GPS輸入',mb_okcancel)=idok then begin
timer1.Enabled:=false;
if mscomm1.PortOpen then
mscomm1.PortOpen:=false;
gpsstrlist.Free;
action:=cafree;
end
else
action:=canone;
end; 3、討論 本文利用MSCOMM控制項成功地接收到了GPS的定位資料,效果良好,在實際應用中,由於GPS的資料處理比資料獲取速度要慢,微機和GPS的通信有可能阻塞,且在系統中一台微機可能要接收多個GPS接收機的定位資料,所以應當考慮採用多線程機制,以避免資源衝突。 本程式在Windows 2000(CHN)和DELPHI 5.0下通過。 *********************************************************
哈哈&兵燹
最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知
K.表Knowlege 知識,就是本站的標語:Open our mind to make knowledge together!
希望能大家敞開心胸,將知識寶庫結合一起
------
********************************************************** 哈哈&兵燹 最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知 K.表Knowlege 知識,就是本站的標語:Open our mind |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |