Main Content

Read and Write Current OPC UA Server Data

R2026b

This example shows you how to read and write current values on an OPC UA server.

This workflow is useful for supervisory control applications. You can read live process variables from a PLC or DCS and write updated setpoints back to the device. For information on how to retrieve values at specific times in the past, see Read Historical OPC UA Server Data.

To run this example, you must install and start the Prosys OPC UA Simulation Server. For more information, see Install an OPC UA Simulation Server for OPC UA Examples.

Create a Client and Connect to the Server

To communicate with an OPC UA server, create a client using opcua and connect to it. You can create client objects using the results of a query to the Local Discovery Service using opcuaserverinfo, or directly using the host name and port number. In this case, use the host and port number syntax. Port 53530 is the default port for the Prosys OPC UA Simulation Server.

uaClient = opcua("localhost",53530);
connect(uaClient)

Find Nodes to Read and Write

Use findNodeByName to locate nodes by name in the server namespace. To interactively browse the server address space, use browseNamespace.

staticNode = findNodeByName(uaClient.Namespace,"StaticData","-once");
dataItemsNode = findNodeByName(staticNode,"DataItems","-once");
doubleNode = findNodeByName(dataItemsNode,"DoubleDataItem");
floatNode = findNodeByName(dataItemsNode,"FloatDataItem");
int16Node = findNodeByName(dataItemsNode,"Int16DataItem");
nodes = [doubleNode,floatNode,int16Node]
nodes = 

1x3 OPC UA Node array:
    index       Name       NsInd    Identifier    NodeType  Children
    -----  --------------  -----  --------------  --------  --------
      1    DoubleDataItem  3      DoubleDataItem  Variable  1
      2    FloatDataItem   3      FloatDataItem   Variable  1
      3    Int16DataItem   3      Int16DataItem   Variable  1

Read Values from Nodes

Use readValue to read the current value of a node. You can query the Value, the Timestamp when the value was updated, and the Quality associated with the value when written.

[v,t,q] = readValue(uaClient,nodes)
v =

  3×1 cell array

    {[0]}
    {[0]}
    {[0]}


t = 

  3×1 datetime array

   19-Mar-2019 02:52:35
   19-Mar-2019 02:52:35
   19-Mar-2019 02:52:35


q = 

OPC UA Quality ID:
	'Good'
	'Good'
	'Good'

When you read from multiple nodes, the values are returned as a cell array. The class of the data on the server is preserved as much as possible.

valClasses = cellfun(@class,v,UniformOutput=false)
valClasses =

  3×1 cell array

    {'double'}
    {'single'}
    {'int16' }

The timestamp is returned as a MATLAB® datetime variable. It represents the time when the source provided the value to the server.

t
t = 

  3×1 datetime array

   19-Mar-2019 02:52:35
   19-Mar-2019 02:52:35
   19-Mar-2019 02:52:35

The quality is returned as an OPC UA Quality, which displays as a text description. Check quality before using the value to ensure it represents valid data.

q
q = 

OPC UA Quality ID:
	'Good'
	'Good'
	'Good'

You can interrogate the Quality to determine characteristics of the returned quality. In this example, the quality is good.

isGood(q)
ans =

  3×1 logical array

   1
   1
   1

The value is not interpolated, but is a raw value (stored by the server directly from the sensor).

interpolated = isInterpolated(q)

raw = isRaw(q)
interpolated =

  3×1 logical array

   0
   0
   0


raw =

  3×1 logical array

   1
   1
   1

Write Data to Nodes

Use writeValue to write data to any scalar node. When you write to multiple nodes, you must pass a cell array of values, one for each node to be written.

newValues = {12,65,-4};
writeValue(uaClient,nodes,newValues);

Verify that the values were written correctly by reading them again.

serverValues = readValue(uaClient,nodes)
serverValues =

  3×1 cell array

    {[12]}
    {[65]}
    {[-4]}

You can update values directly within the cell array and write them back to the server.

serverValues{2} = serverValues{2} + 1;
writeValue(uaClient,nodes,serverValues);

Read and Write Values with a Single Node

When working with a single node, you can receive and pass the value directly, without using a cell array.

dblValue = readValue(uaClient,doubleNode)
writeValue(uaClient, doubleNode, dblValue+15.6)
newDbl = readValue(uaClient, doubleNode)
dblValue =

    12


newDbl =

   27.6000

Reading and Writing to Nodes Directly

You can read and write directly from the node variable, as long as that node was created from the client (using the Namespace property or browseNamespace) or you passed a client to the opcuanode function when creating your node variable.

[vals,ts,qual] = readValue(nodes)
writeValue(nodes,v)
vals =

  3×1 cell array

    {[27.6000]}
    {[     66]}
    {[     -4]}


ts = 

  3×1 datetime array

   19-Mar-2019 02:52:36
   19-Mar-2019 02:52:36
   19-Mar-2019 02:52:36


qual = 

OPC UA Quality ID:
	'Good'
	'Good'
	'Good'

Disconnect from Server

When you have finished communicating with the server, disconnect the client from the server. This is also automatically performed when the client variable goes out of scope in MATLAB.

disconnect(uaClient);

See Also

Functions

Topics