The Open Platform Communications (OPC) is a common and popular communication standard for reliable and secure exchange data in the industrial and automation domains. This standard, among other applications, specifies a series of specifications for exchange of data between servers and clients as well as server to server communication.
OPC UA (Unified Architecture) is a service oriented architecture platform developed to enhance, improve and surpass the abilities and functionalities of the Classic OPC standard, sometimes referred to as OPC Classic.
With its Open Connectiveness, proven security and reliability, OPC UA has become a favored and recognized protocol in the industrial and automation domains. The protocol can work and easily integrate with very many software platforms. In this Tutorial, we are gong to see how to develop a python based OPC UA client to communicate with an OPC UA server.
Step 1. Set up your OPC UA Server
The first step is to make your server ready, up and running. In Case you don’t have a real server, you may use a simulation server for the purpose of testing your client. Follow the tutorial below to see how to install and configure a Simulation OPC UA Server.
Step 2: Install the required python libraries
The opcua python library contains a set of packages and modules to facilitate the implementation of both OPC UA severs and clients in python. To install the library:
pip3 install opcua
Step 3: The code
Import the client module and specify your server endpoint
from opcua import Client
url = "opc.tcp://DESKTOP-FL73J86.mshome.net:53530/OPCUA/SimulationServer"
Create a client object by passing your server endpoint
client = Client(url)
Connect to your server
client.connect()
If your connection was successful, you can now get nodes from our server using the get_node() function and extract data from those nodes using the get_values() function. To get the node, you need to specify the Node namespace along with the node id
tempNode = client.get_node("ns=3;i=1001")
temperature = tempNode.get_value()
To set a value for a node on the server, first you need to get the node, by specifying its namespace and node id. Then you can use the set_values() function to set its value.
numNode = client.get_node("ns=3;i=1008")
client.set_values([numNode], [num+1])
Full Code
You can get the full code from github: brightersidetech/opcua_client_python