OPC UA Server and Client Simple Example – Python
OPC Unified Architecture (OPC UA) is a machine to machine communication protocol for industrial automation developed by the OPC Foundation.
Below is the example code to exhange data between two machine in network.
As a example we are using python.
Create Test Server on Machine 1:
from opcua import Server
from random import randint
import datetime
import time
server = Server()
url = "opc.tcp://127.0.0.1:4840"
server.set_endpoint(url)
name = "OPC_SIMULATION_SERVER"
addspace = server.register_namespace(name)
node = server.get_objects_node()
Param = node.add_object(addspace, "Parameters")
Temp = Param.add_variable(addspace, "Temperature", 0)
Press = Param.add_variable(addspace, "Pressure", 0)
Time = Param.add_variable(addspace, "Time", 0)
Temp.set_writable()
Press.set_writable()
Time.set_writable()
server.start()
print("Server started at {}".format(url))
while True:
Temperature = randint(10,50)
Pressure = randint(200, 999)
TIME = datetime.datetime.now()
print(Temperature, Pressure, TIME)
Temp.set_value(Temperature)
Press.set_value(Pressure)
Time.set_value(TIME)
time.sleep(0.1)
Create Test Client Machine 2:
from opcua import Client
import time
url = "opc.tcp://127.0.0.1:4840"
client = Client(url)
client.connect()
print("Client Connected")
while True:
Temp = client.get_node("ns=2;i=2")
Temperature = Temp.get_value()
print(Temperature)
Download and Install OPC UA Client Free from Unified Automation Website ( https://www.unified-automation.com/downloads/opc-ua-clients.html ) to view Node ids.
Connect Machine 1 and Machine 2 with suitable network (e.g. Ethernet)
Start server and then run client
Client will print fetched data from Server.
That’s cool Testing is completed !
Also,
Example Code for OPC UA Client for KUKA OPC UA Server on Robot Controller:
Code has to run on client side(Laptop with Python installed).
Prerequisites:
1.OPC UA installed on KUKA Controller.
Install OPCUA using PIP install
PIP Install Cryptography
Below code will connect with KUKA Controller OPC port > fetch data from node > Print Data (Here MyPos Variable from $config.dat)>Logs data to CSV file to specified path.
from opcua import Client
import time
import csv
print("Starting")
url = "opc.tcp://192.168.1.147:4840" # IP Address of KLI - KUKA
client = Client(url)
client.set_user("opcuaoperator")#Check username with KUKA OPC Documentation
client.set_password("kuka")#Check password with KUKA OPC Documentation
client.connect()
print("Client Connected to KUKA OPC Server")
X = client.get_node("ns=5;s=MotionDeviceSystem.ProcessData.R1.System.$config.MYPOS.X")
Y = client.get_node("ns=5;s=MotionDeviceSystem.ProcessData.R1.System.$config.MYPOS.Y")
Z = client.get_node("ns=5;s=MotionDeviceSystem.ProcessData.R1.System.$config.MYPOS.Z") # $ACCU_STATE is battery state
Xx = str(X.get_value())
Yy = str(Y.get_value())
Zz = str(Z.get_value())
print("My Pos:" + "X : " + Xx + " Y: " + Yy + " Z: " + Zz)
temp = X.set_value(1200)
temp = Y.set_value(5)
temp = Z.set_value(1174)
with myFile:
writer = csv.writer(myFile)
writer.writerows([now,now])
with open("E:/"filename".csv", "a") as log:
log.write("{0},{1}\n".format(now,str(Xx)))
See complete “KUKA OPC UA with GUI on Python” at https://github.com/pawankumargurav/kukaopcua