Code covered by the BSD License  

Highlights from
Jasco BPR RS232 communication

Jasco BPR RS232 communication



20 May 2013 (Updated )

Drivers and higher level commands for controlling Jasco BPRs via RS232.

bprobjcomm(serialObject, command)
function output = bprobjcomm(serialObject, command)
% BPROBJCOMM Reads and writes data from Jasco back-pressure regulators
% bprobjcomm(serialObject, command) reads or writes information from Jasco
% BP-1580-81 (and newer) back-pressure regulators connected via serial
% port. serialObject is the serial port object, and command is the command
% (as a string). If reading a value, responds with the value or a
% horizontal array of values if successfully carried out.

% Normal usage:

% e.g. bprobjcomm(serialObject, '#0x1000 0x12') for current
% pressure and temperature returns output from BPR as a string.

% Range:

% serialObject = valid serial object
% command = string

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

% pre-parses command for error checking - splits it up into words
commandParse = textscan(command, '%s');

% error handling
if ~isa(serialObject, 'serial') || ~isvalid(serialObject) || ~strcmp(serialObject.Status, 'open')
    % errors if not a valid serial object or is closed
    error('serial must be a valid open serial object.')

% checks validity of command
elseif ~ischar(command) || ~isvector(command) || size(commandParse{1}, 1) > 5
    % errors
    error('Command must be a string with no more than 5 words.')

% flushes out anything in the buffer (sometimes errors otherwise)

% sends command
fprintf(serialObject, command, 'async')

% checks response - this is also an automatic check for being able to
% send the next command - if its understood the command and has sent a
% response - its certainly ready for another command

% reads response
response = fscanf(serialObject);

% if the response is empty, errors
if isempty(response)
    % errors
    error('No response from BPR')

% for some peculiar reason, the start ('#0x16') and stop ('#0x17)
% commands output 3 lines of text so need to be read to correctly empty
% out the input buffer
if any(strcmp(command, {'#0x16', '#0x17'}))
    % reads the lines out
    response = fscanf(serialObject);

% removes the carriage return terminator at the end
response(end) = '';

% checks the first part of the response, removing any spaces
switch deblank(response(1:4))
    case '#0 0'
        % do nothing - the command was successful

    case '#0 1'
        % invalid command
        error('Communications error: unknown command.')

    case '#0 2'
        % invalid parameter
        error('Communications error: invalid parameter.')

    case '#0 3'
        % BPR was busy
        error('Communications error: command not executed - BPR busy.')

    case '#0 4'
        % generic communications error
        error('Communications error.')
    case '#0 10'
        % generic error
        error('Communications error: unknown error.')
        % unknown response
        warning('bprObjComm:unknownResponse', 'Unknown response from BPR.')

% if the response was more than 5 characters long then it was a read
% command, so return data
if numel(response) > 5
    % sets the output
    output = response;

% added a pause because some commands seem to take a little longer to
% get through - so allowing time for the command to arrive before it
% gets flush from the input if an issue with overlap has occurred

Contact us