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.

serialbytesavailable(serialObject)
function bytesAvailable = serialbytesavailable(serialObject)
% SERIALBYTESAVAILABLE gives the bytes available for a serial object
% bytesAvailable = serialbytesavailable(serialObject) reads the bytes
% available for any type of serial object

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

% error handling
if ~isserial(serialObject)
    % errors
    error('serialObject must be a valid serial object.')
end

% if its real term...
if isfield(serialObject.UserData, 'realTermHandle')
    % if its not running, then by definition its 0
    if ~isrunning(serialObject.UserData.realTermHandle)
        % set it to 0
        bytesAvailable = 0;
        
    elseif ~isfield(serialObject.UserData, 'captureFileID')
        % then error, because if its open, and there is no capture file ID,
        % then we can't read what's coming out
        error('Device is connected through Realterm and is open, but unable to find the capture file.')
        
    else
        % get the current position of the file (with respect to the
        % beginning of the file)
        filePosition = ftell(serialObject.UserData.captureFileID);
        
        % seek to the end
        fread(serialObject.UserData.captureFileID);
        
        % gets the new position
        endPosition = ftell(serialObject.UserData.captureFileID);
        
        % seek back to the original position
        fseek(serialObject.UserData.captureFileID, filePosition, 'bof');
        
        % calculates the bytes left
        bytesAvailable = endPosition - filePosition;
    end
    
else
    % return the serial object property as normal
    bytesAvailable = serialObject.bytesAvailable;
end

Contact us