WOLFRAM SYSTEM MODELER
GettingStartedGetting started with OPCUA |
![]() |
SystemModel["OPCUA.Documentation.GettingStarted"]
This getting started guide will provide you with the basic knowledge needed to use OPCUA. If you are new to OPC, it is recommended that you visit the OPC Foundation webpage to get more acquainted with this protocol and common terms related to it.
While the OPCClassic Library is limited to communication between Windows computers, being based on the proprietary Component Object Model (COM) interface, OPC UA can be used to communicate on many other platforms, such as Windows, macOS, Linux and Raspberry Pi. Communication between different platforms is also possible using the OPCUA Library.
The OPC UA specification supports reading data in real time from nodes and reading historical data. OPC UA also has support for scalability, multithreading and security. To see what is implemented in the OPCUA Modelica library here in System Modeler, see Limitations at the bottom of this page.
To be able to perform the steps in this tutorial, you need the following:
Installations:
Other:
You also need a basic knowledge of System Modeler. It is recommended that you check out the learning resources at the Wolfram webpage if you need to brush up on your System Modeler skills.
Connecting to an OPC UA Server
To be able to use OPCUA, you need to establish a connection to an OPC UA server running on a local or remote host. For more information on this, see the section on Common OPC UA Issues at the end of this tutorial.
As a first exercise, you will reproduce the example model OPCUA.Examples.MultipleServers that is shipped with OPCUA. It demonstrates conceptually how to connect to two different OPC servers.
The first thing you need to do is to make sure you have access to starting an OPC Server.
In this particular example, you actually have to have access to two servers.
Included in the OPCUA Library are two example servers, built using the open62541 library.
They can be found by following this link.
If you are on a Windows machine, start the file StartExampleServers.bat
, or if you are on another operating system, such as Linux or macOS, start the file StartExampleServers
.
After you have successfully started the simulation servers, you can start to build your model. To do so, you need to locate the following components:
Modelica.Blocks.Continuous.Filter
(from the Modelica Standard Library)Modelica.Blocks.Sources.CombiTimeTable
(from the Modelica Standard Library)Create a new model in System Modeler and drag each component onto the diagram view. Connect the components as shown in Fig. 1.
Fig. 1. Diagram view of OPCUA.Examples.MultipleServers
.
Next, you need to specify certain component parameters.
Start with the component named clien1
, shown in Fig. 2. Select the component and click the General tab at the bottom of the main window.
Fig. 2. The client1
component.
In the Parameters view, you will find the parameter endpointUrl
. endpointUrl
needs to be specified for each instance of the client component.
Since the the simulation servers have been started on your local computer, "opc.tcp://localhost:16664"
can be kept as endpointUrl
.
Fig. 3. The General view of the client1
component, with all parameters specified.
Repeat the procedure for the second OPC client component (client2
in Fig. 1), but in this case use the endpointUrl "opc.tcp://localhost:16665"
.
Move on to component read
. Again, select the component and click the General tab.
Fig. 4. The read
component highlighted.
The interesting parameters here are nodeID
, nodeIDType
and nodeNamespace
.
Together, they identify the node on the server that you want to read data from (or write data to, in the case of a component for writing data).
The node you specify has to exist on the server, and it has to be available for the kind of operation you want to perform, as some nodes are available for either reading or writing but not both.
Enter these values under the General tab on the read
component:
read
nodeID
: "51031"nodeIDType
: OPCUA.Types.UANodeIdType.NumericId
nodeNamespace
: 1Fig. 5. The General view of the read
component, with all parameters specified.
For the remaining components, choose the following parameters:
write
nodeID
: "51031"nodeIDType
: OPCUA.Types.UANodeIdType.NumericId
nodeNamespace
: 1writeTableData
nodeID
: "51031"nodeIDType
: OPCUA.Types.UANodeIdType.NumericId
nodeNamespace
: 1writeClock
factor
: 100writeClock
provides the write clock used by client1
shift
shiftCounter
: 1resolution
: 10shift
shifts the writeClock
by 1/10
clock cycles.
The shifted clock is used as read clock by client1
and as write clock for client2
.
filter
analogFilter
: Modelica.Blocks.Types.AnalogFilter.Butterworth
filterType
: Modelica.Blocks.Types.FilterType.LowPass
order
: 3f_cut
: 0.2 Hzf_min
: 1.6 HzcombiTimeTable
table[:,:]
: {{0, 10}, {1, 9}, {5, 3}, {8, 2}, {11, 6}, {14, 3}, {15, 8}, {16, 5}, {18, 10}, {22, 7}, {24, 5}, {25, 1}, {27, 6}, {30, 10}}
The combiTimeTable
is providing values that have been randomly generated and that will be written to the first server by client1
. These will then be read filtered and then written to the other server by client2
. The function of the filter is to slow the rate of change coming from the random values from the combiTimeTable
.
What does this model do exactly?
First of all, it connects to two OPC servers. The OPC servers are both running locally but on different ports (16664 and 16665).
If you would like to connect to remote servers, you would have to have access to another server (or two servers) on a remote host.
To connect to that server, endpointUrl
(URL to OPCUA server endpoint) would need to be updated.
The combiTimeTable
component is connected to a write component, writeTableData
, that is writing values to node 51031 on the server that client1
is connected to.
The write rate is controlled by the writeClock
, and in this case the clock period is set to 0.1 s.
Furthermore, client1
is also connected to a read component, read
, that reads data from node 51031 on the server every 0.1 seconds but shifted 1/10
clock cycles as specified by shift
component.
The sampled signal is then passed through a Butterworth lowpass filter with a cutoff frequency of 2.0 Hz.
The filtered signal is sampled by write
and finally written to client2
, a second instance of the OPC client.
The model exemplifies a possible use of connecting to multiple OPC UA servers in one model, where one server is used for reading data and the other for writing data.
You are now getting ready to simulate your newly built model, which brings up the issue of which experiment settings to use for the simulation. For the majority of all practical applications of OPCUA, "Fixed step explicit Euler", with a step size of your choice, should be used as the solver method. For this scenario, use a step size of 0.01 s. Simulate for 30 s. You should also check the Synchronize with real time checkbox.
Fig. 6. The experiment settings specified for the MultipleServers
model.
The first time you simulate the model, if the Synchronize with real time checkbox is unchecked, the simulation is probably too fast for you to actually see what is happening in the simulation. The reason is that System Modeler simulates the model as fast as possible by default in this case. In order to study the simulation in real time, you can also check the Synchronize with real time checkbox in Simulation Center (see Fig. 7).
Fig. 7. Check the Synchronize with real time option in the Settings tab in Simulation Center.
If you are building many models with OPCUA, consider setting the Synchronize with real time option as a default simulation setting. This is done by going to Tools ► Options and choosing Default Experiment under the Simulation Center header. There, check the Synchronize with real time checkbox.
To see the data sampled from the first server, client1
, plot read.y
over time.
Add a second subplot and plot the sampled filtered signal write.uSampled
written to the second server by client2
; the result should be similar to that in the plot below.
It will probably not be identical, since different simulations read data from the server node at different times.
When connecting to an OPC server on a remote host, you will have to be able to connect to that host from the machine you are using. If you are not certain, try to ping the remote host to see if you can connect to it.
If you are having other problems with your OPCUA models, going through the following checklist might help.
endpointUrl
?nodeIDs
, nodeNamespaces
and nodeIDTypes
?nodeIDs
and endpointUrl
have quotation marks?If you are still experiencing problems, visit http://community.wolfram.com and ask a question or contact support@wolfram.com.
This implementation of OPC UA handles reading and writing values to nodes of the types Real
, Integer
and Boolean
.
The nodes should also be scalar, 1D or 2D arrays.
There is currently no support for reading historical values in OPCUA.