如何开发对接OPC UA客户端与HTTP/XML API的OPC UA服务器应用?
OPC UA服务器与HTTP/XML API交互起步方案
一、技术栈选型
优先选成熟工具降低起步门槛,推荐组合:
- OPC UA服务器:
- Python:
FreeOpcUa(轻量易上手,文档齐全) - .NET:OPC Foundation官方库(企业级支持完善)
- Java:Eclipse Milo(跨平台,生态成熟)
- Python:
- HTTP/XML API:
- 对应语言Web框架:Python用FastAPI/Flask,.NET用ASP.NET Core,Java用Spring Boot
- XML解析:Python用
xml.etree.ElementTree,.NET用XmlSerializer,Java用JAXB
二、第一步:搭建基础OPC UA服务器
以Python + FreeOpcUa为例,快速搭建可访问的服务器:
from opcua import Server import time # 初始化服务器 server = Server() server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/") # 注册命名空间 uri = "http://examples.freeopcua.github.io" idx = server.register_namespace(uri) # 创建根节点下的对象与变量节点 objects = server.get_objects_node() my_obj = objects.add_object(idx, "MyObject") my_var = my_obj.add_variable(idx, "MyVariable", 0) my_var.set_writable() # 设置变量可读写 # 启动服务器 server.start() print("OPC UA服务器已启动,端点:opc.tcp://0.0.0.0:4840/freeopcua/server/") try: while True: time.sleep(1) # 模拟变量值自增 current_val = my_var.get_value() my_var.set_value(current_val + 1) finally: server.stop()
运行代码后,即可得到一个带自增变量的OPC UA服务器。
三、第二步:对接OPC UA浏览器客户端
- 桌面客户端测试:用UA Expert输入上述端点地址,连接后可直接浏览
MyObject下的MyVariable节点,实时查看值变化。 - 浏览器端客户端:基于JavaScript的
node-opcua可实现简单浏览器客户端,核心逻辑为连接服务器、浏览节点、订阅值变化。
四、第三步:实现HTTP/XML API模块
以Python + FastAPI为例,搭建处理XML的HTTP接口:
from fastapi import FastAPI, Request from fastapi.responses import XMLResponse import xml.etree.ElementTree as ET from opcua import Client app = FastAPI() # 连接本地OPC UA服务器 opcua_client = Client("opc.tcp://localhost:4840/freeopcua/server/") opcua_client.connect() # 读取OPC UA节点值,返回XML格式响应 @app.get("/api/opcua/read", response_class=XMLResponse) async def read_opcua_node(): var_node = opcua_client.get_node("ns=2;i=2") # 替换为你的节点ID val = var_node.get_value() # 构造XML响应 root = ET.Element("OPCUAData") ET.SubElement(root, "NodeID").text = "ns=2;i=2" ET.SubElement(root, "Value").text = str(val) return ET.tostring(root) # 接收XML请求,写入OPC UA节点 @app.post("/api/opcua/write") async def write_opcua_node(request: Request): xml_content = await request.body() root = ET.fromstring(xml_content) node_id = root.find("NodeID").text value = int(root.find("Value").text) var_node = opcua_client.get_node(node_id) var_node.set_value(value) return {"status": "success"} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)
该接口支持:
- GET请求读取OPC UA节点,返回XML格式数据
- POST请求接收XML写入指令,修改OPC UA节点值
五、第四步:打通双向数据通道
1. OPC UA → HTTP/XML
在OPC UA服务器端添加值变化订阅,节点值改变时主动发送XML到HTTP API:
# 在之前的OPC UA服务器代码中添加订阅逻辑 from opcua import Subscription, DataChangeNotification import requests def handler(event): # 构造XML数据 root = ET.Element("OPCUAChange") ET.SubElement(root, "NodeID").text = event.node.to_string() ET.SubElement(root, "NewValue").text = str(event.monitored_item.Value.Value.Value) xml_data = ET.tostring(root) # 发送HTTP POST请求到API requests.post("http://localhost:8000/api/opcua/notify", data=xml_data, headers={"Content-Type": "application/xml"}) # 创建订阅,500ms检测一次值变化 sub = server.create_subscription(500, handler) sub.subscribe_data_change(my_var)
同时在HTTP API端添加接收通知的接口:
@app.post("/api/opcua/notify") async def receive_opcua_notification(request: Request): xml_content = await request.body() # 处理通知数据(如存储、转发) print("收到OPC UA值变化通知:", xml_content) return {"status": "received"}
2. HTTP/XML → OPC UA
即上述第四步中的写入接口,通过接收XML请求直接修改OPC UA节点值,实现数据从HTTP到OPC UA的同步。
六、起步阶段注意事项
- 权限控制:初期可跳过,正式开发需给OPC UA服务器添加用户认证,HTTP API添加密钥验证
- 错误处理:给OPC UA连接、HTTP请求、XML解析添加异常捕获,避免程序崩溃
- 日志记录:记录OPC UA节点变化、HTTP请求日志,方便调试问题
- 数据类型映射:明确OPC UA数据类型(如Int32、String)与XML元素的对应关系,避免转换错误
内容的提问来源于stack exchange,提问作者Randy Tjoa
相关产品推荐
相关产品推荐

