function [lat, lon, ActiveRMC] = ReadGarmin(serGPS) % Reads GPS via polling. If no data, does not wait, returns nans. % serial port must first be initialized using initializeGPSlovett % inputs: serGPS (from initializeGPSovett) % relToX, relToY, some initial position that you may want to index position from (in % meters) % if you want raw measurements use 0,0 % Outputs: x (lon) and y(lat) position in meters, % speed in m/sec, heading of VELOCITY vector (not vehicle!) % % four ways to terminate: % 1. get expected data! % OR if no data available returns NANs %Screen message tell you why. Here are possibilities: % 2. No data there at all (GPS is dead, between updates, or not connected) % 3. Data there but not what I need / garbled (RMC sentence) % 4. Data there but experinced error reading it! (unexpected? not normal) %% initialize to nan lat =nan; lon = nan; header=nan; e_stop=nan; MS_TIMER=nan; heading=nan; Roll=nan; Pitch=nan; Yaw=nan; samples=nan; footer=nan; V = nan; W = nan; goodSentence = []; ActiveRMC= nan; Mx = nan; My = nan; %% IF THERE IS NO DATA? if (get(serGPS, 'BytesAvailable')==0) disp('Data not avail. Try again or check transmitter.') return end %% IF THERE IS DATA while (get(serGPS, 'BytesAvailable')~=0) % read until terminator sentence = fscanf(serGPS, '%s'); Ns = length(sentence); try % I am using the RMC sentences, don't want partial sentences either if strcmp(sentence(1:6),'$GPRMC') % & (Ns >10) %strip off data (not all used) [prefixRMC,timeRMC,ActiveRMC,lat,latdirRMC,lon,londirRMC,spdKnots,AngleDeg] = strread(sentence, '%s%f%s%f%s%f%s%f%f', 1, 'delimiter', ','); %disp('Success!') if isempty(lat)||(ActiveRMC{1} ~='A') lat = nan; end if isempty(lon)||(ActiveRMC{1} ~='A') lon = nan; end goodSentence = sentence; end catch ERR_MSG %this shoudl never happen! %disp('Error Reading Data! Check Code') %clear sentence end end