Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14490157
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:48:34

下载本文示例代码
  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部分添加以下声明 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 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);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下通过。   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部分添加以下声明 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 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);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下通过。 下载本文示例代码


Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集Delphi中利用MSCOMM控件进行GPS数据采集
阅读(160) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~