日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Delphi利用MSCOMM控件进行GPS数据采集

發(fā)布時(shí)間:2025/3/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Delphi利用MSCOMM控件进行GPS数据采集 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

1、準(zhǔn)備
  GPS(Global Positioning System),即全球定位系統(tǒng),利用GPS衛(wèi)星的測(cè)距和測(cè)時(shí)功能進(jìn)行全球定位,在許多系統(tǒng)中,如機(jī)場(chǎng)導(dǎo)航系統(tǒng),出租車輛管理和調(diào)度系統(tǒng)、江河流域的災(zāi)害信息管理和預(yù)測(cè)系統(tǒng)中,GPS得到了廣泛的應(yīng)用。本文利用MSCOMM控件實(shí)現(xiàn)了GPS數(shù)據(jù)的采集,可為信息管理和指揮調(diào)度等提供定位數(shù)據(jù)。

  本文采用GPS的異步串行傳送方式,將GARMIN 12C按NMEA-0183協(xié)議輸出的數(shù)據(jù)采集到了微機(jī),并將接收到的地理坐標(biāo)轉(zhuǎn)換成為直角坐標(biāo)。

  在DELPHI 中新建一工程,名為GPSReceiver,在主界面上放置四個(gè)TEDIT控件,用于顯示接收到的地理坐標(biāo)和轉(zhuǎn)換后的直角坐標(biāo),其“name”屬性分別為:Editlatitude、Editlongitude、Editxcoord和Editycoord;在四個(gè)TEDIT控件下方放置三個(gè)按鈕,用于數(shù)據(jù)接收的控制和退出程序,其“Caption”屬性分別為“接收”、“斷開(kāi)”和“返回”;在界面上任意位置放置一個(gè)Ttimer控件、其屬性interval值為“1000”,主要用于每隔一秒接收一次GPS數(shù)據(jù);一個(gè)TMSCOMM控件。程序運(yùn)行后的界面如圖1(略):

  2、編寫(xiě)代碼

  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; //默認(rèn)串口1
mscomm1.InBufferSize:=1024;
mscomm1.Settings:='600,n,8,1'; //波特率為600
if not mscomm1.PortOpen then
mscomm1.PortOpen:=true; //打開(kāi)串口
mscomm1.InBufferCount:=0;
mscomm1.RThreshold:=512;
mscomm1.InputLen:=0;
timer1.Enabled:=false; //關(guān)閉定時(shí)器
a:=6378245.0; b:=6356863.0; //參考橢球的長(zhǎng)短軸
firstpxl:=(a*a-b*b)/a/a; //第一偏心率
secondpxl:=(a*a-b*b)/b/b; //第二偏心率
end;


  3)為定時(shí)器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; //縱坐標(biāo)
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; //橫坐標(biāo)
editxcoord.Text:=formatfloat('00000.00',x);
editycoord.Text:=formatfloat('00000.00',y);
end;


  4)為按鈕“接收”添加代碼:

messagebeep(1);
editlatitude.Text:=''; //接收前先清除顯示內(nèi)容
editlongitude.Text:='';
editycoord.Text:='';
editxcoord.Text:='';
timer1.Enabled:= true; //打開(kāi)計(jì)時(shí)器

  5)為按鈕“斷開(kāi)”添中代碼:

messagebeep(1);
editlatitude.Text:=''; //清除顯示內(nèi)容
editlongitude.Text:='';
editycoord.Text:='';
editxcoord.Text:='';
timer1.Enabled:= false; //關(guān)閉計(jì)時(shí)器

  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的定位數(shù)據(jù),效果良好,在實(shí)際應(yīng)用中,由于GPS的數(shù)據(jù)處理比數(shù)據(jù)采集速度要慢,微機(jī)和GPS的通信有可能阻塞,且在系統(tǒng)中一臺(tái)微機(jī)可能要接收多個(gè)GPS接收機(jī)的定位數(shù)據(jù),所以應(yīng)當(dāng)考慮采用多線程機(jī)制,以避免資源沖突。

總結(jié)

以上是生活随笔為你收集整理的Delphi利用MSCOMM控件进行GPS数据采集的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。