python - Python OPCUA DisplayNames 未显示
问题描述
当我尝试使用 Python(运行 python 3.7)启动 OPC UA 服务器并为节点设置 DisplayName 时,我的行为很奇怪。DisplayName 属性设置正确。当我在 OPC UA 客户端工具(如https://github.com/FreeOpcUa/opcua-client-gui )中检查它时,您可以在属性区域中看到 DisplayName 的值……但正如预期的那样,树视图没有显示第一列中的 DisplayName。
有什么不对?我是否监督某事或可能做错了什么?可能不支持吗?问题是,如果我使用 python OPC UA Modeler https://github.com/FreeOpcUa/opcua-modeler设置一个 OPC UA 服务器并连接到该服务器,DisplayNames 将显示在第一列中。
有什么想法或建议吗?提前谢谢。
这是示例代码
import sys
import locale
import time
from datetime import datetime
from opcua import ua, uamethod, Server
# Set Locale
locale.setlocale(locale.LC_ALL, 'de_DE')
t = time.strftime("%a, %d %b %Y %H:%M:%S")
print (t) # works fine: Fr, 05 Jun 2020 14:37:02
# Set Server
server = Server()
server.set_endpoint("opc.tcp://localhost:48400")
uri = "http://opcfoundation.org/UA/"
idx = server.register_namespace(uri)
objects = server.get_objects_node()
storage = objects.add_object(idx, "storage")
storage.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Lager")))
storage.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("")))
print("")
print(storage.get_display_name()) # Works fine: LocalizedText(Encoding:2, Locale:None, Text:Lager)
print("")
st_ready = storage.add_variable(idx, "storage_ready", True)
st_ready.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Lager bereit")))
st_ready.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("")))
stock = storage.add_object(idx, "stock")
stock.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Lagerbestand")))
stock.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("")))
# Values in Stock
circles = stock.add_object(idx, "circles")
circles.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Kreise")))
squaes = stock.add_object(idx, "squares")
squaes.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Quadrate")))
triangles = stock.add_object(idx, "triangles")
triangles.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Dreiecke")))
red_c = circles.add_variable(idx, "red", 1)
red_c.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Rot")))
blue_c = circles.add_variable(idx, "blue", 1)
blue_c.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Blau")))
red_s = squaes.add_variable(idx, "red", 1)
red_s.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Rot")))
blue_s = squaes.add_variable(idx, "blue", 1)
blue_s.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Blau")))
red_t = triangles.add_variable(idx, "red", 0)
red_t.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Rot")))
blue_t = triangles.add_variable(idx, "blue", 1)
blue_t.set_attribute(ua.AttributeIds.DisplayName, ua.DataValue(ua.LocalizedText("Blau")))
# Start Server
server.start()
这是没有 DisplayNames 但在 OPC UA 客户端窗口的属性区域中设置值的第一列。
解决方案
set_attribute 函数仅更新属性部分中的值。如果您希望使用 python-opcua 并在树视图中获取 DisplayName,那么您可以尝试下面的代码(从您的示例代码修改的代码)。使用add_<function_name>函数在树视图中设置显示名称和浏览名称。您可以看到 OPC UA 客户端工具的屏幕截图,其中 DisplayName 按预期显示在树视图中。
import sys
import locale
import time
from datetime import datetime
from opcua import ua, uamethod, Server
# Set Locale
locale.setlocale(locale.LC_ALL, 'de_DE')
t = time.strftime("%a, %d %b %Y %H:%M:%S")
print (t) # works fine: Fr, 05 Jun 2020 14:37:02
# Set Server
server = Server()
server.set_endpoint("opc.tcp://localhost:48400")
uri = "http://opcfoundation.org/UA/"
idx = server.register_namespace(uri)
objects = server.get_objects_node()
storage = objects.add_object(idx, "Lager")
storage.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("")))
print("")
print(storage.get_display_name()) # Works fine: LocalizedText(Encoding:2, Locale:None, Text:Lager)
print("")
st_ready = storage.add_variable(idx, "Lager bereit", True)
st_ready.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("")))
stock = storage.add_object(idx, "Lagerbestand")
stock.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("")))
# Values in Stock
circles = stock.add_object(idx, "Kreise")
circles.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("circles")))
squares = stock.add_object(idx, "Quadrate")
squares.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("squares")))
triangles = stock.add_object(idx, "Dreiecke")
triangles.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("triangles")))
red_c = circles.add_variable(idx, "Rot", 1)
red_c.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("red")))
blue_c = circles.add_variable(idx, "Blau", 1)
blue_c.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("blue")))
red_s = squares.add_variable(idx, "Rot", 1)
red_s.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("red")))
blue_s = squares.add_variable(idx, "Blau", 1)
blue_s.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("blue")))
red_t = triangles.add_variable(idx, "Rot", 0)
red_t.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("red")))
blue_t = triangles.add_variable(idx, "Blau", 1)
blue_t.set_attribute(ua.AttributeIds.Description, ua.DataValue(ua.LocalizedText("blue")))
# Start Server
server.start()
由于 python-opcua 库处于维护模式,您可以尝试 opcua-asyncio 中可用的示例,它是 python-opcua 的分支:https ://github.com/FreeOpcUa/opcua-asyncio/tree/master/examples
您可以参考此处提供的文档:https ://opcua-asyncio.readthedocs.io/en/latest/ 您还可以尝试这些您可能会感兴趣的开源 OPC UA 实现:
- open62541 – https://open62541.org/certified-sdk.html – C 堆栈 | Mozilla 许可证 | 嵌入式就绪 | TSN 就绪
- NodeOPCUA – https://node-opcua.github.io/ – NodeJS | 麻省理工学院许可证 | 云就绪
- UA .NET 堆栈 – https://github.com/OPCFoundation/UA-.NETStandard – 适用于 OPC 基金会企业成员的 RCL 许可证和其他人的 GPL 2.0 | 标准型材 | 面向Web的实现
如果您正在寻找更多动手信息(它使用另一个开源堆栈),您还可以查看以下资源:
- 免费文档:可以查看开源文档页面:https ://open62541.org/doc/current/
- 付费在线课程:OPC UA 实用介绍 - 本课程中的代码演练和示例使用 open62541 堆栈: https ://opcfoundation.org/products/view/practical-introduction-to-opc-ua-part-i
推荐阅读
- sql - 如何将 SQL 作业更改为不同的时区
- node.js - 在内部发出另一个 http 请求后发送 http 响应时出错:“发送后无法设置标头”
- apache-spark - 如何在单机上创建集群hadoop环境?
- django - Django 管理表单错误,删除有错误的表单,然后按取消
- python - 转义数组中的项目(Python)
- python - 如何在python中获取子字符串索引
- django - 如何将具有 FileField 的 Django 模型转换为 JSON
- wordpress - .htaccess - 如果站点 URL 以某个域开头,则删除尾部斜杠
- android - 添加 Firebase 应用内消息依赖会产生 DexArchiveMergerException
- java - 在线程内使用方法