RPC methods

Method calls are a fundamental part of the OPC UA specification. With the introduction of these functionalities into the PLC world, TwinCAT 3 offers the possibility of efficiently executing RPC calls in the IEC61131 world and thus reduces the classic handshake patterns for communication between devices.

With the concept of RPC methods, the TwinCAT OPC UA Server imports a method from the TwinCAT PLC/C++ directly as an OPC UA method.

RPC methods 1:

Requirements

This functionality is only available for Data Access devices based on TwinCAT 3 and the import of TMC and TMI symbol files, as well as the online symbolism.

An RPC method call is handled at OPC UA level as follows:

RPC methods 2:

Processing context

The TwinCAT PLC/C++ method is executed within the real-time context and therefore falls within the processing context of a real-time task. The TwinCAT developer must therefore take precautions so that the execution time of the method matches the task cycle time.

Methods in TwinCAT 3 PLC

Methods in the IEC61131 world are always configured below a function block. At high-level language level, the function block can be regarded as the surrounding class of the method. You have to declare the method itself with a special PLC attribute so that the TwinCAT system knows that the method is to be activated for a remote method call.

{attribute 'TcRpcEnable':='1'}
METHOD M_Sum : INT
VAR_INPUT
  a : INT;
  b : INT;
END_VAR

Depending on the import mode used, you also have to enable the surrounding function block for the OPC UA access. This can be done by using the normal PLC attributes for OPC UA access.

Note that you only need to use the PLC attributes if the filtered mode is used to enable symbols from the PLC to be accessed via OPC UA. This is the default setting of the OPC UA Server.

Sample:

The method M_Sum is located in the function block FB_Mathematics. The declaration of the function block instance uses the PLC attribute that has enabled the function block and thus the method for OPC UA access.

PROGRAM MAIN
VAR
  {attribute 'OPC.UA.DA':='1'}
  fbMathematics : FB_Mathematics;
END_VAR

RPC methods 3:

RPC methods 4:

Pointer variables as VAR_IN_OUT

Pointer variables defined as VAR_IN_OUT are not handled by the PLC or the TwinCAT OPC UA Server. A corresponding Trace event is written to the server trace.

08:47:37.677Z|1|11A0* Error when importing method 'METH_PArray': VAR_IN_OUT pointer variables are not allowed!

Methods in TwinCAT 3 C++

TwinCAT modules could implement interfaces with predefined methods (see TcCOM modules). The method itself must be enabled for RPC calls during its definition (see TwinCAT Module Class Wizard documentation) so that the OPC UA Server knows that it is ready for execution.

RPC methods 5:

So that the return value of the method is available, the corresponding option Include Return Value must be activated. Note that the interface under which the method was created must be implemented.

RPC methods 6:

Depending on the import mode used, you have to declare the method for the access via OPC UA. This can be done by using the TMC Code editor and the usual OPC UA attributes as optional properties.

RPC methods 7:
RPC methods 8: