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.

tempobj2(com, userData)
function serialObject = tempobj2(com, userData)
% TEMPOBJ Returns the serial object for Eurotherm temperature controllers
% serialObject = tempobj(com) returns the serial object for Eurotherm series
% 2000 temperature controllers 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 = tempobj(7) - returns a serial object for the temperature
% controller on COM7


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

% error handling
if ~iscomport(com)
    % COM ports 1-255 allowed only
    error('com must be an unsigned integer from 1 to 255')
    
elseif nargin >= 2 && ~isstruct(userData)
    % user data must be a structure to be used
    error('User data must be a structure when used with Realterm serial objects.')
end

% initialises variables
serialPort = sprintf('COM%d', com);

% if serial object does not exist, defines serial object and opens it
if isempty(instrfindall('Port', serialPort))
    % creates serial object
    serialObject = serial(  serialPort,...
        'BaudRate', 9600,...
        'DataBits', 8,...
        'Parity', 'none',...
        'StopBits', 1,...
        'Terminator', '',...
        'TimeOut', 1);

    % then creates the handle in the user data
    try
        % tries to create it
        serialObject.UserData.realTermHandle = realterm(serialObject, 'Caption', 'Eurotherm Temperature Controller');
        
        % if userData was provided and it was a structure, modify the serial object
        if nargin >= 2
            % change the user data - note that the existing serial data has
            % priority
            serialObject.UserData = catstructs(userData, serialObject.UserData);
        end
        
    catch
        % deletes the serial object
        delete(serialObject)
        
        % rethrows the error
        rethrow(lasterror)
    end
    
else
    % errors
    error('Object(s) already exist on this serial port')
end

Contact us