function [serGPS] = initializeGarmin(ComPortNumber)
%This function initializes the serial port properly for use with the Garmin GPS
%COMPORT is the number of the serial port:   ex use 3 for 'COM3'
% port number can be checked in 
% Control Panel/System/Hardware/DeviceManager/Ports
% serGPS output is a matlab serial port object
% Esposito 6/25/2008 with code from regneier, bishop, et al; modified by
% MIDN 1/C Li


port = strcat('COM',num2str(ComPortNumber) );

out = instrfind('Port', port);  % Check to see if THAT serial port is already defined in MATLAB
if (~isempty(out))  % It is
    disp('WARNING:  port in use.  Closing.')
    if (~strcmp(get(out(1), 'Status'),'open'))  % Is it open?
        delete(out(1)); % If not, delete
    else  % is open
        fclose(out(1));
        delete(out(1)); % Otherwise, let user know it was never closed
    end
end


serGPS = serial(port);  % Define serial port
set(serGPS, 'BaudRate', 19200);  % Default Baud rate of 19200
set(serGPS, 'Tag', 'GPS');   % give it a name
set(serGPS, 'TimeOut', .1);
% want to make the buffer big enough that new message can always fit
%(example is 389 characters long but messsage length is variable)
% but not so big as to hold 2 messages
set(serGPS, 'OutputBufferSize', floor(389*1.5) );
set(serGPS, 'InputBufferSize',  floor(389*1.5) );
fopen(serGPS);  % Open it;
pause(1)  % give it a second to start getting data, Maybe not nessecary
 disp('Garmin Initialized')