% General purpose Function for any command

% Inputs: two strings 'a' and 'b', indicating the IS command and the 
% port number respectively.
% For example: a = '@ndl 500','@gtv 10', etc.. 
% and b = 'COM2', 'COM3', etc..;


function [out] = autosampler(command)

port = 'COM6';

a = command; b = port;

p = serial(b); % creating a serial port object for input port 'b'

set(p,'BaudRate',4800)
set(p,'Terminator','CR')
set(p,'Timeout',180)
fopen (p)
set(p,'BaudRate',4800)
set(p,'Terminator','CR')
set(p,'Timeout',180)
fprintf(p,a)

%out = fscanf(p);
out = fgets(p);
if(str2num(out) == -128)
    out = fgets(p);
end
%disp(['Got response from fgets for command ' a]);
%while str2num(out) == -128
%    out = fscanf(p);
%end
    
fclose(p)
delete(p)
clear p
%disp(['Closed port after completing command ' a]);
if str2num(out) ~= 0
    %warning(['Invalid Entry in instruction' a ' for port ' b 'Output' out]);
%    warning on debug
end


