fopen(serial3);%翻开串口
count_opentimes=count_opentimes+1;
end
ifcontains(serial3.status,'open')<1
disp('opencomfailed!
');
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
ifflag_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
fori=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
ifdata(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);
fori=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);
elseifcontains(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;
=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;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
%other=str_tab(index(count)+1:
end);
%进一步处理
=Time(1:
2);
=Time(3:
4);
=Time(5:
6);
=Time(8:
10);
=Latitude(1:
2);%纬度
=Latitude(3:
4);
tmp=str2double(Latitude(6:
9));
tmp=tmp*6/1000;%tmp=tmp/10000*60;
=num2str(floor(tmp));
=num2str((tmp-floor(tmp))*10000);
=Longitude(1:
3);%经度
=Longitude(4:
5);
tmp=str2double(Longitude(7:
10));
tmp=tmp*6/1000;%tmp=tmp/10000*60;
=num2str(floor(tmp));
=num2str((tmp-floor(tmp))*10000);
%UTC时间转换为北京时间
hour=;
ifstr2num(hour)+8>=24
=num2str(str2num(hour)+8-24);
else
=num2str(str2num(hour)+8);
end
end
functionGPS_Data_tmp=GNGSA(str_tab)
index=strfind(str_tab,',');
count=1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
%other=str_tab(index(count)+1:
end);
end
functionGPS_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;
=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;
=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;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=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;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
%other=str_tab(index(count)+1:
end);
%进一步处理
=Time(1:
2);
=Time(3:
4);
=Time(5:
6);
=Time(8:
10);
=Latitude(1:
2);%纬度
=Latitude(3:
4);
tmp=str2double(Latitude(6:
9));
tmp=tmp*6/1000;%tmp=tmp/10000*60;
=num2str(floor(tmp));
=num2str((tmp-floor(tmp))*10000);
=Longitude(1:
3);%经度
=Longitude(4:
5);
tmp=str2double(Longitude(7:
10));
tmp=tmp*6/1000;%tmp=tmp/10000*60;
=num2str(floor(tmp));
=num2str((tmp-floor(tmp))*10000);
=Date(1:
2);
=Date(3:
4);
=Date(5:
6);
%UTC时间转换为北京时间
hour=;
ifstr2num(hour)+8>=24
=num2str(str2num(hour)+8-24);
else
=num2str(str2num(hour)+8);
end
end
functionGPS_Data_tmp=GNVTG(str_tab)
index=strfind(str_tab,',');
count=1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
=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;
=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;
=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;
=str_tab(index(count)+1:
index(count+1)-1);count=count+1;
%other=str_tab(index(count)+1:
end);
%进一步处理
=Latitude(1:
2);%纬度
=Latitude(3:
4);
tmp=str2double(Latitude(6:
9));
tmp=tmp*6/1000;%tmp=tmp/10000*60;
=num2str(floor(tmp));
=num2str((tmp-floor(tmp))*10000);
=Longitude(1:
3);%经度
=Longitude(4:
5);
tmp=str2double(Longitude(7:
10));
tmp=tmp*6/1000;%tmp=tmp/10000*60;
=num2str(floor(tmp));
=num2str((tmp-floor(tmp))*10000);
=Date(1:
2);
=Date(3:
4);
=Date(5:
6);
end
%更新获取到的相关数据
functionGPS_Data=Updata_GPU_Data(GPS_Data,GPS_Data_tmp)
%用不到的数据可以注释掉
ifisfield(GPS_Data_tmp,'Time')==1
=;
=;
=;
=;
end
ifisfield(GPS_Data_tmp,'DATE')==1
=;
=;
=;
end
ifisfield(GPS_Data_tmp,'Latitude')==1
=;
=;
=;
=;
end
ifisfield(GPS_Data_tmp,'LatitudeDir')==1
=;
end
ifisfield(GPS_Data_tmp,'Longitude')==1
=;
=;
=;
=;
end
ifisfield(GPS_Data_tmp,'LongitudeDir')==1
=;
end
ifisfield(GPS_Data_tmp,'GPSState')==1
=;
end
ifisfield(GPS_Data_tmp,'SatelliteNum')==1
=;
end
ifisfield(GPS_Data_tmp,'speed')==1
=;
end
ifisfield(GPS_Data_tmp,'velocity')==1
=;
end
ifisfield(GPS_Data_tmp,'LocationState')==1
=;
end
ifisfield(GPS_Data_tmp,'altitude')==1
=;
end
ifisfield(GPS_Data_tmp,'CurState')==1
=;
end
ifisfield(GPS_Data_tmp,'LocationMode')==1
=;
end
ifisfield(GPS_Data_tmp,'HDOP')==1
=;
end
ifisfield(GPS_Data_tmp,'VDOP')==1
=;
end
ifisfield(GPS_Data_tmp,'PDOP')==1
=;
end
ifisfield(GPS_Data_tmp,'TrueDir')==1
=;
end
ifisfield(GPS_Data_tmp,'MagneticAngle')==1
=;
end
ifisfield(GPS_Data_tmp,'MagneticDir')==1
=;
end
ifisfield(GPS_Data_tmp,'ReferenceTrueDir')==1
=;
end
ifisfield(GPS_Data_tmp,'RelativeDir')==1
=;
end
ifisfield(GPS_Data_tmp,'ReferenceRelativeDir')==1
=;
end
ifisfield(GPS_Data_tmp,'step')==1
=;
end
ifisfield(GPS_Data_tmp,'stepflag')==1
=;
end
ifisfield(GPS_Data_tmp,'PRN')==1
=;
end
end
%显示相关GPS数据
functionshow_GPS_Data(GPS_Data)
DataA