Создать метод opc-ua, имеющий доступ к узлам. Моя основная цель — вернуть список узлов (например, все узлы в одной папке или определенных папках) с помощью одного вызова сервера, а не по одному для каждого узла.
Что я пробовал< /h2>
Я нашел этот пример методов opc-ua, который у меня работает:
https://github.com/ifak-prototypes/opcu ... rverSimple
Но этот не имеет доступа к узлам.
Минимальный нерабочий пример
Я создал минимальный нерабочий пример сервера, который предоставляет два узла (Node1, Node2) и метод sum().
Код: Выделить всё
from opcua import ua, Server
def sum_method(parent):
val1 = node1.get_value()
val2 = node2.get_value()
return [ua.Variant(val1 + val2, ua.VariantType.Double)]
if __name__ == "__main__":
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
uri = "http://example.opcua.server"
idx = server.register_namespace(uri)
objects = server.get_objects_node()
# Add two nodes with initial values
node1 = objects.add_variable(idx, "Node1", 5.5)
node2 = objects.add_variable(idx, "Node2", 10.5)
node1.set_writable()
node2.set_writable()
# Add a method to sum the two nodes
objects.add_method(idx, "sum", sum_method, [], [ua.VariantType.Double])
# Start the server
server.start()
try:
input("Server is running. Press Enter to stop...")
finally:
server.stop()
Код: Выделить всё
from opcua import Client
if __name__ == "__main__":
client = Client("opc.tcp://localhost:4840/freeopcua/server/")
try:
# Connect to the server
client.connect()
print("Client connected")
# Access the root node and browse to the Objects node
objects_node = client.get_objects_node()
# Find the method node by browsing or by known ID/path
sum_method = objects_node.get_child(["0:MyObject", "0:sum"])
# Call the method, no input arguments
result = sum_method.call()
print(f"The sum of the two node values is: {result}")
finally:
# Close the connection
client.disconnect()
print("Client disconnected")
Код: Выделить всё
opcua.ua.uaerrors._auto.BadNoMatch: "The requested operation has no match to return."(BadNoMatch)
- Как исправить ошибку? Или...
- Есть ли лучший способ получить несколько значений узлов за один вызов сервера (или меньше)?
- Есть ли примеры (показывающие как серверный, так и клиентский код) для этого?
Подробнее здесь: https://stackoverflow.com/questions/790 ... h-one-call