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.

tharbprobj2(comPort, userData)
function serialObject = tharbprobj2(comPort, userData)
% THARBPROBJ2 Returns the serial object for Thar back-pressure regulators
% serialObject = tharbprobj2(com, userData) returns the serial object for
% Thar back-pressure regulators given a suitable COM port number (unsigned
% integer: 1-255), for use with the Realterm helper application. 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 = tharbprobj2(5) - returns a serial object for the BPR
% on COM5.

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

% error handling
if ~iscomport(comPort)
    % errors
    error('com must be an unsigned integer from 1 to 255')
elseif nargin >= 2 && ~isstruct(userData)
    % user data must be a structure to be used
    error('User data must be a structure when used with Realterm serial objects.')

% 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 the serial object
    serialObject = serial(  serialPort,...
                            'BaudRate', 9600,...
                            'DataBits', 8,...
                            'Parity', 'none',...
                            'StopBits', 1,...
                            'Flowcontrol', 'none',...
                            'Terminator', char(27),...
                            'TimeOut', 0.5);
    % then creates the handle in the user data
        % tries to create it
        serialObject.UserData.realTermHandle = realterm(serialObject, 'Caption', 'Thar ABPR-20');
        % if userData was provided and it was a structure, modify the serial object
        if nargin >= 2
            % change the user data - note that the existing serial data has
            % priority
            serialObject.UserData = catstructs(userData, serialObject.UserData);
        % deletes the serial object
        % rethrows the error

    % errors
    error('Object(s) already exist on this serial port')

Contact us