% 注:本程序可直接在MATLAB 2017a 中运行%该脚本文件用于学习GPS数据的读取,需要做其他用途请自行修改代码%本脚本文件的前面几行代码是要设置的一些参数%默认使用COM3(需视情况修改)%波特率设为GPS模块默认的38400%下面为程序源码clearnum_execute = 100; % 执行次数num_SingleRead = 150; %单次从串口读取的字节数(最好设置足够大(最低大概设为80),保证单次读取的数据包含一条完整的GPS数据)Timedelay = ; % 用于延时读取串口数据BaudRate = 38400; % 读取数据的波特率Terminator = 'CR';num_MaxTry = 5; %打开串口的最多尝试次数BytesAvailableFcnCount = 1000;%% 设置参数% delete(instrfindall); % 串口打开失败时使用此句% delete(s);clear s % 串口打开失败时使用此句serial3 = serial('COM3');% 串口设置= 'byte';% = 38400; % 输出波特率= BaudRate; % 读入波特率% = 1024;= BytesAvailableFcnCount;= 'continuous';= Terminator;%% 打开串口count_opentimes = 1;while contains,'closed') > 0 && count_opentimes < num_MaxTryfopen(serial3); %打开串口count_opentimes = count_opentimes+1;endif contains,'open') < 1disp('open com failed!');returnend%% 读取并处理数据% 初始化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);clear serial3%%%将字符串根据'\r\n'划分成多个子字符串,同时去掉首尾无用的残余字符串function out_strs = split_str2strs(StrData)if contains(class(StrData),'char')uint8(StrData);endrecord = get_pos_enterflag(StrData);if StrData(1) == uint8('$') %开头为'$'的情况flag_start = 1;elseif size(record,2) > 0flag_start = record(1)+2;elseout_strs = cell(0,0);returnendendif StrData(end) == 13flag_end = length(StrData)-1;elseif size(record,2) > 0flag_end = record(end)-1;endendif flag_start >= flag_endout_strs = cell(0,0);returnendStrData = StrData(flag_start:flag_end); % 截取有效数据,方便下面划分子字符串record = get_pos_enterflag(StrData);num_strs = size(record,2)+1;out_strs = cell(num_strs,1);if num_strs > 1out_strs{1,1} = char(StrData(1:record(1)-1));if num_strs == 2out_strs{num_strs,1} = char(StrData(record(1)+2:end));elsefor i = 2 : num_strs-1out_strs{i,1} = char(StrData(record(i-1)+2:record(i)-1)); endout_strs{num_strs,1} = char(StrData(record(i)+2:end));endelseout_strs{1,1} = char(StrData);end% 得到字符串中'\r\n'在字符串中的位置(实际为'\r'的位置)function record = get_pos_enterflag(data)record = []; % 记录回车符号位置for ii = 1 : length(data)-1if data(ii) == 13if data(ii+1) == 10record = [record,ii];ii = ii+1;endendendendend% 得到具体GPS结构体数据function GPS_Data_tmp = get_GPS_specificData(StrsData) GPS_Data_tmp = [];num_str = size(StrsData,1);for i = 1 : num_strstr_tab = StrsData{i,1};if contains(str_tab,'GGA') > 0GPS_Data_tmp = GNGGA(str_tab);elseif contains(str_tab,'GSA') > 0GPS_Data_tmp = GNGSA(str_tab);elseif contains(str_tab,'GSV') > 0GPS_Data_tmp = GNGSV(str_tab);elseif contains(str_tab,'RMC') > 0GPS_Data_tmp = GNRMC(str_tab);elseif contains(str_tab,'VTG') > 0GPS_Data_tmp = GNVTG(str_tab);elseif contains(str_tab,'GLL') > 0GPS_Data_tmp = GNGLL(str_tab);endendend% GPS字符串解析function GPS_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 = if str2num(hour)+8 >= 24= num2str(str2num(hour)+8-24);else= num2str(str2num(hour)+8);endendfunction GPS_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);endfunction GPS_Data_tmp = GNGSV(str_tab)% 此语句为与卫星有关的信息(包括卫星方位,卫星编号)% 暂时用不着,不处理GPS_Data_tmp = [];endfunction GPS_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 = if str2num(hour)+8 >= 24= num2str(str2num(hour)+8-24);else= num2str(str2num(hour)+8);endendfunction GPS_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);endfunction GPS_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% 更新获取到的相关数据function GPS_Data = Updata_GPU_Data(GPS_Data,GPS_Data_tmp) % 用不到的数据可以注释掉if isfield(GPS_Data_tmp,'Time') == 1= = = = endif isfield(GPS_Data_tmp,'DATE') == 1= = = endif isfield(GPS_Data_tmp,'Latitude') == 1= = = = endif isfield(GPS_Data_tmp,'LatitudeDir') == 1= ;endif isfield(GPS_Data_tmp,'Longitude') == 1= = = = endif isfield(GPS_Data_tmp,'LongitudeDir') == 1= ;endif isfield(GPS_Data_tmp,'GPSState') == 1= ;endif isfield(GPS_Data_tmp,'SatelliteNum') == 1 = ;endif isfield(GPS_Data_tmp,'speed') == 1= ;endif isfield(GPS_Data_tmp,'velocity') == 1= ;endif isfield(GPS_Data_tmp,'LocationState') == 1 = ;endif isfield(GPS_Data_tmp,'altitude') == 1= ;endif isfield(GPS_Data_tmp,'CurState') == 1= ;endif isfield(GPS_Data_tmp,'LocationMode') == 1 = ;endif isfield(GPS_Data_tmp,'HDOP') == 1= ;endif isfield(GPS_Data_tmp,'VDOP') == 1= ;endif isfield(GPS_Data_tmp,'PDOP') == 1= ;endif isfield(GPS_Data_tmp,'TrueDir') == 1= ;endif isfield(GPS_Data_tmp,'MagneticAngle') == 1= ;endif isfield(GPS_Data_tmp,'MagneticDir') == 1= ;endif isfield(GPS_Data_tmp,'ReferenceTrueDir') == 1 = ;endif isfield(GPS_Data_tmp,'RelativeDir') == 1= ;endif isfield(GPS_Data_tmp,'ReferenceRelativeDir') == 1= ;endif isfield(GPS_Data_tmp,'step') == 1= ;endif isfield(GPS_Data_tmp,'stepflag') == 1= ;endif isfield(GPS_Data_tmp,'PRN') == 1= ;endend% 显示相关GPS数据function show_GPS_Data(GPS_Data)DataAndTime = sprintf('20%02s-%02s-%02s %02s:%02s:%02s:%03s',...Location =sprintf(' %s:%02s°%02s′%03s″%04s,%s:%02s°%02s′%03s″%04s',..., , Others = sprintf('GPSState:%s,SatelliteNum:%02s,Speed:%03s,Velocity:%s,LocationState:%s',... % ,,,,;% show_Message_str(strcat(DataAndTime,Location,Others));show_Message_str(strcat(DataAndTime,Location));end% 初始化GPS数据结构体function GPS_Data = GPS_Init()= '0';= '0';= '0';= '0';= '29';= '8';= '18';= '0'; % 纬度= '0';= '0';= '0';= 'N';= '0'; % 经度= '0';= '0';= '0';= 'E';= '0'; % GPS状态,0:未定位;1:无差分定位;2:带差分定位;3:无效GPS;6:正在估算= '0'; % 可用卫星数目= '0';= '0';= 'V';= '0'; % 海拔高度= '1'; % 当前状态,1:无定位信息;2:2D;3:3D = 'A'; % 定位模式,'A':自动,'M':手动= ''; % 水平精度因子= ''; % 垂直精度因子= ''; % 综合位置精度因子= '0'; % 方位角= '0'; % 磁偏角= 'E'; % 磁偏角参考方向,E/W(东西经)= 'T'; % 真实方向的参考方向,T:正北参照系= '0'; % 相对方向= 'M'; % M:磁北参照系= '0'; % 步长= 'N';= '01'; % 正在使用的卫星PRN码编号end% 可实现静态显示输出内容(防止输出内容不同换行) function show_Message_str(strData)persistent CurMessage;if isempty(CurMessage)CurMessage = '';endfprintf(1,repmat('\b',1,numel(CurMessage))); fprintf(1,'%s',strData);CurMessage = strData;end。