Code covered by the BSD License  

Highlights from
Thar ABPR-20 RS232 communication via handheld emulation

Thar ABPR-20 RS232 communication via handheld emulation



20 May 2013 (Updated )

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

tharbprobj(comPort, userData)
function serialObject = tharbprobj(comPort, userData)
% THARBPROBJ Returns the serial object for Thar back-pressure regulators
% serialObject = tharbprobj(com, userData) returns the serial object for Thar
% back-pressure regulators given a suitable COM port number (unsigned
% integer: 1-255). If an object already exists on that COM port (open or
% otherwise), returns an error.  Information can be passed into the serial
% object through the optional userData argument.

% e.g. serialObject = tharbprobj(4) - returns a serial object for the BPR
% on COM4

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

% error handling
if ~isscalar(comPort) || comPort ~= uint8(comPort) || ~comPort
    % errors
    error('comPort must be an unsigned integer from 1 to 255')
elseif nargin >= 2 && ~isstruct(userData)
    % user data must be a structure
    error('If supplied, user data must be a structure.')

% There are currently issues with the Thar BPR Obj, possibly due to the
% large volume of data it collects.  After an indeterminate period of time,
% MATLAB errors with a C++ buffer over-run, and then must be closed.  The
% COM port the Thar BPR is attached to (COM5 at the moment) is then not
% accessible until the computer is restarted.  Sometimes, the fclose/fopen
% methods works for a long time, but then on doing anything in MATLAB,
% fopen will fail.  Attempting to delete and recreate the serial object
% results in a Java error.

% To try to fix:
% 1. Reduce the buffer size on purpose (this will break the readnoflush
% commands).
% 2. Increase the buffer size to 100 MB
% 3. Try emptying the buffer every 1, 2, or 10 minutes.
% 4. Try fopen/fclose again every 1 minute, then 2 or 10 minutes.
% 5. Replace this entirely using RealTerm (most awkward).

% Could possibly set the BytesAvailableCount to 1000 and then use that, to
% decrease the computational load.

% if object on specified COM port exists, errors, otherwise creates
% serial object

% initialises variables
serialPort = sprintf('COM%d', comPort);

% if serial object does not exist, defines serial object and opens it
if isempty(instrfindall('Port', serialPort))
    % creates serial port
    serialObject = serial(  serialPort,...
        'BaudRate', 9600,...
        'DataBits', 8,...
        'Parity', 'none',...
        'StopBits', 1,...
        'Flowcontrol', 'none',...
        'InputBufferSize', 1024 * 1000,...
        'Terminator', char(27),...
        'TimeOut', 0.5);

    % if userData was provided, modify the object
    if nargin >= 2
        % sets it
        serialObject.UserData = userData;
    % errors
    error('Object(s) already exist on this serial port')

Contact us