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

    %% Browse OPC UA Server Namespace
% This example shows you how to find OPC Unified Automation (UA) servers, connect to them, and browse their namespace to
% find nodes of interest.
%
% OPC UA servers structure available data through one or more namespaces, consisting of multiple connected Nodes. OPC
% Toolbox exposes two types of OPC UA Nodes: Object nodes, which help to organise data, and Variable nodes which store
% data in their Value property. Variables nodes may contain other Variable nodes as children. Each namespace has an
% Index uniquely identifying that namespace.
%
% All OPC servers must publish a Server node, containing information about the OPC UA server including capabilities of
% that server, available functionality of the server and other diagnostic information. The Server node must exist as
% namespace index 0, named 'Server'. This example will explore the ServerCapabilities node contained in the Server node
% of an example OPC UA server.
%
% *PREREQUISITES:*
%
% * <docid:opc_examples.example-ex55000998>

% Copyright 2015 The MathWorks, Inc.

%% Explore Available OPC UA Servers on a Host
% All OPC UA servers register with a Local DIscovery Service on their host. The Local Discovery Service (LDS) publishes
% all available servers, as well as their unique "address" (or URL) for connecting to that server.
%
% You can discover OPC UA servers available on a host using |opcuaserverinfo|. This example uses the local host.
serverList = opcuaserverinfo('localhost')

%%
% The list of servers shows 3 available OPC UA servers, and the port number on which you can connect to the server. You
% can find a specific server by searching the Description of the servers. Find the server containing the word "Sample".
sampleServerInfo = findDescription(serverList, 'Sample')

%% Construct an OPC UA Client and Connect to the Server
% In order to browse the server namespace, you need to construct an OPC UA Client and connect that client to the server.
% You can construct the client directly from the |opcuaserverinfo| results.
uaClient = opcua(sampleServerInfo);

%%
% Initially the client is disconnected from the server, and shows a brief summary of the client properties. 
disp(uaClient)

%%
% You know that the client is disconnected by querying the Status property, or calling the |isConnected| function.
disp(uaClient.Status)

isConnected(uaClient)

%%
% Once you connect the client to the server, additional properties from the server are displayed.
connect(uaClient)
disp(uaClient)

%%
% The display shows that the client Status is now 'Connected', the server is in the 'Running' state, and the client
% stores information regarding server limits. In this case, all limits are set to zero, indicating that there is no
% server-wide limit for sample rates, maximum nodes or values for read operations on the Sample Server.

%% Browsing the Server Namespace
% The server namespace is incrementally retrieved directly into the OPC UA Client variable in MATLAB. You access the top
% level of the server namespace using the |Namespace| property. This property stores OPC UA Nodes. Each node can contain
% one or more Children, which are themselves nodes.
topNodes = uaClient.Namespace

%%
% The node named |'Server'| contains 10 children.
%
% You can search the namespace using indexing into the Children property of available nodes. For example, to find the ServerCapabilities node, you can query the CHildren  of the Server node.
serverChildren = topNodes(1).Children

%%
% The ServerCapabilities node is the sixth node in the list.
serverCapabilities = serverChildren(6)

%% Searching for Nodes in the Namespace
% You can search for nodes from a Node variable, or from the Namespace property directly. To find the
% 'ServerCapabilities' node without indexing into the |Namespace| property, use |findNodeByName|. To avoid the search
% finding all instances of nodes containing the word 'ServerCapabilities' you use the |'-once'| parameter.
serverCapabilities = findNodeByName(topNodes, 'ServerCapabilities', '-once')

%%
% To find all nodes containing the word 'Double' in the Name, query all topNodes using the |'-partial'| parameter. Note
% that this search will load the entire namespace into MATLAB, so use this search method with caution.
doubleNodes = findNodeByName(topNodes, 'Double', '-partial')

%% Understanding the NodeType
% Nodes have a NodeType which describes whether that node is simply an organisational unit (an Object NodeType) or
% contains data (a Variable NodeType). Variable NodeTypes may contain Children - A NodeType of Variable does not
% guarantee that the node contains no Children. In this example, |doubleNodes| contains one Object node, and 12 Variable
% nodes.
allNodeTypes = {doubleNodes.NodeType}

%% Understanding Variable NodeType Properties
% A Variable node has additional properties describing the data stored in the Variable node. To view these properties,
% display a Variable node.
doubleNodes(2)

%%
% This node has a |ServerDataType| of 'Double', and allows reading and writing of the Current value
% (|AccessLevelCurrent| property) but supports no historical data reading (|AccessLevelHistory|). The server is not 
% Historizing this node, as evidenced by the |Historizing| property.
%
% Some properties, such as |ServerValueRank|, and |ServerArrayDimensions| are not shown in the display of a node, but
% can be queried through the respective property. See help on these properties for further information.

%% Constructing Nodes Directly
% Nodes are defined uniquely by their NamespaceIndex and their Identifier. You can construct a known node without
% browsing the |Namespace| property using the |opcuanode| function. For example, to construct the ServerCapabilities
% node directly you can use the NamespaceIndex 0 and Identifier 2268 (all OPC UA servers must publish a
% ServerCapabilities node with this NamespaceIndex and Identifier). 
capabilitiesNode = opcuanode(0, 2268, uaClient)

%%
% Note that nodes constructed using |opcuanode| have no Parent property.
capabilitiesNode.Parent

%%
% However their Children are automatically retrieved if the node is associated with a connected OPC UA Client.
capabilitiesNode.Children

%% Disconnect from Server
% When you have finished communicating with the server, you should disconnect the client from the server. This is also
% automatically performed when the client variable goes out of scope in MATLAB.
disconnect(uaClient);