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

    %% Log Data from an OPC Data Access Server
% This example shows you how to configure and execute a logging session,
% and how to retrieve data from that logging session.
%
% *PREREQUISITES:*
%
% * <docid:opc_examples.example-ex84291595>

% Copyright 2004-2014 The MathWorks, Inc. 
% $Revision: 1.1.6.10 $  $Date: 2013/01/25 18:44:25 $

%% Create the OPC Toolbox Object Hierarchy
% Create a hierarchy of OPC Toolbox(TM) objects.
da = opcda('localhost','Matrikon.OPC.Simulation.1');
connect(da);
grp = addgroup(da,'CallbackTest');
additem(grp,'Random.Real8');
additem(grp,'Random.UInt2');
additem(grp,'Random.Real4');

%% Configure the Logging Duration
% Set the group's |UpdateRate| value to |0.2| seconds, and the
% |RecordsToAcquire| property to |40|. 
grp.UpdateRate = 0.2;
grp.RecordsToAcquire = 40;

%% Configure the Logging Destination
% Configure the group to log data to disk and memory. Use a file in a
% temporary folder.
logFileName = fullfile(tempdir,'LoggingExample.olf');
grp.LoggingMode = 'disk&memory';
grp.LogFileName = logFileName;
grp.LogToDiskMode = 'overwrite';

%%
% The disk file name is |LoggingExample.olf|. If the file name exists, the toolbox engine
% overwrites the file.

%% Start the Logging Task
% Start the logging task on the group object. Wait two seconds and show the last acquired
% value.
start(grp)
pause(2)
sPeek = peekdata(grp,1)

%%
% Display the item ID and values
disp({sPeek.Items.ItemID;sPeek.Items.Value});

%%
% Wait for the object to complete logging before continuing with the
% example.
wait(grp)

%% Retrieve the Data
% Retrieve the first 20 acquired records into a structure. 
sFirst = getdata(grp,20);

%%
% The |getdata| function removes the records from the OPC Toolbox engine.
% Examine the available records using the |RecordsAvailable|
% property of the group.
recAvail = grp.RecordsAvailable

%%
% Retrieve the balance of the records into separate arrays, converting all values to
% double-precision floating point numbers.
[exItmId,exVal,exQual,exTStamp,exEvtTime] = getdata(grp, ...
    recAvail,'double');

%%
% Examine the contents of the workspace.
whos ex*

%% 
% Retrieve data from disk for a specific item, using the 'itemids' filter.
sReal8Disk = opcread(logFileName,'itemids','Random.Real8')

%%
% Examine the second record.
sReal8Disk(2).Items

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

%%
% Deleting the client object also deletes the group and item objects.