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.

realtermfscanf(handle, captureFileID, terminator, timeOut, varargin)
function output = realtermfscanf(handle, captureFileID, terminator, timeOut, varargin)
% REALTERMSCANF reads out a "line" up to the terminator specified

% checks the number of input arguments
error(nargchk(4, 6, nargin))

% pulls out the arguments
if numel(varargin) == 2
    % stores them
    precision = varargin{1};
    maxCharacters = varargin{2};
    
elseif numel(varargin) == 1
    % empties the varargin
    varargin = cell(0, 1);
end

% error handling
if ~isa(handle, 'COM.realterm_realtermintf')
    % the real term handle must be a valid handle
    error('The realTermHandle is not valid.')
    
elseif ~isrunning(handle)
    % realterm must be connected
    error('Realterm is not connected to the serial object.')
    
elseif any(strfind(handle.Capture, 'off'))
    % capture must be started
    error('Data capture is not on.')
    
elseif ~isfid(captureFileID)
    % file id must be valid
    error('Capture file ID is invalid.')
    
elseif ~isnumeric(terminator) && ~ischar(terminator)
    % terminator must be a string or a number
    error('Terminators may only be a string or number.')
    
elseif ~isscalar(timeOut) || ~isnumberbetween(timeOut, 0, Inf) || ~timeOut
    % the timeout must be either by empty or a number greater than or equal
    % to 0
    error('Timeout must be either empty (no timeout) or greater than 0.')
    
elseif numel(varargin) >= 1 && ~isstring(precision)
    % errors
    error('Precision must be a valid string.')
    
elseif numel(varargin) >= 2 && (~isnumeric(maxCharacters) || ~isscalar(maxCharacters) || ~maxCharacters)
    % errors (note that floating point characters are allowed, although
    % fscanf will floor those numbers (i.e. round towards -Inf)
    error('Maximum characters must be a scalar number bigger than 0.')
end

% if the maximum characters wasn't supplied, set it to Inf
if nargin < 6
    % set it to Inf
    maxCharacters = Inf;
end

% specify a start time (in seconds)
startTime = now * 24 * 60 * 60;

% defines the precision
precision = '*char';

% initialses output
output = '';

% keeps on going until it matches or hits the end of the file, or
% the timeout is breached (doesn't use for loop since you can't do
% 1:Inf as a counter)
while numel(output) < maxCharacters
    % defines the logical responses
    reachedTerminator = numel(output) >= numel(terminator) && all(output(end - numel(terminator) + 1:end) == terminator);
    timedOut = (now * 24 * 60 * 60 - startTime) > timeOut;

    % if any of those are reached, then leave the loop
    if reachedTerminator || timedOut
        % leave
        break

    else
        % reads another byte out
        output = [output, fread(captureFileID, 1, precision)];
    end
end

Contact us