Python OPCUA显示名称未显示

2024-04-25 17:40:15 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试用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服务器,并连接到该服务器,则显示名称将显示在第一列中

有什么想法或建议吗?THX提前

下面是示例代码

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()

这是第一列,没有显示名称,但在OPC UA客户端窗口的属性区域中有设置值

OPC UA client window


Tags: addstockattributestorageblueredvariableua
2条回答

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

UaExpert — A Full-Featured OPC UA Client

原因:

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

set_attribute函数仅更新Attributes部分中的值。如果您希望使用python opcua并在树视图中获取DisplayName,那么您可以尝试下面的代码(根据示例代码修改的代码)。显示名称和浏览名称在树状视图中通过使用添加来设置<;功能名称>功能。您可以看到OPC UA客户端工具的屏幕截图,其中DisplayName在树视图中按预期显示。 enter image description here

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

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

相关问题 更多 >

    热门问题