image thumbnail

Multivariate Data Analysis and Monitoring for the Process Industries

by

 

26 Feb 2008 (Updated )

Files from the webinar Multivariate Data Analysis and Monitoring for the Process Industries

writeToOPC
function writeToOPC

data = load('metal');

%% Create main figure

f = figure('units', 'normalized', 'position', [0.1,0.1,0.2,0.2],...
    'menubar', 'none', 'toolbar', 'none', 'nextplot', 'new',...
    'name','OPC Write Utility','numbertitle','off','tag', 'writeToOPCGUI');

%% Save data in figure's appdata
setappdata(f, 'data', data.metals);

%% Add start and stop buttons, and textbox
uicontrol('style', 'pushbutton', 'string', 'Start',...
    'units', 'normalized', 'position', [0.2,0.7,0.2,0.2],...
    'tag', 'writeToOPCstartbut', 'callback', @writeToOPCstartfcn);

uicontrol('style', 'pushbutton', 'string', 'Stop',...
    'units', 'normalized', 'position', [0.6,0.7,0.2,0.2],...
    'callback', @writeToOPCstopfcn);

uicontrol('style', 'text',...
    'units', 'normalized', 'position', [0.2,0.2,0.6,0.4],...
    'tag', 'writeToOPCtextbox');

%% Callbacks

%% Start button callback
    function writeToOPCstartfcn(hObject,eventdata) %#ok
        % Get handle to main figure
        f = findobj('Tag', 'writeToOPCGUI');
        
        % Make a connection to the OPC server and add items to group
        da = opcda('localhost', 'Iconics.SimulatorOPCDA.2');
        connect(da);    
        metalsgroup = addgroup(da);
        additem(metalsgroup, 'Metals.Ag');
        additem(metalsgroup, 'Metals.Ni');
        additem(metalsgroup, 'Metals.Pb');
        additem(metalsgroup, 'Metals.Bi');
        additem(metalsgroup, 'Metals.Sb');
        additem(metalsgroup, 'Metals.As');
        additem(metalsgroup, 'Metals.Te');
        additem(metalsgroup, 'Metals.Se');

        % Store handles to connection and group in figure
        setappdata(f, 'dahandle', da);
        setappdata(f, 'grouphandle', metalsgroup);

        % Set time to zero and store in figure
        time = 0;
        setappdata(f, 'time', time);

        % Create a timer object
        t = timer('ExecutionMode', 'fixedRate', 'Period', 0.5, 'BusyMode', 'drop',...
            'Tag', 'writeToOPCtimer', 'TimerFcn', @writeToOPCtimerfcn);

        % Gray out the start button
        startbut = findobj('tag', 'writeToOPCstartbut');
        set(startbut, 'Enable', 'off');

        % Start the timer
        start(t);

%% Timer callback
        function writeToOPCtimerfcn(hObject,eventdata) %#ok
            % Get handle to main figure
            f = findobj('Tag', 'writeToOPCGUI');
            
            % Retrieve the time, data, and handle to the OPC item group
            time = getappdata(f, 'time');
            metalsgroup = getappdata(f, 'grouphandle');
            metals = getappdata(f, 'data');
            
            % Increase the time, looping round end of dataset
            time = mod(time,730)+1;
            
            % Write the latest measurement to the OPC item group
            write(metalsgroup, num2cell(metals(time,:)));
            
            % Report the write to the textbox
            textbox = findobj('Tag', 'writeToOPCtextbox');
            string = sprintf('Time = %3.0f\nAg = %3.2f, As = %3.2f, Bi = %3.2f, Ni = %3.2f,\nPb = %3.2f, Sb = %3.2f, Se = %3.2f, Te = %3.2f',...
                time, metals(time,1),metals(time,2),metals(time,3),metals(time,4),...
                metals(time,5),metals(time,6),metals(time,7),metals(time,8));
            set(textbox, 'String', string)
            
            % Update the time to the figure
            setappdata(f, 'time', time);
        end % timer callback

    end % start button callback

%% Stop button callback
    function writeToOPCstopfcn(hObject,eventdata) %#ok
        % Get handle to main figure
        f = findobj('Tag', 'writeToOPCGUI');
        
        % Get handle to timer
        t = timerfind('Tag','writeToOPCtimer');
        
        % Quit the timer
        stop(t);
        delete(t);
        
        % Disconnect the OPC connection
        da = getappdata(f, 'dahandle');
        disconnect(da);
        
        % Close the figure
        close(f);
    end % stop button callback

end % main function

Contact us