【问题标题】:Python OPCUA DisplayNames not showingPython OPCUA DisplayNames 未显示
【发布时间】:2020-09-24 17:31:36
【问题描述】:

当我尝试使用 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 client window

【问题讨论】:

    标签: python opc-ua displayname-attribute


    【解决方案1】:

    set_attribute 函数只更新属性部分的值。如果您希望使用 python-opcua 并在树形视图中获取 DisplayName,那么您可以尝试下面的代码(从您的示例代码修改的代码)。使用 add_ 函数在树视图中设置显示名称和浏览名称。您可以看到 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 库处于维护模式,您可以试用 python-opcua 的分支 opcua-asyncio 中提供的示例:https://github.com/FreeOpcUa/opcua-asyncio/tree/master/examples

    您可以参考此处提供的文档:https://opcua-asyncio.readthedocs.io/en/latest/ 您还可以尝试这些您可能会感兴趣的开源 OPC UA 实现:

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

    【讨论】:

    • THX 很多 @thangavaila 看起来确实值得看看不同的实现。但是该示例在树视图中显示了德语名称,但浏览名称仍然与显示名称相同。希望是,在编码标准的意义上,让代码中的浏览名称和变量名称或多或少相同,因此放置德语本地化显示名称会很好。甚至 add_ 函数似乎也无法解决它。但是,嘿,为了获得更可靠的功能,改用 NodeJS 或 C 似乎是最好的。谢谢!
    【解决方案2】:

    我建议使用另一个 OPC UA 客户端工具:

    UaExpert — A Full-Featured OPC UA Client

    原因:

    1. FreeOpcUa 未在持续开发中
    2. 有限的支持文档
    3. UaExpert 设计为支持 OPC UA 功能的通用测试客户端 如数据访问、警报和条件、历史访问和 UA 方法的调用。
    4. 它提供树视图和other features

    【讨论】:

    • @m-b-explorer THX 的建议。在运行 OSX 时,看到 UaExpert 无法在我的机器上运行,真是令人遗憾。但是我设法用同事的机器对其进行了测试。不幸的是,它并没有解决问题。即使在 UaExpert 中,我在 TreeView 中也看不到任何 DisplayName,只是在您看到字符串的属性中。请参阅Screenshot of UaExpert 那么通过在 Python 中设置节点并在之后设置 DisplayNames 我做错了什么吗?非常感谢您提供其他建议!
    猜你喜欢
    • 1970-01-01
    • 2019-07-20
    • 2018-03-17
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2020-12-22
    • 2021-11-19
    • 2013-04-04
    相关资源
    最近更新 更多