首页 > 解决方案 > 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 客户端窗口的属性区域中设置值的第一列。

OPC UA 客户端窗口

标签: pythonopc-uadisplayname-attribute

解决方案


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 实现:

如果您正在寻找更多动手信息(它使用另一个开源堆栈),您还可以查看以下资源:


推荐阅读