Code covered by the BSD License  

Highlights from
Rheodyne valve RS232 communications

Rheodyne valve RS232 communications

by

 

Commands for communicating with Rheodyne valves via RS232

rheodyne232obj(comPort, userData)
function serialObject = rheodyne232obj(comPort, userData)
% RHEODYNE232OBJ Returns the serial object for Rheodyne serial comms
% serialObject = rheodyne232obj(comPort, userData) returns the serial
% object for Rheodyne serial-controlled valves. given a suitable COM port
% number (unsigned integer: 1-255). 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 = rheodyne232obj(5) - returns a serial object for the
% valve on COM5


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

% Error handling
if ~isscalar(comPort) || comPort ~= uint8(comPort) || ~comPort
    % errors
    error('comPort must be an unsigned integer from 1 to 255')
end

% 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 serial object
    serialObject = serial(  serialPort,...
        'BaudRate', 19200,...
        'DataBits', 8,...
        'Flowcontrol', 'none',...
        'Parity', 'none',...
        'StopBits', 1,...
        'Terminator', {'', 'CR'},...
        'TimeOut', 2);
    
    % creates the timer object
    timerObject = rheodyne232objtimer(serialObject);
    
    % if userData was provided, modify the object (infernal MATLAB not
    % understanding the timers properly)
    if nargin >= 2
        % modifies the user data first
        userData.timerObject = timerObject;
        
        % changes it
        serialObject.UserData = userData;
        
    else
        % store it in the user data
        serialObject.UserData = struct('timerObject', timerObject);
    end
    
else
    % errors
    error('Object(s) already exist on this serial port')
end

Contact us