%% serial_comm.m
% BJ Furman
% 29AUG2015
%
%% Open a serial port object
% Check if the serial port is already defined in Matlab
% If it is already defined and is not open, then delete it.
% If it is already defined and it's open, close it, then delete it.
port_info = instrfind('Port', 'COM22');
if(~isempty(port_info)) % TRUE if port is defined
    disp('WARNING: port in use. Closing!')
    if(~strcmp(get(port_info(1),'Status'),'open')) % TRUE if not open
        delete(port_info(1));
    else    % it is open
        fclose(port_info(1));
        delete(port_info(1));
    end
end
% Define the serial port object, and open it
EOL = 'CR/LF';
psoc_uart = serial('COM22', 'baudrate', 9600, 'Terminator', EOL); % Your COMM number will be different
fopen(psoc_uart);
get(psoc_uart)  % Let's see the full list of parameters. Comment out later.

% fclose(psoc_uart);
% delete(psoc_uart);