我有一个例子,其中一个方法被添加到服务器空间。该示例设置为获取两个输入值并将它们相加
代码示例from opcua import Server, ua, uamethod, instantiate
import time
from opcua.ua import NodeId, NodeIdType
@uamethod
def add_func(self, a, b):
print("Adding parameters:\n", a, "\tand\t", b)
return a + b
if __name__ == "__main__":
# setup server
server = Server()
server.set_endpoint("opc.tcp://localhost:4840") #alternative:
server.set_endpoint("opc.tcp://0.0.0.0:4840")
server.set_server_name("Server example")
server.set_security_policy([ua.SecurityPolicyType.NoSecurity])
uri = "http://example.uri.de"
idx = server.register_namespace(uri)
# get Objects node, this is where we should adress our custom stuff to
objects = server.get_objects_node() # alternative: root_node = server.get_root_node()
# add structure to the namespace (idx) below the Objects node
myobj = objects.add_object(idx, "MyObject")
# add variables, type can be defined by assigning a value or ua Variant Type definition
var1 = myobj.add_variable(idx, "var1", 321, ua.VariantType.Int64)
var2 = myobj.add_variable(idx, "var2", 123, ua.VariantType.Int64)
# set variables writable for clients (e.g. UaExpert)
var1.set_writable()
var2.set_writable()
# set input arguments for method
input_arg1 = ua.Argument() # create new argument
input_arg1.Name = "a" # name for argument
input_arg1.DataType = ua.NodeId(ua.ObjectIds.Int64) # data type
input_arg1.ValueRank = -1 # value rank to be treated a scalar number
input_arg1.Description = ua.LocalizedText("First integer a") # add description text
input_arg2 = ua.Argument()
input_arg2.Name = "b"
input_arg2.DataType = ua.NodeId(ua.ObjectIds.Int64)
input_arg2.ValueRank = -1
input_arg2.Description = ua.LocalizedText("Second integer b")
# set output arguments
output_arg = ua.Argument()
output_arg.Name = "Result"
output_arg.DataType = ua.NodeId(ua.ObjectIds.Int64)
output_arg.ValueRank = -1
output_arg.Description = ua.LocalizedText("Sum")
print(var1.nodeid)
print(var2.nodeid)
# add method to namespace
mymethod = myobj.add_method(idx, "MyAddFunction", add_func, [input_arg1, input_arg2], [output_arg])
server.start()
我想做的更改是删除输入参数并自动接受var1&;调用MyAddFunction时将var2作为输入。 我使用UaExpert调用此方法。 UaExpert when calling MyAddFunction
我尝试通过使用变量的NodeID传递变量,如下所示:
mymethod = myobj.add_method(idx, "MyAddFunction", add_func, [var1.nodeid, var1.nodeid], [output_arg])
并获取以下错误消息: opcua.ua.uaerrors.\u base.UaError:NodeId:无法猜测NodeId的类型,请设置NodeId类型
创建变量时是否未定义类型? 采用var1&;的方法需要做哪些更改;var2作为输入参数?是否有方法将这些值分配给创建的输入参数(input_arg1和input_arg2)
提前谢谢你! 问候南多
目前没有回答
相关问题 更多 >
编程相关推荐