主要内容

从OPC UA服务器访问数据

OPC UA编程概述

本主题显示了通过从本地计算机上运行的模拟服务器检索当前和历史数据来创建OPC统一体系结构(UA)应用程序的基本步骤。

Note

要在以下步骤中运行示例代码,您需要在本地计算机上运行的Prosys OPC UA仿真服务器。您还可以选择安装本地发现服务,并在LDS上注册Prosys Server。有关安装详细信息,请参阅Install an OPC UA Simulation Server for OPC UA Examples。这code requires only minor changes to work with other servers.

Step 1: Locate Your OPC UA Server

In this step, you obtain information that the toolbox needs to uniquely identify the OPC UA server that you want to connect to. You use this information when creating an OPC UA client object, described in Step 2: Create an OPC UA Client Object.

第一个信息是服务器计算机的主机名。主机名(诸如“历史学家”或192.168.16.32之类的IP地址的描述性名称符合网络上的计算机,并且由OPC协议使用来确定该计算机上的可用OPC服务器。在任何OPC Toolbox™应用程序中,您必须知道OPC服务器主机的名称,以便可以建立与该主机的连接。您的网络管理员可以提供主机名列表,这些列表在您的网络上提供OPC服务器。在此示例中,您将使用“ Local主持”as the host name, because you will connect to the OPC server on the same machine as the client.

OPC UA servers are uniquely identified by Universal Resource Locations. Similar to web addresses, a URL for an OPC UA server starts withopc.tcp://, and then provides an address of the server as a hostname, port, and address in standard ASCII text. For example, the URL for the Prosys OPC UA Simulation Server isopc.tcp:// localhost:53530/opcua/simulateserver

OPC UA服务器URL通过OPC UA本地发现服务(LDS)宣传,该服务可在每个OPC UA服务器主机计算机上使用。您的系统管理员可以为特定主机提供服务器URL的列表,也可以查询主机的所有可用OPC UA服务器。

If you have installed the LDS and registered the OPC UA server with the LDS, you can use theopcuaserverinfofunction to query hosts from the command line. If you have not installed the LDS, skip to Step 2.

serverlist = opcuaserverinfo(“ Local主持”)
serverList = 1x2 OPC UA ServerInfo array: index Description Hostname Port ----- ----------------------------- ------------ ----- 1 SimulationServer myhost.local 53530 2 Quickstart Data Access Server myhost.local 62547

通过使用FindDescription功能以搜索服务器描述中的特定字符向量。

hsInfo = findDescription(serverList,'模拟')
hsinfo = opc ua serverInfo'simulationserver':连接信息主机名:'myhost.local'端口:53530

From this discovery process, you can identify the port (53530) on which the OPC UA server listens for connections. The discovery process also makes it easier to construct and connect to the required OPC UA server.

步骤2:创建一个OPC UA客户端并连接到服务器

找到OPC UA服务器后,您可以创建一个OPC UA客户端来管理与服务器的连接,获取密钥服务器特性,并从服务器读取和写入数据。您可以使用opcuaserverinfo结果直接构建OPC UA客户端。

uaClient = opcua (hsInfo)

或者,您可以直接使用主机名创建客户端。

uAclient = opcua(“ Local主持”,53530)
uaClient = OPC UA Client: Server Information: Name: 'SimulationServer@localhost' Hostname: 'localhost' Port: 53530 EndpointUrl: 'opc.tcp://localhost:53530/OPCUA/SimulationServer' Connection Information: Timeout: 10 Status: 'Disconnected' ServerState: '' Security Information: MessageSecurityMode: SignAndEncrypt ChannelSecurityPolicy: Aes256_Sha256_RsaPss Endpoints: [1×11 opc.ua.EndpointDescription]

这client is initially disconnected from the server, as shown by theStatusproperty. After you connect to the server, additional properties are shown in the client display.

连接(uAclient)
UACLIENT OPC UA客户端:服务器信息:名称:'simulationServer@localhost'hostName:'localhost'端口:53530 endpointUrl:'opc.tcp:// localhost:53530/opcua/simulationserver/simulationserver'连接:超时:超时:10个状态:10个状态:'连接'连接'连接'连接'' ServerState: 'Running' Security Information: MessageSecurityMode: SignAndEncrypt ChannelSecurityPolicy: Aes256_Sha256_RsaPss Endpoints: [1×11 opc.ua.EndpointDescription] Server Limits: MinSampleRate: 0 sec MaxReadNodes: 0 MaxWriteNodes: 0 MaxHistoryReadNodes: 0 MaxHistoryValuesPerNode: 0

其他属性描述了服务器的功能,特别是限制了各种读写操作。限值为0表示服务器不会对该功能施加直接限制。

步骤3:浏览OPC UA服务器名称空间

OPC UA服务器提供一个单个名称空间,供您读取和编写当前数据和历史数据。名称空间被组织为节点的层次结构。每个节点都有描述该节点的属性。一个节点由两个元素唯一识别:名称空间索引(数字整数)和节点标识符(数字整数,字符向量或全球唯一标识符或GUID)。要唯一描述一个节点,您必须同时提供命名点和标识符;您不能仅提供标识符,因为可能会重复不同的名称空间索引。

OPC工具箱通过OPC UA客户端的名称空间属性公开节点的层次结构。名称空间属性的每个元素都是服务器最高级别的节点。名称空间中的每个节点都有一个孩子们暴露该节点中包含的子节点的属性。您可以使用图形方式浏览名称空间Browsenamespace功能。结果对话框允许您从层次结构中选择节点,并从函数中返回输出中。

serverNodes = browseNamespace(uaClient)

使用选定的项目命名空格浏览器

当您单击时好的the selected items are returned in the command window output.

serverNodes = 1x2 OPC UA Node array: index Name NsInd Identifier NodeType Children ----- ---------------------- ----- ----------- ------------------- 1分金宝app钟缩略为0 2272变量0 2 maxArrayLength 0 11702变量0

节点可以具有与它们关联的数据值,也可以仅仅是其他节点的容器。这结构型节点的属性将节点标识为对象节点(容器)或变量节点(数据)。有关如何编程搜索服务器名称空间的更多信息,请参阅Browse OPC UA Server Namespace

Step 4: Read Current Values from the OPC UA Server

OPC UA服务器提供对其当前和历史价值的访问权限多变的节点。使用OPC工具箱,您使用节点数组来读取服务器的当前值。当前数据包括该值,服务器从传感器接收数据值的时间戳以及描述数据值的准确性和来源的质量。

[Val,TS,Qual] = ReadValue(uAclient,servernodes)
val = 2×1单元格数组{[0 sec]} {[65535]} ts = 2×1 DateTime Array 10-APR-2019 09:46:43 10-APR-2019 09:46:43 Qual Qual = OPC UA质量id:'好''好'

有关阅读和编写当前价值的更多信息,请参见Read and Write Current OPC UA Server Data

步骤5:从OPC UA服务器读取历史数据

历史数据存储在OPC UA服务器上的选定节点。在上一步中检索到的服务器节点不会由服务器存档,因为值通常不会更改。您可以查询Historizing节点的属性以确定服务器当前是该节点的当前归档数据。

Because the serverNode list is an array, you must collect the outputs using concatenation.

[serverNodes.Historizing]
ans = 0 0

None of the server nodes are currently being historized. In addition, the server does not allow historical access to these nodes, as evidenced by theAccessLevelHistory节点的属性。

{servernodes.accesslevelhistory}
ans ='无''none'

To locate nodes with history, query the server for the Double and Int32 nodes in the模拟父节点。

simnode = findnodebyname(uaclient.namespace,'模拟')
simNode = OPC UA Node: Node Information: Name: 'Simulation' Description: 'The type for objects that organize other nodes.' NamespaceIndex: 5 Identifier: '85/0:Simulation' NodeType: 'Object' Hierarchy Information: Parent: Server Children: 14

模拟节点是一个目的node, so it has no价值。但是,它有7个孩子。找到Sinusoid随机的child nodes. The'-部分的'flag finds nodes beginning with the argument provided.

sinenode = findnodebyname(simnode,'Sinusoid','-部分的');randnode = findnodebyname(simnode,'随机的','-部分的')
randNode = OPC UA Node: Node Information: Name: 'Random1' Description: '' NamespaceIndex: 5 Identifier: 'Random1' NodeType: 'Variable' Hierarchy Information: Parent: 'Simulation' Children: 0 ServerDataType: Double AccessLevelCurrent: read/write AccessLevelHistory: read Historizing: 0

Although the正弦1随机的1节点目前尚未存档(Historizing是错误的)您可以从节点中读取历史数据(历史记录在启动时已记录,然后关闭)。要读取在指定时间范围内存储在服务器上的所有数据,请使用阅读历史功能,传递节点以读取和读取数据的时间范围。

histData = readHistory(uaClient,[sineNode,randNode],datetime('现在')-seconds(10),datetime('现在'))
histData = 1-by-2 OPC UA Data object array: Timestamp Sinusoid1 Random1 ----------------------- ------------------------------------------------------------- 2019-04-10 09:58:31.000 0.415823 [good(raw)] 0.131428 [good(raw)] 2019-04-10 09:58:32.000 0.813473 [good(raw)] 0.038980 [good(warg)] 2019-04-10 09-04-10 09:33.000:33.000 1.17555700[good(raw)] 0.316324 [good(raw)] 2019-04-10 09:58:34.000 1.486290 [good(raw)] 0.229609 [good(raw)] 2019-04-10 09:58:35.000 1.732051 [好(RAW)] 0.208826 [good(raw)] 2019-04-10 09:58:36.000 1.902113 [good(raw)] 0.483303 [good(raw)] 2019-04-10 09:58:37.000 1.989044)0.393722 [good(raw)] 2019-04-10 09:58:38.000 1.989044 [good(raw)] 0.206232 [good(raw)] 2019-04-10 09:58:39.000 1.9021130.116650 [good(raw)] 2019-04-10 09:58:40.000 1.732051 [good(raw)] 0.391128 [good(raw)]

获取检索数据的摘要。

summary(histData)
1-by-2 opc ua数据对象:名称值启动时间戳末端时间戳质量----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 10双值2019-04-10 09:58:31.000 2019-04-10 09:58:40.000 1独特质量[良好(RAW)]Random1 10双值2019-04-10 09:58:31.000 2019-04-10 09:58:40.000 1独特的质量[GOOD(RAW)]

步骤6:绘制数据

You can plot the data directly from the resultingopc.ua.data目的。

情节(Histdata)传奇显示

您也可以将数据转换为MATLAB®用于进一步处理的天然数据类型。有关处理数据的信息,请参阅Visualize and Preprocess OPC UA Data

Step 7: Clean Up

When you have finished exchanging data with the OPC server, you should disconnect from the server.

断开连接(UACLIENT)

You can then clear the OPC UA variables from MATLAB memory. If you clear an OPC UA client from memory, the connection to the server is automatically closed.