Main Content

Read and Write Current OPC UA Server Data

This example shows you how to read and write data to an OPC UA server.

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.

Create a Client and Connect to the Server

You 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 of the server you are connecting to. In this case, use the host and port number syntax.

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

Find the DoubleDataItem, FloatDataItem, and Int16DataItem nodes in the StaticData namespace.

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.

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

You can 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);

To verify that the values were written correctly, and retrieve the value 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 receive and can pass and 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 write and read 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);