




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、%注:本程序可直接在MATLAB 2017a中运行%亥脚本文件用于学习 GP缴据的读取,需要做其他用途请自行修改代码%本脚本文件的前面几行代码是要设置的一些参数哪认使用COM3需视情况修改)姓特率设为GPS1块默认的38400%不面为程序源码clearnum_execute = 100; % 执行次数num_SingleRead = 150; %单次从串口读取的字节数(最好设置足够大(最低大概设为80),保证单次读取的数据包含一条完整的GPSiK据)Timedelay = ; %用于延时读取串口数据BaudRate = 38400; % 读取数据的波特率Terminator = 'CR
2、'num_MaxTry = 5; %打开串口的最多尝试次数BytesAvailableFcnCount = 1000;%设置参数% delete(instrfindall); %串口打开失败时使用此句% delete(s);clear s %串口打开失败时使用此句serial3 = serial('COM3');%串口设置='byte'% = 38400; % 输出波特率=BaudRate; % 读入波特率% = 1024;=BytesAvailableFcnCount;='continuous'=Terminator;%丁开串口coun
3、t_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_Dat
4、aStrs = 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_
5、execute = num_execute-1;end% fprintf(s,'abcd');%给串口的发送数据% fscanf(s);%从串口的接收缓存读数据%关闭审口并删除相关数据fclose(serial3);%关闭串口delete(serial3);clear serial3%各字符串根据rn'划分成多个子字符串,同时去掉首尾无用的残余字符串function out_strs = split_str2strs(StrData)if contains(class(StrData),'char')uint8(StrData);endrecord =
6、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
7、_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_strs1,1 = char(StrData(1:record(1)-1);if num_strs = 2out_strsnum_strs,1 = cha
8、r(StrData(record(1)+2:end);elsefor i = 2 : num_strs-1out_strsi,1 = char(StrData(record(i-1)+2:record(i)-1);endout_strsnum_strs,1 = char(StrData(record(i)+2:end);endelseout_strs1,1 = char(StrData);end% 得到字符串中rn' 在字符串中的位置(实际为'r'的位置)function record = get_pos_enterflag(data)record = ; %记录回车符
9、号位置for ii = 1 : length(data)-1if data(ii) = 13if data(ii+1) = 10record = record;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 = StrsDatai,1;if contains(str_tab,'GGA') > 0GP
10、S_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') > 0GP
11、S_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
12、: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(cou
13、nt+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
14、/1000; % tmp = tmp/10000*60;=num2str(floor(tmp);=num2str(tmp-f100r(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(str2
15、num(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=cou
16、nt+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
17、 =;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:ind
18、ex(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(cou
19、nt+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/
20、1000; % tmp = tmp/10000*60;=num2str(floor(tmp);=num2str(tmp-f100r(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-f100r(tmp)*10000);=Date(1:2);=Date(3:4);=Date(5:6);% UTC时间转换为北京时间hour = if str2n
21、um(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(coun
22、t)+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);e
23、ndfunction 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:in
24、dex(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;=num
25、2str(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-f100r(tmp)*10000);=Date(1:2);=Date(3:4);=Date(5:6);end%更新获取到的相关数据function GPS_Data = Updata_GPU_Data(GPS_Data,G
26、PS_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_
27、tmp,'LongitudeDir') = 1=;endif isfield(GPS_Data_tmp,'GPSState') = 15endif 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=;en
28、dif 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') =
29、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_D
30、ata_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%显示相关GPSK据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,Lo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 薪酬管理制度理论意义
- 集训基地库房管理制度
- 铁路管理制度读后感悟
- 保安室人员管理制度
- 高职学校寝室管理制度
- 装卸人员日常管理制度
- 行政餐厅宿舍管理制度
- 供销社公司管理制度
- 项目公司技术管理制度
- 西安深化预算管理制度
- 生产车间5S管理制度
- 2025年吉林铁道职业技术学院单招职业倾向性考试题库含答案
- 装修公司设计经理述职报告
- 2025年吉林铁道职业技术学院单招职业技能测试题库一套
- 【珍藏版】鲁教版初中英语全部单词表(带音标、按单元顺序)
- 2023学年杭州市余杭区七年级语文下学期期中考试卷附答案解析
- 9《小水滴的诉说》(教学设计)-2023-2024学年统编版道德与法治二年级下册
- 2025年安徽合肥兴泰金融控股集团招聘笔试参考题库含答案解析
- 《道路交通安全法》课件完整版
- 加快形成农业新质生产力
- 施工现场应急救援知识
评论
0/150
提交评论