Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

isvalidserial(serialObject)
function response = isvalidserial(serialObject)
% ISVALIDSERIAL checks if an object is a valid serial object, i.e. it
% checks the COM port too - requires the Instrument Toolbox.

% if the instrument tool box isn't there...
if isempty(ver('instrument'))
    % errors
    error('Instrument Toolbox not available.')
end

% checks the number of arguments
error(nargchk(1, 1, nargin))

% if its a structure, error
if isstruct(serialObject)
    % errors
    error('Not defined for structures.')
end

% different tests for different types
if iscell(serialObject)
    % recursively call this function
    response = cellfun(@isvalidserial, serialObject, 'UniformOutput', false);
    
else
    % if it is a serial object (by definition a 1x1 serial object)
    if isa(serialObject, 'serial');
        % checks the serial object for validity
        response = isvalid(serialObject);
        
        % then checks the COM port if necessary
        if response
            % checks it
            response = isvalidcomport(serialObject.Port);
        end
    
    % a serial object that is larger than 1x1 becomes an instrument object
    elseif isa(serialObject, 'instrument');
        % if its a vector, turn it into a column vector so the next part
        % can be more efficient
        if isvector(serialObject)
            % reshapes it
            serialObject = serialObject(:);
        end
        
        % pre-defines the answer
        response = false(size(serialObject));
        
        % defines the indices for each column (this is because of a bizarre
        % shortcoming where you can't do half-logical, half-subscript
        % indexing with serial objects)
        index = 1:size(serialObject, 1);
        
        % apply isvalid on each column (doesn't work on arrays)
        for m = 1:size(serialObject, 2)
            % tests each column (could equally do this by rows)
            response(:, m) = isvalid(serialObject(:, m));
            
            % tests the valid ones
            response(response(:, m), m) = isvalidcomport(serialObject(index(response(:, m)), m).Port);
        end
        
    else
        % generates a false response the same size as the serialObject
        response = false(size(serialObject));
    end
end

Contact us