MATLAB Examples

Read and Write Current OPC UA Server Data

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

PREREQUISITES:

docid:opc_examples.example-ex55000998

Contents

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',51210);
connect(uaClient)

Find the Double, Float, and Int16 nodes in the Static.Scalar namespace.

staticNode = findNodeByName(uaClient.Namespace,'Static','-once');
scalarNode = findNodeByName(staticNode,'Scalar','-once');
doubleNode = findNodeByName(scalarNode,'DoubleValue');
floatNode = findNodeByName(scalarNode,'FloatValue');
int16Node = findNodeByName(scalarNode,'Int16Value');
nodes = [doubleNode,floatNode,int16Node];
disp(nodes)
1x3 OPC UA Node array:
    index     Name      NsInd  Identifier  NodeType  Children
    -----  -----------  -----  ----------  --------  --------
      1    DoubleValue  2      10226       Variable  0
      2    FloatValue   2      10225       Variable  0
      3    Int16Value   2      10219       Variable  0

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

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.

disp(v)
disp(cellfun(@class,v,'UniformOutput',false))
    [ 2.2933e-35]
    [-9.0353e-26]
    [     -29737]

    '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.

disp(t)
   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11

The quality is returned as an OPC UA Quality, which displays as a text description.

disp(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.

disp(isGood(q))
   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 =

   0
   0
   0


raw =

   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 = 

    [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, 12)
newDbl = readValue(uaClient, doubleNode)
dblValue =

    12


newDbl =

    12

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 = 

    [12]
    [66]
    [-4]


ts = 

   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11
   12-Apr-2016 16:48:11


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