问题描述
我想知道是否可以将数据从 OPC UA 客户端发送到服务器.我有一台装有 OPC UA 服务器的 Windows 10 PC 和一些 Raspberry Pi 作为客户端.
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.
我已经编写了 Python 代码来将数据从服务器发送到客户端.现在,我想将数据从 Raspberry Pi 上的客户端发送到 Windows 10 PC 上的服务器.这能做到吗?还是我必须在 Raspberry Pi 上设置服务器,在 Windows 10 PC 上设置客户端?
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?
这是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)
这是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)
推荐答案
简短的回答是肯定的!您可以从 OPC UA 客户端写入、读取和订阅 OPC UA 服务器上可用的标签.实际上,这就是我们需要客户的原因.
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.
我认为您的困惑始于对 OPC UA 的服务器/客户端架构的工作方式的误解.考虑到上面共享的代码,您的 OPC UA 服务器不会向您的客户端发送任何数据.您的客户端请求并从服务器读取它.同理,你只需要从同一个客户端向服务器发送另一个请求来写入/设置值.例如;
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!
这篇关于opc ua 客户端到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!