OPC-UA has been established as one of the most common communication protocol in the Industrial Internet of Things (IIoT) spaning from field level to the enterprise level.
This article will show how simple it is to build a server / client projects. The programming language is purely the author’s choice.
What will be built at the end of this article
OPC-UA Server in python having analog, digital and string variables
OPC-UA Client using .net winforms
Containierize the server
Let`s build
The server
The OPC-UA server is going to be developed in python. For the development, create a virtual environment. For this article, I will be using poetry.
Open up a terminal and initialize a virtual environment
Install the dependencies
Create a file server.py
Add the import
Create a main function and setup server
Create an object and numeric variable within the object
Start the server
Start the script
Start the server
Switch to the virtual environment
Start the server
Sample start output
The client
Now let’s create the client to interact with the server in Visual Studio 2022
Create a Windows Forms App in C# language
Give a project name: OPC.Client.Demo and select a folder to store the solution
Select .NET 8.0 (Long Term Support) (at the time of writing, this is the latest LTS version)
In the Form-Designer, make the following:
Control
Name
Text
Description
Label
lblStatus
Display the connection status
Label
lblNodeValue
Shows the current node-value
Label
label1
OPC Server
Label
label2
Node ID
—
—
—
Textbox
txtServerUrl
opc.tcp://localhost:4840/server
Set the server url
Textbox
txtNodeId
Set the Node-ID
Textbox
txtNodeWrite
Write value to the node id
—
—
—
Button
btnConnect
Connect
Connect to the server
Button
btnNodeRead
Read
Read value of the node-id
Button
btnNodeWrite
Write
Write to the node
Install the Nuget package OPCFoundation.NetStandard.Opc.Ua
Use the following code
In the Form1.cs add 2 private variables
Function: Button - Connect
Function: Button - Read
Function: Button - Write
Now start the Windows Form project.
Containierize the server
Create the requirements.txt file using poetry
—without-hashes removes the package hashes
—without-urls removes the package urls
Create a Dockerfile
Build the image
Create a docker-compose.yml file
Now you can start the server in network and not worry about local environment