Code covered by the BSD License  

Highlights from
Thar ABPR-20 RS232 communication via handheld emulation

Thar ABPR-20 RS232 communication via handheld emulation

by

 

20 May 2013 (Updated )

Controls and reads from a Thar ABPR-20, by emulating the handheld controller.

serialflush(serialObject, varargin)
function output = serialflush(serialObject, varargin)
% SERIALFLUSH reads from a serial object depending on if it contains the
% Realterm handle or not.

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

% checks that a serial object has been passed
if ~isserial(serialObject)
    % errors
    error('Must supply a serial object.')
end

% if the size is supplied, change it to Inf to read out everything, while
% still allowing the use of the precision argument
if nargin >= 2
    % set it to inf
    varargin{1} = Inf;
end

% finds if it should be realterm or not
if isfield(serialObject.UserData, 'realTermHandle')
    % checks the rest of the fields are there
    if ~isfield(serialObject.UserData, 'captureFileID')
        % errors
        error('Cannot retrieve the capture file ID.')
        
    elseif ~isrunning(serialObject.UserData.realTermHandle)
        % errors
        error('Realterm is not connected.')
    end
    
    % read everything out
    output = fread(serialObject.UserData.captureFileID, varargin{:});
    
else
    % checks its connected
    if ~isrunning(serialObject)
        % errors
        error('Serial port is not open.')
    end
    
    % if there is some data to flush
    if serialObject.BytesAvailable
        % if there's some arguments...
        if nargin > 1
            % read it out conventionally
            output = fread(serialObject, varargin{:});

        else
            % read out all the bytes
            output = fread(serialObject, serialObject.BytesAvailable);
        end
        
    else
        % return something empty
        output = [];
    end
end

Contact us