Skip to content

Step 8: Connecting an OPC UA Variable with a Physical Process using open62541

Last updated on September 1, 2020


Connect the variables of the application code with the UANodeSet. This step can be skipped if you want to review the custom UANodeSet on an open62541 server without any meaningful data or functionality attached.

This Post is part of the OPC UA Information Model Tutorial.


This post is based on https://open62541.org/doc/current/tutorial_server_datasource.htm and the code snippets shown are part of https://github.com/open62541/open62541/blob/master/examples/tutorial_server_datasource.c

In summary, open62541 offers three approaches for connecting a process variable to an OPC UA variable:

  • manually writing new values using the Server API
  • callbacks that copy values between process variable and OPC UA representation (bidirectional)
    • When a value changes continuously, such as the system time, updating the value in a tight loop would take up a lot of resources. Value callbacks allow to synchronize a variable value with an external representation. They attach callbacks to the variable that are executed before every read and after every write operation.
  • With value callbacks, the value is still stored in the variable node. So-called data sources go one step further. The server redirects every read and write request to a callback function. Upon reading, the callback provides copy of the current value. Internally, the data source needs to implement its own memory management.

open62541 provides a ready-to-go tutorial example, that demonstrates all three approaches side by side in open62541/examples/tutorial_server_datasource.c .

See Setup open62541 on Debian & Build first server for how to build/compile this .c file.

Quick command (it will take a few seconds/minutes to compile the full NS0 nodeset):

cd ~/myServer
cp ~/open62541/examples/tutorial_server_datasource.c ~/myServer/tutorial_server_datasource.c
gcc -std=c99 -I$HOME/install/include -L$HOME/install/lib tutorial_server_datasource.c -lopen62541 -lmbedtls -lmbedx509 -lmbedcrypto -o myServer

Practical Implementation

Independent of the chosen approach, the mapping between process variables and UA variable is done in C. At best, the C code may be generated with a (python) script.

Otherwise you need to manually write the C code which is assigning a data source to a specific variable with a specific node id. To use defines instead of the numerical node ID, you can include the corresponding generated node ID header generated by the open62541 nodeset compiler (see Step 9 of this tutorial).

Enforcing EURange

AnalogItemType nodes may have EURange optional information that describes the maximum and minimum valid values. The range must be enforced in the read and write callback functions of process variables. There are three rules that may be applied to an EURange violation:

  • ignore the new out-of-range input and stay on the current value
  • change the value to maximum/minimum in-range value
  • force the out-of-range value
Published inopen62541

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.