Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation



06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

function comPorts = getavailablecom
% GETAVAILABLECOM fetches the available com ports on the system
% comPorts = getavailablecom returns comPorts as a cell array of available
% COM port names on this system.  NOTE it does not consider those COM ports
% which are open by other programs - only those open in MATLAB.

% if the instrument tool box is there...
if ~isempty(ver('instrument'))
    % gets the serial info
    info = instrhwinfo('serial');

    % gets the available ports
    comPorts = info.AvailableSerialPorts;
    % if that didn't work, do it an awkward cowboy way

    % fetches the standard error message featuring the list of available COM
    % ports - this sometimes does not work due to not being able to retrieve
    % the error message
        % generates an impossible serial object
        s = serial(num2str(rand));

        % attempts to connect to object - forcing an error

        % returns details of last error
        lastError = lasterror;

        % extracts out the message part of the last error - it contains a list
        % of COM ports in a long string
        serialError = lastError.message;

    % cleans up ports at end of error fetching

    % if the message is empty, error
    if isempty(lastError.message)
        % error
        error('Could not retrieve the error message to find the COM ports.')

    % finds the indices of each mention of the string 'COM' (the index actually
    % refers to the 'C')
    comStartPosition = findstr(serialError, 'COM');

    % finds the index of the first mention of the string 'Use' (3 is subtracted
    % from this to get the index of last digit of the COM name e.g. 'COM9')
    comEndPosition = findstr(serialError, 'Use') - 3;

    % extracts out the relevent part of the string
    comString = serialError(comStartPosition:comEndPosition);

    % finds the indices of the all of the commas in comString - this marks the
    % END of the COM[X] part of the string
    commaPositions = findstr(comString, ',');

    % different cases need different evaluations
    if isempty(comStartPosition)
        % if no COM ports are available, doesn't return anything
        comPorts{1} = '';

    elseif isempty(commaPositions)
        % if only one COM port is available, don't need to parse string
        comPorts{1} = comString;

        % extracts first COM port
        comPorts{1} = comString(1:commaPositions(1) - 1);

        % finds the number of comma positions
        numberCommaPositions = numel(commaPositions);

        % extracts the rest
        for m = 2:numberCommaPositions
            % extracts the COM[X] string by finding the next COM AFTER the
            % previous commaPosition, moving forward 2 to ignore the comma
            % and the space, and capturing it up to the next comma, then
            % subtracting 1 to remove the comma part
            comPorts{m, 1} = comString(commaPositions(m - 1) + 2:commaPositions(m) - 1);

        % extracts last COM port (doesn't have a comma at the end)
        comPorts{numberCommaPositions + 1, 1} = comString(commaPositions(numel(commaPositions)) + 2:end);

Contact us