问题
I would like to know if I can send data from an OPC UA Client to Server. I have a Windows 10 PC with OPC UA Server set up and some Raspberry Pi as Clients.
I already programmed Python code to send data from Server to Client. Now, I want to send data from the clients on Raspberry Pis to the Server on Windows 10 PC. Can this be done? Or will I have to set up the servers on Raspberry Pis, and clients on Windows 10 PC?
This the server.py
:
from opcua import Server
from random import randint
import datetime
import time
server = Server()
url = "opc.tcp://131.246.76.240:4840"
server.set_endpoint(url)
name = "OPCUA_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(1)
This is client.py
:
import time
from opcua import Client
url = "opc.tcp://131.246.76.240: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)
Press = client.get_node("ns=2;i=3")
Pressure = Press.get_value()
print(Pressure)
TIME = client.get_node("ns=2;i=4")
Time = TIME.get_value()
print(Time)
time.sleep(1)
回答1:
The short answer is yes! You can write, read, and subscribe tags available on an OPC UA Server from an OPC UA Client. Actually, that's why we need a client.
I reckon your confusion starts with a misunderstanding of how OPC UA's server/client architecture works. Considering the code shared above, your OPC UA Server does not send any data to your client. Your client requests and reads it from the server. In the same way, you only need to send another request from the same client to the server to write/set value. For instance;
# set/write node value (e.g. 26) by using implicit data type
Temp = client.get_node("ns=2;i=2")
Temp.set_value(26)
All in all, no need to deploy and set up more servers or clients. Just update your client-side code to support writing tags. Hope this helps!
来源:https://stackoverflow.com/questions/55025311/opc-ua-client-to-server