Code covered by the BSD License  

Highlights from
Rheodyne valve RS232 communications

Rheodyne valve RS232 communications



Commands for communicating with Rheodyne valves via RS232

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. When sending commands to be written (e.g. a
% valve position change), waits for confirmation from the device, which is
% not sent until the device has finished turning (quite slow) - use
% fastcomm if this confirmation is not required.

% 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 ~isa(serialObject, 'serial') || ~isvalid(serialObject) || ~strcmp(serialObject.Status, 'open')
    % errors
    error('serialObject must be a valid open serial object.')

% checks the command is a valid string    
elseif ~ischar(command) || (~isvector(command) && ~isscalar(command))
    % errors
    error('Command must be a string.')

% flushes out anything in the buffer

% sends command
fprintf(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 = fread(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