Main Content

Call Method Nodes from OPC UA Client

This example shows you how to call method nodes from an OPC UA client to control various aspects of plant operations in a simple factory scenario.

The system, subsystem, and devices in a plant are represented as object nodes in an OPC UA address space. The various functionalities of these objects are represented as method nodes. You can call these method nodes from a client to invoke specific actions on the corresponding machine or device. These actions can range from simple operations to more complex tasks.

Consider a factory where a digital control system is represented as an object node, and multiple machines are attached to it. The functionalities of these machines are represented using method nodes. To control a specific functionality of a machine, you can call the corresponding method node.

Create OPC UA Client and Connect to Server

Create a connection from MATLAB® to the OPC UA server using opcua function. This example uses the server URL to establish a connection.

clientObj = opcua("opc.tcp://172.18.69.30:4842")
clientObj = 
OPC UA Client:

   Server Information:
                     Name: 'open62541-based OPC UA Application'
                 Hostname: 'lsdbgl29437glnxa64'
                     Port: 4842
              EndpointUrl: 'opc.tcp://lsdbgl29437glnxa64:4842'

   Connection Information:
                  Timeout: 10
                   Status: 'Disconnected'
              ServerState: '<Not connected>'

   Security Information:
      MessageSecurityMode: None
    ChannelSecurityPolicy: None
                Endpoints: [1×1 opc.ua.EndpointDescription]

The clientObj properties indicate that the client is not connected to the server.

Connect the client to the server and verify its connection status using the isConnected function.

connect(clientObj);
connectionStatus = isConnected(clientObj)
connectionStatus = logical
   1

Select Method Nodes

Call the browseNamespace function, and select the method nodes from the Browse Namespace dialog box.

Namespace browser

methodNodes = browseNamespace(clientObj)
methodNodes = 
1×3 OPC UA Node array:
    index       Name       NsInd    Identifier    NodeType
    -----  --------------  -----  --------------  --------
      1    StartMachine    1      62541           Method
      2    MachineProfile  1      MachineProfile  Method
      3    StopMachine     1      62542           Method

The functionalities represented by the method nodes are:

  1. StartMachine - Start machine.

  2. MachineProfile - Select profile of machine. You can use the profile to improve machine performance and for failure diagnosis.

  3. StopMachine - Stop machine.

Note: The nodes in this example are created for the purpose of demonstration.

Start Machine

Execute the StartMachine node using the invoke function and by specifying the client connection object, method node name, and the required input arguments.

To find the input properties of the node, view the node information.

methodNodes(1)
ans = 
OPC UA Node:

   Node Information:
              Name: 'StartMachine'
       Description: ''
    NamespaceIndex: 1
        Identifier: 62541
          NodeType: 'Method'

   Hierarchy Information:
            Parent: 'ControlSystem'
          Children: 2

   Method Information:
        Executable: 1
    UserExecutable: 1
         NumInputs: 1
        InputTypes: {'String'}
        NumOutputs: 1
       OutputTypes: {'String'}

In this example, the StartMachine node requires one input of String data type as specified by the NumInputs and InputTypes properties, respectively, of the method node object. The method node also returns one output of String data type as specified by the NumOutputs and OutputTypes properties, respectively.

[value, time, quality] = invoke(clientObj,methodNodes(1),"Compressor")
value = 
'Started Compressor'
time = datetime
   01-Jan-1601 05:53:28

quality = 
OPC UA Quality ID:
	Good

Select Machine Profile

Select a machine profile to select between different modes in which the machine operates. In this example, the available profiles are:

  1. Diagnostic Profile - Use this profile to troubleshoot machine failures.

  2. Base Profile - The machine works with minimum resources.

  3. High Profile - The machine works at maximum capacity.

For this example, select the base profile.

[value, time, quality] = invoke(clientObj,methodNodes(2),2)
value = 
'Base Profile'
time = datetime
   01-Jan-1601 05:53:28

quality = 
OPC UA Quality ID:
	Good

Stop Machine

Execute the StopMachine node.

[value, time, quality] = invoke(clientObj,methodNodes(3),"Compressor")
value = 
'Stopped Compressor'
time = datetime
   01-Jan-1601 05:53:28

quality = 
OPC UA Quality ID:
	Good

Disconnect Client from Server

When you have finished communicating with the server, disconnect the client from the server. The client is automatically disconnected from the sever when the client variable goes out of scope in MATLAB. Additionally, clearing the client object deletes the client information and removes the variable from the workspace.

disconnect(clientObj)
clear clientObj