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

    %% Acquire Data from an OPC Historical Data Access Server
% This example shows you how to acquire data from an OPC Historical Data
% Access (HDA) server.
%
% *PREREQUISITES:*
%
% * <docid:opc_examples.example-ex84291595>

% Copyright 2010-2014 The MathWorks, Inc. 
% $Revision: 1.1.6.6 $  $Date: 2013/03/26 00:40:57 $

%% Start Historical Data Logging on the Server
% *NOTE*: _You do not normally need to execute this step on a production
% server._
%
% This example uses a simulation server that only logs historical data for items that are subscribed
% using an OPC Data Access client. Load the client object from a MAT file
% and reconnect the client.
daObjs = load('opcdemoHDAConfigure.mat');
connect(daObjs.opcdemoHDAConfigure);

%%
% Wait a while for the server to log some data.
pause(10);

%% Create an OPC HDA Client Object
% Create an OPC HDA Client associated with the OPC HDA Server.
hdaObj = opchda('localhost','matrikon.OPC.Simulation')

%%
% The client object manages the connection with the server, allows you to retrieve information
% about the server, browse the server name space, and to read data stored on the server.
%
% At this point, the client is not yet connected to the server. Connect the client to the server.
connect(hdaObj);

%%
% To confirm that the client is connected, display the client |Status| property.
hdaObj.Status

%% Define Items of Interest
% This example uses the |Real8| items from |Saw-toothed Waves| and the
% |Real8| and |UInt2| items from |Random|. Make a cell array of
% item names for ease-of-use.

itmIDs = {'Saw-toothed Waves.Real8', ...
    'Random.Real8', ...
    'Random.UInt2'};

%% Read Raw Data from the Server
% Read the raw data values from the historical server over the past day.
data = readRaw(hdaObj,itmIDs,now-1,now)

%%
% *Note:* The Matrikon server retains only the last 200 simulated values for each item.

%%
% Display the values of the first data element.
showValues(data(1))

%% Read Processed Data from the Server
% Query the |Aggregates| property of the HDA Client object to find out what 
% aggregate types the server supports.
hdaObj.Aggregates

%%
% The Matrikon server supports the time weighted average value, so we will use that aggregate
% type on 10 seconds of data for the last 1 minute. Note below how the |Aggregates| property can
% be used to specify the aggregate type.
pData = readProcessed(hdaObj,itmIDs,hdaObj.Aggregates.TIMEAVERAGE,10,now-1/24/60,now)'

%%
% Display the values for the |Random.Real8| item.
itmInd = getIndexFromID(pData,'Random.Real8');
showValues(pData(itmInd))

%%
% The last value has a quality of |'Uncertain'| because the time interval is not a complete 10
% seconds.

%% Clean Up
% When you have finished with the OPC Toolbox(TM) objects, delete
% them from the OPC Toolbox engine. Although deleting an HDA Client object automatically
% disconnects the object from the server, this example explicitly shows it.
disconnect(hdaObj)
delete(hdaObj)
disconnect(daObjs.opcdemoHDAConfigure);
delete(daObjs.opcdemoHDAConfigure);

%%
% The client object is now invalid.
isvalid(hdaObj)