Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

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 ~isserial(serialObject) || (~isfield(serialObject.UserData, 'realTermHandle') && ~isrunning(serialObject))
    % the serial object must be valid and open to send or receive data
    % (unless there is a realterm handle available)
    error('serialObject must be a valid open serial object')
    
elseif ~isserial(serialObject) || (isfield(serialObject.UserData, 'realTermHandle') && ~isrunning(serialObject.UserData.realTermHandle))
    % if the handle is there, the object must be running (PortOpen is 1)
    error('If the Realterm handle is present, the object must be connected via Realterm.')
        
elseif isfield(serialObject.UserData, 'realTermHandle') && (~isfield(serialObject.UserData, 'captureFileID') || ~isfid(serialObject.UserData.captureFileID))
    % if using realterm, the capture file ID must be there
    error('If using Realterm, the capture file handle must be in the serial object''s UserData.')
    
elseif ~ischar(command) || ~isvector(command) || size(commandParse{1}, 1) > 5
    % errors
    error('Command must be a string with no more than 5 words.')
end

% flushes out anything in the buffer
serialflush(serialObject);

% sends command
serialprint(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 = serialscan(serialObject);

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

% 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
    serialscan(serialObject);
    response = serialscan(serialObject);
end

% removes the carriage return terminator at the end (not sure if this is
% necessary with realterm)
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.')
        
    otherwise
        % unknown response
        warning('bprObjComm:unknownResponse', 'Unknown response from BPR.')
end

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

% 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
pause(0.1)

Contact us