Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation



06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

rheodyne232objcomm(serialObject, command)
function output = rheodyne232objcomm(serialObject, command)
% RHEODYNE232OBJCOMM Reads and writes data from Rheodyne valves
% rheodyne232objcomm(serialObject, command) reads or writes
% information from Rheodyne valves 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. rheodyne232objcomm(serialObject, 'S') for the current valve status.

% Range:

% serialObject = valid serial object
% command = string

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

% 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) && ~isscalar(command))
    % errors
    error('Command must be a string.')

% flushes out anything in the buffer

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

% leaves a short pause to ensure the communication works (this may not be
% strictly necessary)

% while the data is not what we expected (maximum 10 goes before giving up)
for m = 1:10
    % gets the data
    response = fetchData(serialObject);

    % if its '*' or empty, then go round again, otherwise...
    if ~isequal(response, '*') || ~isempty(response)
        % escape

% defines the possible error codes
statusCodes = { 99, 'Valve failure (valve cannot be homed.';...
                88, 'Non-volatile memory error';...
                77, 'Valve-configuration error or command mode error;...'
                66, 'Valve positioning error';...
                55, 'Data integrity error';...
                44, 'Data CRC error'};

% defines the numeric version of the response
numResponse = str2double(char(response));

% tries to match it
statusIndex = find(cell2mat(statusCodes(:, 1)) == numResponse);

% if its empty, it is information (the valve position in this case)
if ~isempty(statusIndex)
    % then error
    error(['Valve error: ', statusCodes{2, statusIndex}])

% if the response is empty, errors
if isempty(response)
    % errors
    error('No response from Rheodyne.')
elseif isequal(response, '*')
    % errors
    error('Rheodyne was busy and command was not executed.')
elseif isequal(response, 13)
    % don't need to do anything - this is the acknowledge of a successful
    % command
elseif isnan(numResponse)
    % error
    error('NaN response from the Rheodyne.')
    % assign the output
    output = numResponse;

function output = fetchData(serialObject)
% checks response - this is also an automatic check for being able to
% send the next command - if it has understood the command and has sent a
% response - its certainly ready for another command

% reads response
output = serialread(serialObject)';

% if the response is larger than one, it has useful information
if numel(output) > 1
    % remove the carriage return, and return the rest
    output = output(1:end - 1);

Contact us