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.

fibreobj2(com, userData)
function serialObject = fibreobj2(com, userData)
% FIBREOBJ2 Returns the serial object for the Fibre Optic Reflectometer
% serialObject = fibreobj2(com, userData) returns the serial object for the
% Fibre Optic Reflectometer (FOR) given a suitable COM port number
% (unsigned integer: 1-255), but implemented using Realterm. If an object
% already exists on that COM port (open or otherwise), it returns an error.
% Information can be passed into the serial object through the optional
% userData argument.

% e.g. serialObject = fibreobj2(9) - returns a serial object for the FOR on
% COM9

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

% Error handling
if ~iscomport(com)
    % errors
    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.')

% if object on specified COM port exists, errors, otherwise creates
% serial object

% 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', 19200,...
        'DataBits', 8,...
        'Parity', 'none',...
        'StopBits', 1,...
        'Terminator', '',...
        'TimeOut', 1,...
        'UserData', struct('activeChannels', [true, false]));

    % then creates the handle in the user data
        % tries to create it
        serialObject.UserData.realTermHandle = realterm(serialObject, 'Caption', 'Fibre Optic Reflectometer');

        % 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);

        % deletes the serial object

        % rethrows the error

    % errors
    error('Object(s) already exist on this serial port')

Contact us