MATLAB解析GPS数据程序.docx

上传人:b****7 文档编号:10183433 上传时间:2023-02-09 格式:DOCX 页数:22 大小:29.97KB
下载 相关 举报
MATLAB解析GPS数据程序.docx_第1页
第1页 / 共22页
MATLAB解析GPS数据程序.docx_第2页
第2页 / 共22页
MATLAB解析GPS数据程序.docx_第3页
第3页 / 共22页
MATLAB解析GPS数据程序.docx_第4页
第4页 / 共22页
MATLAB解析GPS数据程序.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

MATLAB解析GPS数据程序.docx

《MATLAB解析GPS数据程序.docx》由会员分享,可在线阅读,更多相关《MATLAB解析GPS数据程序.docx(22页珍藏版)》请在冰豆网上搜索。

MATLAB解析GPS数据程序.docx

MATLAB解析GPS数据程序

%注:

本程序可直接在MATLAB 2017a中运行

%该脚本文件用于学习GPS数据得读取,需要做其她用途请自行修改代码

%本脚本文件得前面几行代码就是要设置得一些参数

%默认使用3(需视情况修改)

%波特率设为GPS模块默认得38400

%下面为程序源码

 clear

num_execute=100;%执行次数

num_SingleRead=150; %单次从串口读取得字节数(最好设置足够大(最低大概设为80),保证单次读取得数据包含一条完整得GPS数据)

Timedelay= 0、2;% 用于延时读取串口数据

BaudRate = 38400;%读取数据得波特率

Terminator='CR';

num_MaxTry =5;%打开串口得最多尝试次数

BytesAvailableFcnCount=1000;

%%设置参数

% delete(instrfindall);%串口打开失败时使用此句

%delete(s);clears %串口打开失败时使用此句

serial3= serial('3');

%串口设置

serial3、BytesAvailableFcnMode=’byte';

%serial3、InputBufferSize=38400;%输出波特率

serial3、BaudRate =BaudRate; %读入波特率

%serial3、OutputBufferSize= 1024;

serial3、BytesAvailableFcnCount =BytesAvailableFcnCount;

serial3、ReadAsyncMode='continuous’;

serial3、Terminator=Terminator;

%% 打开串口

count_opentimes = 1;

