




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、% 注:本程序可直接在MATLAB 2017a 中運行%該腳本文件用于學習GPS數據的讀取,需要做其他用途請自行修改代碼%本腳本文件的前面幾行代碼是要設置的一些參數%默認使用COM3(需視情況修改)%波特率設為GPS模塊默認的38400%下面為程序源碼 clearnum_execute = 100; % 執(zhí)行次數num_SingleRead = 150; %單次從串口讀取的字節(jié)數(最好設置足夠大(最低大概設為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'2 / 19serial3.Terminator = Terminator;% 打開串口count_opentimes = 1;while contains(serial3.status,'closed') > 0 && count_opentimes < num
4、_MaxTry 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_
5、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'); %給串口的發(fā)送數
6、據% fscanf(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
8、= 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); 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)
9、+2:end); 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 = ; % 記錄回車符號位
10、置 for 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,
12、'VTG') > 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;
13、 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+
14、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(coun
15、t+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.Tlisec = Time(8:10); GPS_Data_tmp.Latitude.degree = Latitude(1:2); % 緯度 GPS_Data_tmp.Latitude.min = Latit
16、ude(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.Llisec = num2str(tmp-floor(tmp)*10000); GPS_Data_tmp.Longitude.degree = Longitude(1:3); % 經度 GPS_Data_tmp.Longitude.min = Longitude(4:5); tmp = st
17、r2double(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)
18、+8-24); 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
19、+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(co
20、unt+1)-1);count=count+1;% other = str_tab(index(count)+1:end);endfunction GPS_Data_tmp = GNGSV(str_tab)% 此語句為與衛(wèi)星有關的信息(包括衛(wèi)星方位,衛(wèi)星編號)% 暫時用不著,不處理 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);cou
21、nt=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);
22、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)-
23、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
24、); 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:4); tmp = str2double(Latitude(6:9); tmp = tmp*6/1000; % tmp = tmp/10000*60; GPS_Data_tmp.Latitude.sec = num2str(floor(tmp); GPS_Dat
25、a_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.sec = num2str(floor(tmp); GPS_Data_tmp.Longitude
26、.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; if str2num(hour)+8 >= 24 GPS_Data_tmp.Time.hour = num2str(str2num(hour)+8-24); else GPS_Data_tmp.Tim
27、e.hour = 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_
28、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(
29、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);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
30、; 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;
31、GPS_Data_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
32、_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 = str2double(Longitude(7:10); tmp = tmp*6/1000; % tmp = tmp/10000*60; GPS_Data_tmp.Longit
33、ude.sec = 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(
34、GPS_Data_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.DA
35、TE.day; 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.Lat
36、itude.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.Lon
37、gitude.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,'GPS
38、State') = 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')
39、= 1 GPS_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')
40、= 1 GPS_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 =
41、 GPS_Data_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.MagneticAn
42、gle; end 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.Relative
43、Dir = 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 GP
44、S_Data.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,.
45、 GPS_Data.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.Longitu
46、deDir,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.Location
47、State);% 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.day = '29' GPS_Data.DATE.month = '8'
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 筆的制造生產過程優(yōu)化與仿真技術考核試卷
- 紙板容器工廠環(huán)境噪聲治理考核試卷
- 石棉水泥制品的國內外質量標準對比考核試卷
- 外科個案護理專題分析
- 新生兒高膽紅素血癥業(yè)務查房
- 腦癱患者麻醉管理規(guī)范
- Heneicomycin-生命科學試劑-MCE
- 湖北省2025年中考第三次模擬考試道德與法治試卷(解析版)
- 房地產行業(yè)深度報告-“好房子”系列專題一:四代宅崛起政策紅利與產品創(chuàng)新驅動居住升級
- 2025年下半年食品飲料行業(yè)寒來暑往結構破局
- 變電工程施工合同協(xié)議書
- 電工技術-北京科技大學中國大學mooc課后章節(jié)答案期末考試題庫2023年
- 星海音樂學院樂理試題A卷
- 2019年4月27日山東省紀委監(jiān)委遴選公務員考試真題及答案
- ktv包房服務員崗位職責8篇
- 西安某大跨度鋼桁架人行天橋結構設計分析
- 新疆全部及全國部分加氣站分布情況6
- 初中學段勞動任務清單(七到九年級)
- 2023年中國各地磁偏角
- 六維領導力專題知識
- 【護士資格考試】云南省精神病醫(yī)院模擬檢測練習題
評論
0/150
提交評論