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

    %% Locate and Browse OPC Historical Data Access Servers
% This example shows how to use the OPC Toolbox(TM) to browse the
% network for OPC Historical Data Access servers, and use OPC Toolbox functions to query the
% server name space for server items and their properties.
%
% *PREREQUISITES:*
%
% * <docid:opc_examples.example-ex84291595>

% Copyright 2004-2014 The MathWorks, Inc. 
% $Revision: 1.1.6.3 $  $Date: 2013/03/26 00:40:56 $

%% Step 1: Browse the Network for OPC HDA Servers
% You use the |opchdaserverinfo| function to query a host on the network for
% available OPC Historical Data Access servers. This example uses the local host.
hostInfo = opchdaserverinfo('localhost')

%%
% Find the server info entry with a description starting with Matrikon.
hIndex = findDescription(hostInfo,'Matrikon')
hostInfo(hIndex)

%% Step 2: Construct a Client Object and Connect to the Server
% Use the |ServerInfo| object returned in the previous step to construct a client object.
hdaObj = opchda(hostInfo(hIndex));

%%
% You can also specify the host name and server ID directly.
hdaObj = opchda('localhost','Matrikon.OPC.Simulation.1')

%% 
% Connect the client to the server.
connect(hdaObj);

%% Step 3: Retrieve the Server Name Space
% Retrieve the name space of the server.
ns = getNameSpace(hdaObj)

%%
% Each element of the structure is a node in the server name space.
ns(1)

%% Step 4: Find Items in the Name Space
% Use the |serveritems| function to find all items in the name space containing the string |Real|. 
realItems = serveritems(ns,'*Real*')

%% Step 5: Query Server Item Attributes
% Examine the current normal maximum value of the tenth item found.
maxVal = readItemAttributes(hdaObj,realItems{10},hdaObj.ItemAttributes.NORMAL_MAXIMUM,now,now)

%%
% The warning indicates that the item has not yet been stored in the historian database, but the
% preconfigured item attributes are being returned.

%% Step 6: Clean Up OPC Toolbox Objects
% Disconnect the client from the server and remove OPC Toolbox objects from memory when you no
% longer need them. Deleting the client object also deletes the group and item objects.
disconnect(hdaObj)
delete(hdaObj)