版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、% 注:本程序可直接在MATLAB 2017a 中运行%该脚本文件用于学习GPS数据的读取,需要做其他用途请自行修改代码%本脚本文件的前面几行代码是要设置的一些参数%默认使用COM3(需视情况修改)%波特率设为GPS模块默认的38400%下面为程序源码 clearnum_execute = 100; % 执行次数num_SingleRead = 150; %单次从串口读取的字节数(最好设置足够大(最低大概设为80),保证单次读取的数据包含一条完整的GPS数据)Timedelay = 0.2; % 用于延时读取串口数据BaudRate = 38400; % 读取数据的波特率Terminator
2、= 'CR'num_MaxTry = 5; %打开串口的最多尝试次数BytesAvailableFcnCount = 1000;% 设置参数% delete(instrfindall); % 串口打开失败时使用此句% delete(s);clear s % 串口打开失败时使用此句serial3 = serial('COM3');% 串口设置 serial3.BytesAvailableFcnMode = 'byte'% serial3.InputBufferSize = 38400; % 输出波特率serial3.BaudRate = BaudR
3、ate; % 读入波特率% serial3.OutputBufferSize = 1024;serial3.BytesAvailableFcnCount = BytesAvailableFcnCount;serial3.ReadAsyncMode = 'continuous'serial3.Terminator = Terminator;% 打开串口count_opentimes = 1;while contains(serial3.status,'closed') > 0 && count_opentimes < num_MaxTr
4、y fopen(serial3); %打开串口 count_opentimes = count_opentimes+1;endif contains(serial3.status,'open') < 1 disp('open com failed!'); returnend% 读取并处理数据% 初始化GPS_Data = GPS_Init();while(num_execute > 0) GPS_DataStrs = fread(serial3,num_SingleRead,'char'); %一次读出10个字符 GPS_DataSt
5、rs = 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'); %给串口的发送数据% fsc
6、anf(s); %从串口的接收缓存读数据% 关闭串口并删除相关数据fclose(serial3); %关闭串口delete(serial3);clear serial3% %将字符串根据'rn'划分成多个子字符串,同时去掉首尾无用的残余字符串function out_strs = split_str2strs(StrData) if contains(class(StrData),'char') uint8(StrData); end record = get_pos_enterflag(StrData); if StrData(1) = uint8('
7、$') %开头为'$'的情况 flag_start = 1; else if size(record,2) > 0 flag_start = record(1)+2; else out_strs = cell(0,0); return end end if StrData(end) = 13 flag_end = length(StrData)-1; else if size(record,2) > 0 flag_end = record(end)-1; end end if flag_start >= flag_end out_strs = cell
8、(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); if num_strs > 1 out_strs1,1 = char(StrData(1:record(1)-1); if num_strs = 2 out_strsnum_strs,1 = char(StrData(record(1)+2:end
9、); else for i = 2 : num_strs-1 out_strsi,1 = char(StrData(record(i-1)+2:record(i)-1); end out_strsnum_strs,1 = char(StrData(record(i)+2:end); end else out_strs1,1 = char(StrData); end % 得到字符串中'rn'在字符串中的位置(实际为'r'的位置) function record = get_pos_enterflag(data) record = ; % 记录回车符号位置 for
10、ii = 1 : length(data)-1 if data(ii) = 13 if data(ii+1) = 10 record = record,ii; ii = ii+1; end end end endend% 得到具体GPS结构体数据function GPS_Data_tmp = get_GPS_specificData(StrsData) GPS_Data_tmp = ; num_str = size(StrsData,1); for i = 1 : num_str str_tab = StrsDatai,1; if contains(str_tab,'GGA')
11、 > 0 GPS_Data_tmp = GNGGA(str_tab); elseif contains(str_tab,'GSA') > 0 GPS_Data_tmp = GNGSA(str_tab); elseif contains(str_tab,'GSV') > 0 GPS_Data_tmp = GNGSV(str_tab); elseif contains(str_tab,'RMC') > 0 GPS_Data_tmp = GNRMC(str_tab); elseif contains(str_tab,'V
12、TG') > 0 GPS_Data_tmp = GNVTG(str_tab); elseif contains(str_tab,'GLL') > 0 GPS_Data_tmp = GNGLL(str_tab); end endend% 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; Latit
13、ude = 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
14、_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
15、);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.Tlisec = Time(8:10); GPS_Data_tmp.Latitude.degree = Latitude(1:2); % 纬度 GPS_Data_tmp.Latitude.min = Latitude(3:
16、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.Llisec = num2str(tmp-floor(tmp)*10000); GPS_Data_tmp.Longitude.degree = Longitude(1:3); % 经度 GPS_Data_tmp.Longitude.min = Longitude(4:5); tmp = str2doub
17、le(Longitude(7:10); tmp = tmp*6/1000; % tmp = tmp/10000*60; GPS_Data_tmp.Longitude.sec = num2str(floor(tmp); GPS_Data_tmp.Llisec = num2str(tmp-floor(tmp)*10000); % UTC时间转换为北京时间 hour = GPS_Data_tmp.Time.hour; if str2num(hour)+8 >= 24 GPS_Data_tmp.Time.hour = num2str(str2num(hour)+8-24)
18、; else GPS_Data_tmp.Time.hour = num2str(str2num(hour)+8); endendfunction 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)
19、;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)
20、-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=cou
21、nt+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=
22、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);cou
23、nt=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
24、_Data_tmp.Time.sec = Time(5:6); GPS_Data_tmp.Tlisec = 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.
25、Llisec = 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.Lli
26、sec = 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; if str2num(hour)+8 >= 24 GPS_Data_tmp.Time.hour = num2str(str2num(hour)+8-24); else GPS_Data_tmp.Time.hour
27、 = num2str(str2num(hour)+8); endendfunction GPS_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_t
28、mp.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+
29、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);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; GPS_
30、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_Da
31、ta_tmp.LocationState = str_tab(index(count)+1:index(count+1)-1);count=count+1;% other = str_tab(index(count)+1:end); % 进一步处理 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_
32、tmp.Latitude.sec = num2str(floor(tmp); GPS_Data_tmp.Llisec = 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.se
33、c = num2str(floor(tmp); GPS_Data_tmp.Llisec = 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);end% 更新获取到的相关数据function GPS_Data = Updata_GPU_Data(GPS_Data,GPS_Data_tmp)% 用不到的数据可以注释掉 if isfield(GPS_Da
34、ta_tmp,'Time') = 1 GPS_Data.Time.hour = GPS_Data_tmp.Time.hour; GPS_Data.Time.min = GPS_Data_tmp.Time.min; GPS_Data.Time.sec = GPS_Data_tmp.Time.sec; GPS_Data.Tlisec = GPS_Data_tmp.Tlisec; end if isfield(GPS_Data_tmp,'DATE') = 1 GPS_Data.DATE.day = GPS_Data_tmp.DATE.day
35、; GPS_Data.DATE.month = GPS_Data_tmp.DATE.month; GPS_Data.DATE.year = GPS_Data_tmp.DATE.year; end if isfield(GPS_Data_tmp,'Latitude') = 1 GPS_Data.Latitude.degree = GPS_Data_tmp.Latitude.degree; GPS_Data.Latitude.min = GPS_Data_tmp.Latitude.min; GPS_Data.Latitude.sec = GPS_Data_tmp.Latitude.
36、sec; GPS_Data.Llisec = GPS_Data_tmp.Llisec; end if isfield(GPS_Data_tmp,'LatitudeDir') = 1 GPS_Data.LatitudeDir = GPS_Data_tmp.LatitudeDir; end if isfield(GPS_Data_tmp,'Longitude') = 1 GPS_Data.Longitude.degree = GPS_Data_tmp.Longitude.degree; GPS_Data.Longitude
37、.min = GPS_Data_tmp.Longitude.min; GPS_Data.Longitude.sec = GPS_Data_tmp.Longitude.sec; GPS_Data.Llisec = GPS_Data_tmp.Llisec; end if isfield(GPS_Data_tmp,'LongitudeDir') = 1 GPS_Data.LongitudeDir = GPS_Data_tmp.LongitudeDir; end if isfield(GPS_Data_tmp,'GPSState&
38、#39;) = 1 GPS_Data.GPSState = GPS_Data_tmp.GPSState; end if isfield(GPS_Data_tmp,'SatelliteNum') = 1 GPS_Data.SatelliteNum = GPS_Data_tmp.SatelliteNum; end if isfield(GPS_Data_tmp,'speed') = 1 GPS_Data.speed = GPS_Data_tmp.speed; end if isfield(GPS_Data_tmp,'velocity') = 1 GP
39、S_Data.velocity = GPS_Data_tmp.velocity; end if isfield(GPS_Data_tmp,'LocationState') = 1 GPS_Data.LocationState = GPS_Data_tmp.LocationState; end if isfield(GPS_Data_tmp,'altitude') = 1 GPS_Data.altitude = GPS_Data_tmp.altitude; end if isfield(GPS_Data_tmp,'CurState') = 1 GP
40、S_Data.CurState = GPS_Data_tmp.CurState; end if isfield(GPS_Data_tmp,'LocationMode') = 1 GPS_Data.LocationMode = GPS_Data_tmp.LocationMode; end if isfield(GPS_Data_tmp,'HDOP') = 1 GPS_Data.HDOP = GPS_Data_tmp.HDOP; end if isfield(GPS_Data_tmp,'VDOP') = 1 GPS_Data.VDOP = GPS_D
41、ata_tmp.VDOP; end if isfield(GPS_Data_tmp,'PDOP') = 1 GPS_Data.PDOP = GPS_Data_tmp.PDOP; end if isfield(GPS_Data_tmp,'TrueDir') = 1 GPS_Data.TrueDir = GPS_Data_tmp.TrueDir; end if isfield(GPS_Data_tmp,'MagneticAngle') = 1 GPS_Data.MagneticAngle = GPS_Data_tmp.MagneticAngle; e
42、nd if isfield(GPS_Data_tmp,'MagneticDir') = 1 GPS_Data.MagneticDir = GPS_Data_tmp.MagneticDir; end if isfield(GPS_Data_tmp,'ReferenceTrueDir') = 1 GPS_Data.ReferenceTrueDir = GPS_Data_tmp.ReferenceTrueDir; end if isfield(GPS_Data_tmp,'RelativeDir') = 1 GPS_Data.RelativeDir =
43、GPS_Data_tmp.RelativeDir; end if isfield(GPS_Data_tmp,'ReferenceRelativeDir') = 1 GPS_Data.ReferenceRelativeDir = GPS_Data_tmp.ReferenceRelativeDir; end if isfield(GPS_Data_tmp,'step') = 1 GPS_Data.step = GPS_Data_tmp.step; end if isfield(GPS_Data_tmp,'stepflag') = 1 GPS_Data
44、.stepflag = GPS_Data_tmp.stepflag; end if isfield(GPS_Data_tmp,'PRN') = 1 GPS_Data.PRN = GPS_Data_tmp.PRN; endend% 显示相关GPS数据function show_GPS_Data(GPS_Data) DataAndTime = sprintf('20%02s-%02s-%02s %02s:%02s:%02s:%03s',. GPS_Data.DATE.year,GPS_Data.DATE.month,GPS_Data.DATE.day,. GPS_D
45、ata.Time.hour,GPS_Data.Time.min,GPS_Data.Time.sec,GPS_Data.Tlisec); Location = sprintf(' %s:%02s°%02s%03s%04s,%s:%02s°%02s%03s%04s',. GPS_Data.LatitudeDir,GPS_Data.Latitude.degree,GPS_Data.Latitude.min,GPS_Data.Latitude.sec,GPS_Data.Llisec,. GPS_Data.LongitudeDir,
46、GPS_Data.Longitude.degree,GPS_Data.Longitude.min,GPS_Data.Longitude.sec,GPS_Data.Llisec);% Others = sprintf(' GPSState:%s,SatelliteNum:%02s,Speed:%03s,Velocity:%s,LocationState:%s',.% GPS_Data.GPSState,GPS_Data.SatelliteNum,GPS_Data.speed,GPS_Data.velocity,GPS_Data.LocationState)
47、;% show_Message_str(strcat(DataAndTime,Location,Others); show_Message_str(strcat(DataAndTime,Location);end% 初始化GPS数据结构体function GPS_Data = GPS_Init() GPS_Data.Time.hour = '0' GPS_Data.Time.min = '0' GPS_Data.Time.sec = '0' GPS_Data.Tlisec = '0' GPS_Data.DATE.da
48、y = '29' GPS_Data.DATE.month = '8' GPS_Data.DATE.year = '18' GPS_Data.Latitude.degree = '0' % 纬度 GPS_Data.Latitude.min = '0' GPS_Data.Latitude.sec = '0' GPS_Data.Llisec = '0' GPS_Data.LatitudeDir = 'N' GPS_Data.Longitude.degree = '0' % 经度 GPS_Data.Longitude.min = '0' GPS_Data.Longitude.sec = '0' GPS_Data.Llisec = '0' GPS_Data.LongitudeDir = 'E' GPS_Data.GPSState = '0' % GPS状态,0:未定位;1:无差分定位;2:带差分
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 深圳二手房公积金贷款合同样本
- 养殖场环保设施运维施工合同
- 交通枢纽加油站施工合同
- 2025产品寄售合同范本
- 物业房屋维修合同
- 2025进料加工复出口合同范本
- 小学生情绪课堂模板
- 现状与挑战:就业市场模板
- 山西财经大学华商学院《电力技术经济》2023-2024学年第一学期期末试卷
- 儿童秋季常见病预防知识
- 国开(浙江)2024年秋《中国建筑史(本)》形考作业1-4答案
- 2024年海南省高考历史试卷(含答案解析)
- 北京工业大学《数学模型》2022-2023学年第一学期期末试卷
- 部编版语文五年级上册《父爱之舟》说课
- 大学生思想道德与法治课件
- 《个人防守技术:抢、断球技术》教案
- 2024版成人术中非计划低体温预防与护理培训课件
- 浙江省数字化改革总体方案
- 光伏屋顶安全施工方案怎么写
- XX道路危险运输企业安全管理台账标准化表格
- (2024年新版本)七年级上册道德与法治1-4单元试卷
评论
0/150
提交评论