whilecontains(serial3、status,’closed') 〉0&& count_opentimes 〈num_MaxTry

   fopen(serial3);%打开串口

  count_opentimes=count_opentimes+1;

end

ifcontains(serial3、status,’open') < 1

 disp(’openfailed!

');

 return

end

%%读取并处理数据

%初始化

GPS_Data=GPS_Init();

while(num_execute 〉0)

GPS_DataStrs=fread(serial3,num_SingleRead,'char’);  %一次读出10个字符

 GPS_DataStrs=reshape(GPS_DataStrs,1,[]);

  GPS_DataStrs=split_str2strs(GPS_DataStrs);

 GPS_Data_tmp =get_GPS_specificData(GPS_DataStrs);

 GPS_Data=Updata_GPU_Data(GPS_Data,GPS_Data_tmp);

 show_GPS_Data(GPS_Data);

 pause(Timedelay);%延时

 num_execute = num_execute—1;

end

%fprintf(s,'abcd');  %给串口得发送数据

%fscanf(s);     %从串口得接收缓存读数据

%%关闭串口并删除相关数据

fclose(serial3); %关闭串口

delete(serial3);

clearserial3

%%  

%将字符串根据'\r\n'划分成多个子字符串,同时去掉首尾无用得残余字符串

functionout_strs = split_str2strs(StrData)

  ifcontains(class(StrData),’char')

  uint8(StrData);

  end

 record=get_pos_enterflag(StrData);

 ifStrData(1)==uint8('$')%开头为'$’得情况

  flag_start =1;

else

     ifsize(record,2)>0

   flag_start=record

(1)+2;

   else

  out_strs=cell(0,0);

  return

  end

 end

 ifStrData(end)==13

flag_end= length(StrData)—1;

 else

  ifsize(record,2)〉0

  flag_end=record(end)-1;

  end

end

  if flag_start〉=flag_end

 out_strs=cell(0,0);

 return

end

 StrData= StrData(flag_start:

flag_end);%截取有效数据,方便下面划分子字符串

 record=get_pos_enterflag(StrData);

  num_strs=size(record,2)+1;

 out_strs= cell(num_strs,1);

  ifnum_strs〉1

    out_strs{1,1}=char(StrData(1:

record

(1)—1));

  ifnum_strs ==2

 out_strs{num_strs,1}=char(StrData(record

(1)+2:

end));

    else

  for i=2:

 num_strs-1

       out_strs{i,1}=char(StrData(record(i—1)+2:

record(i)-1));

  end

 out_strs{num_strs,1}= char(StrData(record(i)+2:

end));

  end

else

   out_strs{1,1} =char(StrData);

  end

  %得到字符串中’\r\n’在字符串中得位置(实际为'\r’得位置)

functionrecord=get_pos_enterflag(data)

 record=[];%记录回车符号位置

 forii= 1:

length(data)—1

   ifdata(ii)==13

     if data(ii+1) == 10

     record=[record,ii];

      ii=ii+1;

      end

   end

  end

 end

end

%得到具体GPS结构体数据

functionGPS_Data_tmp=get_GPS_specificData(StrsData)

GPS_Data_tmp = [];

 num_str=size(StrsData,1);

 for i=1:

 num_str

  str_tab=StrsData{i,1};

   ifcontains(str_tab,'GGA’)〉0

     GPS_Data_tmp= GNGGA(str_tab);

   elseifcontains(str_tab,’GSA')>0

      GPS_Data_tmp=GNGSA(str_tab);

    elseifcontains(str_tab,'GSV’)>0

   GPS_Data_tmp =GNGSV(str_tab);

 elseifcontains(str_tab,’RMC’)〉 0

   GPS_Data_tmp =GNRMC(str_tab);

  elseifcontains(str_tab,’VTG’)〉 0

  GPS_Data_tmp=GNVTG(str_tab);

  elseif contains(str_tab,’GLL')>0

     GPS_Data_tmp =GNGLL(str_tab);

end

   end

end

% GPS字符串解析

functionGPS_Data_tmp= GNGGA(str_tab)

index=strfind(str_tab,’,');

 count =1;

 Time     = str_tab(index(count)+1:

index(count+1)-1);count=count+1;

  Latitude      =str_tab(index(count)+1:

index(count+1)—1);count=count+1;

GPS_Data_tmp、LatitudeDir= str_tab(index(count)+1:

index(count+1)—1);count=count+1;

Longitude     = str_tab(index(count)+1:

index(count+1)—1);count=count+1;

GPS_Data_tmp、LongitudeDir=str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、GPSState  =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、SatelliteNum= str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 GPS_Data_tmp、HDOP =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

GPS_Data_tmp、altitude  =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

%other     =str_tab(index(count)+1:

end);

%进一步处理

GPS_Data_tmp、Time、hour=Time(1:

2);

   GPS_Data_tmp、Time、min =Time(3:

4);

 GPS_Data_tmp、Time、sec= Time(5:

6);

   GPS_Data_tmp、Time、millisec=Time(8:

10);

  GPS_Data_tmp、Latitude、degree=Latitude(1:

2); %纬度

 GPS_Data_tmp、Latitude、min=Latitude(3:

4);

 tmp=str2double(Latitude(6:

9));

 tmp= tmp*6/1000;%tmp =tmp/10000*60;

 GPS_Data_tmp、Latitude、sec=num2str(floor(tmp));

 GPS_Data_tmp、Latitude、millisec= num2str((tmp-floor(tmp))*10000);

 GPS_Data_tmp、Longitude、degree=Longitude(1:

3);%经度

 GPS_Data_tmp、Longitude、min=Longitude(4:

5);

 tmp =str2double(Longitude(7:

10));

 tmp= tmp*6/1000;%tmp =tmp/10000*60;

 GPS_Data_tmp、Longitude、sec=num2str(floor(tmp));

 GPS_Data_tmp、Longitude、millisec=num2str((tmp—floor(tmp))*10000);

  %UTC时间转换为北京时间

  hour=GPS_Data_tmp、Time、hour;

ifstr2num(hour)+8>= 24

  GPS_Data_tmp、Time、hour = num2str(str2num(hour)+8—24);

 else

  GPS_Data_tmp、Time、hour=num2str(str2num(hour)+8);

end

end

function GPS_Data_tmp=GNGSA(str_tab)

 index=strfind(str_tab,’,');

 count= 1;

 GPS_Data_tmp、LocationMode=str_tab(index(count)+1:

index(count+1)-1);count=count+1;

  GPS_Data_tmp、CurState =str_tab(index(count)+1:

index(count+1)—1);count=count+1;

  GPS_Data_tmp、PRN     =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、PDOP  = str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 GPS_Data_tmp、HDOP   = str_tab(index(count)+1:

index(count+1)-1);count=count+1;

GPS_Data_tmp、VDOP  =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

% other   = str_tab(index(count)+1:

end);

end

function GPS_Data_tmp= GNGSV(str_tab)

%此语句为与卫星有关得信息(包括卫星方位,卫星编号)

%暂时用不着,不处理

 GPS_Data_tmp =[];

end

functionGPS_Data_tmp= GNRMC(str_tab)

 index=strfind(str_tab,',’);

  count=1;

 Time       = str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 GPS_Data_tmp、LocationState= str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 Latitude    =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、LatitudeDir=str_tab(index(count)+1:

index(count+1)-1);count=count+1;

Longitude   =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、LongitudeDir =str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 GPS_Data_tmp、speed= str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 GPS_Data_tmp、TrueDir =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

  Date   =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、MagneticAngle=str_tab(index(count)+1:

index(count+1)—1);count=count+1;

  GPS_Data_tmp、MagneticDir= str_tab(index(count)+1:

index(count+1)-1);count=count+1;

%other      = str_tab(index(count)+1:

end);

 % 进一步处理

 GPS_Data_tmp、Time、hour= Time(1:

2);

 GPS_Data_tmp、Time、min=Time(3:

4);

GPS_Data_tmp、Time、sec=Time(5:

6);

 GPS_Data_tmp、Time、millisec=Time(8:

10);

GPS_Data_tmp、Latitude、degree=Latitude(1:

2); % 纬度

 GPS_Data_tmp、Latitude、min= Latitude(3:

4);

 tmp=str2double(Latitude(6:

9));

tmp=tmp*6/1000;% tmp=tmp/10000*60;

GPS_Data_tmp、Latitude、sec =num2str(floor(tmp));

 GPS_Data_tmp、Latitude、millisec=num2str((tmp—floor(tmp))*10000);

  GPS_Data_tmp、Longitude、degree=Longitude(1:

3);% 经度

 GPS_Data_tmp、Longitude、min =Longitude(4:

5);

 tmp=str2double(Longitude(7:

10));

 tmp=tmp*6/1000; %tmp=tmp/10000*60;

  GPS_Data_tmp、Longitude、sec= num2str(floor(tmp));

GPS_Data_tmp、Longitude、millisec=num2str((tmp—floor(tmp))*10000);

 GPS_Data_tmp、DATE、day =Date(1:

2);

GPS_Data_tmp、DATE、month=Date(3:

4);

GPS_Data_tmp、DATE、year=Date(5:

6);

  %UTC时间转换为北京时间

 hour =GPS_Data_tmp、Time、hour;

ifstr2num(hour)+8>=24

   GPS_Data_tmp、Time、hour = num2str(str2num(hour)+8—24);

 else

   GPS_Data_tmp、Time、hour= num2str(str2num(hour)+8);

  end

end

functionGPS_Data_tmp=GNVTG(str_tab)

index=strfind(str_tab,',');

count=1;

GPS_Data_tmp、TrueDir    =str_tab(index(count)+1:

index(count+1)—1);count=count+1;

  GPS_Data_tmp、ReferenceTrueDir =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

GPS_Data_tmp、RelativeDir    =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

GPS_Data_tmp、ReferenceRelativeDir= str_tab(index(count)+1:

index(count+1)—1);count=count+1;

  GPS_Data_tmp、step     =str_tab(index(count)+1:

index(count+1)—1);count=count+1;

 GPS_Data_tmp、stepflag    =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 GPS_Data_tmp、velocity   = str_tab(index(count)+1:

index(count+1)-1);count=count+1;

%other       =str_tab(index(count)+1:

end);

end

functionGPS_Data_tmp=GNGLL(str_tab)

index=strfind(str_tab,',’);

  count =1;

  Latitude          = str_tab(index(count)+1:

index(count+1)-1);count=count+1;

GPS_Data_tmp、LatitudeDir  =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 Longitude     =str_tab(index(count)+1:

index(count+1)—1);count=count+1;

GPS_Data_tmp、LongitudeDir=str_tab(index(count)+1:

index(count+1)-1);count=count+1;

 Date       =str_tab(index(count)+1:

index(count+1)-1);count=count+1;

  GPS_Data_tmp、LocationState=str_tab(index(count)+1:

index(count+1)-1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1