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

    %% Read and Write to an OPC Data Access Server from Simulink
% This example shows you how to exchange data between Simulink and OPC Data Access servers.
%
% *PREREQUISITES:*
%
% * <docid:opc_examples.example-ex84291595>

% Copyright 2005-2012 The MathWorks, Inc.
% $Revision: 1.1.6.3 $ $Date: 2012/08/01 00:52:42 $

%% Model Description
% In the following model, all OPC Toolbox(TM) blocks are highlighted in blue.

da = opcda('localhost','Matrikon.OPC.Simulation.1');
connect(da);
grp = addgroup(da,'FlushBuckets');
additem(grp,'Bucket Brigade.Real8');
write(grp,0);
for k=1:100,
    read(grp,'device');
end
disconnect(da);
delete(da);
clear da grp itm
open_system('OPCBlksTutorial');
%%
% The |OPC Config| block defines the servers to use in the model, the
% pseudo-realtime behaviour of the model when it is simulated, and the actions to take when
% OPC-specific events occur (such as a pseudo-realtime violation, server shutdown, etc.)
%
% The signal from the |Sine Wave| block is written to the OPC Server using
% the |OPC Write| block. The same signal is read back from the server using
% the |OPC Read| block, and displayed in the |Scope| together with the
% original Sine Wave signal. The OPC data quality is shown in the |Display|
% block.

%% Understanding the Simulation Results
open_system('OPCBlksTutorial/Scope')
sim('OPCBlksTutorial');

%%
% The |Scope| shows that the Sine Wave signal is delayed by only one sample.
% The |OPC Write| block's priority is set higher than the |OPC Read| block
% to ensure that the read operation occurs after the write operation. This
% ensures only one sample delay between the write and read operation.

close_system('OPCBlksTutorial');