Code covered by the BSD License  

Highlights from
Knauer pump RS232 communication

Knauer pump RS232 communication

by

 

Functions for communicating with Knauer pumps via RS232.

knauerpumpobj(comPort, userData)
function serialObject = knauerpumpobj(comPort, userData)
% KNAUERPUMPOBJ Returns the serial object for Knauer pumps
% serialObject = knauerpumpobj(comPort) returns the serial object for
% Knauer pumps 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.

% Normal usage:

% e.g. serialObject = knauerumpobj(9) - returns a serial object for the
% Knauer pump on COM9.


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

% Error handling
if ~isscalar(comPort) || ~isreal(comPort) || isnan(comPort) || comPort < 1 || comPort > 255 || comPort ~= round(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 the serial object (note that the first terminator is for
    % reading, and the second terminator is for writing)
    serialObject = serial(  serialPort,...
        'BaudRate', 9600,...
        'DataBits', 8,...
        'Parity', 'none',...
        'StopBits', 1,...
        'Flowcontrol', 'none',...
        'Terminator', 'CR',...
        'TimeOut', 1);

    % if userData was provided, modify the object
    if nargin >= 2
        
        % changes it
        serialObject.UserData = userData;
    end
    
else
    
    % errors
    error('Object(s) already exist on this serial port')
end

Contact us