Main Content

浏览OPC UA服务器名称空间

This example shows you how to find OPC Unified Architecture (UA) servers, connect to them, and browse their namespace to find nodes of interest.

To run this example in your MATLAB® session, you must install and start the Prosys OPC UA Simulation Server. For further information, see the Getting Started section of the Industrial Communication Toolbox™ documentation.

OPC UA服务器通过一个或多个名称空间构建可用数据,由多个连接的节点组成。每个名称空间都有一个唯一识别该名称空间的索引。该工具箱公开了两种类型的OPC UA节点:对象节点,它们有助于组织数据,以及将数据存储在其值属性中的可变节点。变量节点可能包含其他变量节点作为儿童。

All OPC UA 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.

Explore Available OPC UA Servers on a Host

NOTE: This section of this example requires you to install the Local Discovery Service, and configure the Prosys OPC UA Simulation Server to register with the LDS. Instructions for how to do this are included in the Getting Started section of the Industrial Communication Toolbox documentation.

OPC UA servers may 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 usingopcuaserverinfo。此示例使用本地主机。

serverList = opcuaserverinfo('localhost'
ServerList = 1×3 OPC UA ServerInfo数组:索引说明主机名端口---------------------------------------------------------------------------------------------------------------------------------------------------------------- 1仿真器TMOPTI01WIN1064.DHCP.MATHWORKS。com 53530 2 UA Sample Server tmopti01win1064 51210 3 Quickstart Historical Access Server tmopti01win1064 62550

服务器列表显示可用的OPC UA服务器以及您可以连接到服务器的主机名和端口号。您可以通过搜索服务器的描述来找到特定的服务器。找到包含“仿真”一词的服务器。

SampleSerVerinfo = FindDescription(ServerList,,,'Simulation'
SampleSerVerinfo = opc ua serverinfo'simulateserver':连接信息主机名:'tmopti01win1064.dhcp.mathworks.com'端口:53530

Construct an OPC UA Client and Connect to the Server

为了浏览服务器名称空间,您需要构建一个OPC UA客户端并将该客户端连接到服务器。如果您知道OPC UA服务器的主机名和端口,则可以使用主机名和端口参数构造OPC UA客户端。

uaClient = opcua('localhost',,,,53530);

If you have previously discovered the server using theopcuaserverinfo命令,您可以直接从opcuaserverinforesults.

uaClient = opcua(sampleServerInfo)
uAclient = opc ua客户端仿真器:主机名:tmopti01win1064.dhcp.mathworks.com端口:53530超时:10状态:断开连接

Initially the client is disconnected from the server, and shows a brief summary of the client properties. You know that the client is disconnected by querying the Status property, or calling theisConnectedfunction.

status = uaclient.status issonnected(uaclient)
status = 'Disconnected' ans = logical 0

Once you connect the client to the server, additional properties from the server are displayed.

connect(uaClient) uaClient
uaClient = OPC UA客户SimulationServer: Hostname: tmopti01win1064.dhcp.mathworks.com Port: 53530 Timeout: 10 Status: Connected ServerState: Running MinSampleRate: 0 sec MaxHistoryReadNodes: 0 MaxHistoryValuesPerNode: 0 MaxReadNodes: 0 MaxWriteNodes: 0

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 theNamespace财产。This property stores OPC UA Nodes. Each node can contain one or more Children, which are themselves nodes.

topNodes = uaClient.Namespace
topNodes = 1x6 OPC UA Node array: index Name NsInd Identifier NodeType Children ----- ----------------------- ----- -------------------------------------------------- 1服务器0 2253对象12 2 MyObject 2 MyObjectsFolder对象1 3 StaticData3 staticdata对象9 4 nonuanodecompliancetest 4 nonuanodecompliancetest toctexest 33 5仿真5 85/0:仿真对象7 6 mybignodemanager 6 mybignodemanager对象1000

命名的节点'Server'包含12个孩子。

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
serverChildren = 1x12 OPC UA Node array: index Name NsInd Identifier NodeType Children ----- ------------------- ----- ---------- -------- -------- 1 ServerStatus 0 2256 Variable 6 2 ServerCapabilities 0 2268 Object 14 3 ServerDiagnostics 0 2274 Object 4 4 VendorServerInfo 0 2295 Object 0 5 ServerRedundancy 0 2296 Object 5 6 Namespaces 0 11715 Object 1 7 ServerConfiguration 0 12637 Object 5 8 NamespaceArray 0 2255 Variable 0 9 Auditing 0 2994 Variable 0 10 ServerArray 0 2254 Variable 0 11 EstimatedReturnTime 0 12885 Variable 0 12 ServiceLevel 0 2267 Variable 0

The ServerCapabilities node is the second node in the list.

ServerCapabilities = ServerChildren(2)
serverCapabilities = OPC UA Node object: Name: ServerCapabilities Description: Describes capabilities supported by the server. NamespaceIndex: 0 Identifier: 2268 NodeType: Object Parent: Server Children: 14 nodes.

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 theNamespace属性,使用findNodeByName。为了避免搜索查找所有包含“服务器范围”一词的节点的实例'-一次'parameter.

serverCapabilities = findNodeByName(topNodes,“ ServerCapabilities',,,,'-一次'
serverCapabilities = OPC UA Node object: Name: ServerCapabilities Description: Describes capabilities supported by the server. NamespaceIndex: 0 Identifier: 2268 NodeType: Object Parent: Server Children: 14 nodes.

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.

doubleenodes = findnodebyname(topnodes,'双倍的',,,,'-partial'
doubleNodes = 1x6 OPC UA Node array: index Name NsInd Identifier NodeType Children ----- --------------------- ----- --------------------- -------- -------- 1 Double 4 Double Variable 0 2 DoubleAnalogItemArray 3 DoubleAnalogItemArray Variable 3 3 DoubleAnalogItem 3 DoubleAnalogItem Variable 3 4 DoubleDataItem 3 DoubleDataItem Variable 1 5 DoubleArray 3 DoubleArray Variable 0 6 Double 3 Double Variable 0

Understanding the NodeType

Nodes have a NodeType which describes whether that node is simply an organisational unit (an Object NodeType) or contains data that can be read or written (a Variable NodeType). An example of an Object node is tha ServerCapabilities node shown above. You cannot read data from an Object node. In this example,doubleNodescontains no Object nodes, and 6 Variable nodes.

allNodeTypes = {doubleNodes.NodeType}
AllNodEtypes = 1×6个单元阵列列1至4 {'variable'} {'variable'} {'variable'} {'variable'}列5至6 {'variable'} {'variable'}

Variable NodeTypes may contain Children - A NodeType of Variable does not guarantee that the node contains no Children. The second node listed is a variable node (and so its Value can can be read) but also has children (which can be read individually). For information on reading values from a node, seereadvalue

Understanding Variable NodeType Properties

变量节点具有描述可变节点中存储的数据的其他属性,包括该节点的服务器数据类型和访问权限。要查看这些属性,请显示一个变量节点。

doubleNodes(2)
ans = OPC UA Node object: Name: DoubleAnalogItemArray Description: NamespaceIndex: 3 Identifier: DoubleAnalogItemArray NodeType: Variable Parent: AnalogItemArrays Children: 3 nodes. ServerDataType: Double AccessLevelCurrent: read/write AccessLevelHistory: none Historizing: 0

这个节点有一个ServerDataTypeof 'Double', and allows reading and writing of the Current value (AccessLevelCurrent属性),但没有支持历史数据阅万博1manbetx读(cosceplevelvelistory)。The server is not Historizing this node, as evidenced by the历史化财产。

一些属性,例如ServerValuerank,,,,andServerArrayDimensionsare not shown in the display of a node, but can be queried through the respective property. See help on these properties for further information.

doubleNodes(2).ServerArrayDimensions
ans = uint32 0

直接构建节点

节点是由其命名点及其标识符唯一定义的。您可以构造一个已知节点,而无需浏览Namespaceproperty using theopcuanodefunction. 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)
capabilitiesNode = OPC UA Node object: Name: ServerCapabilities Description: Describes capabilities supported by the server. NamespaceIndex: 0 Identifier: 2268 NodeType: Object Children: 14 nodes.

Note that nodes constructed usingopcuanodehave no Parent property.

capabilitiesNode.Parent
ANS =空的OPC UA节点对象。

但是,如果节点与连接的OPC UA客户端关联,则将自动检索他们的孩子。

capabilitiesNode.Children
ans = 1x14 OPC UA Node array: index Name NsInd Identifier NodeType Children ----- ---------------------------- ------------------------------------ 1个模块0 2996对象6 2聚合功能0 2997对象14 3历史记录ServerCapabilities 0 11192对象15 4 Operation Limits 0 11704Object 12 5 LocaleIdArray 0 2271 Variable 0 6 Min万博1manbetxSupportedSampleRate 0 2272 Variable 0 7 MaxQueryContinuationPoints 0 2736 Variable 0 8 MaxByteStringLength 0 12911 Variable 0 9 ServerProfileArray 0 2269 Variable 0 10 MaxHistoryContinuationPoints 0 2737 Variable 0 11 SoftwareCertificates 0 3704 Variable 0 12 MaxStringLength 0 11703 Variable 013 maxBrowseContinuationpoints 0 2735变量0 14 maxArrayLength 0 11702变量0

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.

断开连接(uAclient);