www.gusucode.com > opc 案例源码 matlab代码程序 > opc/opcdemo_quickstart.m

    %% Acquire Data from an OPC Data Access Server
% This example shows the basic steps involved in using OPC Toolbox(TM)
% to acquire data from an OPC Server.
%
% *PREREQUISITES:*
%
% * <docid:opc_examples.example-ex84291595>

% Copyright 2004-2014 The MathWorks, Inc. 
% $Revision: 1.1.6.10 $  $Date: 2012/08/01 00:52:27 $

%% Create OPC Data Access Object Hierarchy
% Create an |opcda| object associated with the required server and connect to the server.
da = opcda('localhost','Matrikon.OPC.Simulation.1')
connect(da)

%%
% Create a group object to manage the required items.
grp = addgroup(da,'DemoGroup')

%%
% Add the |Real8| item from |Saw-Toothed Waves| and the
% |Real8| and |UInt2| items from |Triangle Waves| to the group.
itmIDs = {'Saw-toothed Waves.Real8', ...
    'Triangle Waves.Real8', ...
    'Triangle Waves.UInt2'};
itm = additem(grp,itmIDs)

%% Configure OPC Toolbox Object Properties
% Configure the group to log 60 seconds of data at 0.2 second intervals.
logDuration = 60;
logRate = 0.2;
numRecords = ceil(logDuration./logRate)
grp.UpdateRate = logRate;
grp.RecordsToAcquire = numRecords;

%% Acquire OPC Server Data
% Start the acquisition task, and wait for the task to complete before continuing execution of any
% MATLAB(TM) code.
start(grp)
wait(grp)

%%
% Note that while waiting for a logging task to complete, MATLAB continues to process callbacks
% from OPC Toolbox objects (and other objects that include callback functionality).
%
% Retrieve the logged data into separate arrays for the time stamps, quality, and values.
[logIDs,logVal,logQual,logTime,logEvtTime] = getdata(grp,'double');

%%
% Examine the workspace for the sizes of the data.
whos log*

%% Plot the Data
% You can now plot this data all on one set of axes. The |datetick| function
% converts the X-axis ticks into formatted date strings.
plot(logTime,logVal);
axis tight
datetick('x','keeplimits')
legend(logIDs)

%%
% The value data does not provide the full picture. You should always
% examine the quality of the data to determine the validity of the value
% array. 
%
% Annotate the plot with markers where the quality is
% not |Good|.
hold on
isBadQual = strncmp(logQual,'Bad',3);
isRepeatQual = strncmp(logQual,'Repeat',6);
for k = 1:size(logQual,2)
    badInd = isBadQual(:,k);
    plot(logTime(badInd,k),logVal(badInd,k),'ro', ...
        'MarkerFaceColor','r','MarkerEdgeColor','k')
    repInd = isRepeatQual(:,k);
    plot(logTime(repInd, k),logVal(repInd,k),'ro', ...
        'MarkerFaceColor',[0.8 0.5 0],'MarkerEdgeColor','k')
end
hold off

%%
% Bad quality is marked in red, and Repeat quality is marked in
% orange. 

%% Clean Up
% Disconnect and delete the client object from the OPC Toolbox engine.
disconnect(da)
delete(da)