Code covered by the BSD License  

Highlights from
Realterm communications suite

Realterm communications suite

by

 

06 Jan 2010 (Updated )

Use Realterm in a similar way to using the MATLAB "serial" commands and instrument control toolbox.

realtermfread(handle, captureFileID, timeOut, maxCharacters, precision)
function output = realtermfread(handle, captureFileID, timeOut, maxCharacters, precision)
% REALTERMFREAD used similarly to fread with serial objects
% (only limited to one output argument at present)

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

% error handling
if ~isrealtermhandle(handle)
    
    % 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)
    
    % the capture file ID must be correct
    error('Capture file ID is invalid.')

elseif nargin >= 3 && (~isscalar(timeOut) || ~isnumberbetween(timeOut, 0, Inf))
    
    % the timeout must be greater than 0
    error('Timeout must be egreater than 0.')
    
elseif nargin >= 4 && (~isscalar(maxCharacters) || ~isreal(maxCharacters) || isnan(maxCharacters) || ~maxCharacters)
    
    % the max characters must be a number bigger than 0 (Inf is allowed)
    error('Max characters must be a number bigger than 0.')
    
elseif nargin >= 5 && ~isstring(precision)
    
    % the precision must be a string
    error('The precision must be a string.')
end

% if the timeout wasn't supplied, use a default
if nargin < 3 || isempty(timeOut)
    
    % set it to 1
    timeOut = 1;
end

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

% if the precision wasn't specified, use the default
if nargin < 5
    
    % set it to uchar
    precision = 'uchar';
end

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

% defines an output
output = zeros(0, 1);

while numel(output) < maxCharacters && (now * 24 * 60 * 60 - startTime) < timeOut
    
    % tries to read it out
    output = vertcat(output, fread(captureFileID, maxCharacters, precision));
end

% if at the end it was a timeout and the max characters was not reached...
if (now - startTime) > timeOut && isfinite(maxCharacters) && numel(output) < maxCharacters
    
    % display a warning
    warning('realtermfread:timeOut', 'Communication timed out.')
end

Contact us