在Open62541中添加不带输入参数的方法

huangapple go评论61阅读模式
英文:

Adding method without input arguments in Open62541

问题

I've been working for a long time with Python and the FreeOPCUA Stack, but I wanted to go into more low-level stuff, so I wanted to try the Open62541 Stack. My C skills are a bit rusted, but I understand the concepts and syntax.

我已经用Python和FreeOPCUA Stack工作了很长时间,但我想深入研究更底层的东西,所以我想尝试Open62541 Stack。我的C技能有点生疏,但我理解概念和语法。

I am trying to create a method node without an input argument. Once called, the method should call a function and return as output the function's output. In Python, the arguments are passed into the add_method as a list. In C, it should be a string or an array of strings.

我正在尝试创建一个没有输入参数的方法节点。一旦调用,该方法应该调用一个函数,并将函数的输出作为输出返回。在Python中,参数被传递到add_method作为一个列表。在C中,它应该是一个字符串或字符串数组。

How should I write it in order to have just a function call, without inputs?

我应该如何编写它,以便只进行函数调用,没有输入参数?

I was following the docs at https://www.open62541.org/doc/1.0/tutorial_server_method.html, but the two examples take input arguments (even the hello world), and since then I am stuck.

我曾经按照https://www.open62541.org/doc/1.0/tutorial_server_method.html上的文档,但两个示例都使用了输入参数(甚至是hello world),从那以后我就卡住了。

英文:

I've been working for a long time with Python and the FreeOPCUA Stack, but I wanted to go into more low-level stuff, so I wanted to try the Open62541 Stack. My C skills are a bit rusted, but I understand the concepts and syntax.

I am trying to create a method node without an input argument. Once called, the method should call a function and return as output the function's output. In Python, the arguments are passed into the add_method as a list. In C, it should be a string or an array of strings.

How should I write it in order to have just a function call, without inputs?

I was following the docs at https://www.open62541.org/doc/1.0/tutorial_server_method.html, but the two examples take input arguments (even the hello world), and since then I am stuck.

答案1

得分: 1

以下是您提供的代码的中文翻译:

我实际上在做一些愚蠢的事情,编译错误。输入参数大小可以设置为零,并且可以将输入参数的引用设置为NULL。

我的方法定义如下:

static UA_StatusCode
enableRobotMethodCallback(UA_Server *server,
                         const UA_NodeId *sessionId, void *sessionHandle,
                         const UA_NodeId *methodId, void *methodContext,
                         const UA_NodeId *objectId, void *objectContext,
                         size_t inputSize, const UA_Variant *input,
                         size_t outputSize, UA_Variant *output) {
    UA_String tmp = UA_STRING_ALLOC(enableRobot());
    UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
    UA_String_clear(&tmp);
    return UA_STATUSCODE_GOOD;
}

static void
addEnableRobotMethod(UA_Server *server) {
    UA_Argument outputArgument;
    UA_Argument_init(&outputArgument);
    outputArgument.description = UA_LOCALIZEDTEXT("en-US", "一个字符串");
    outputArgument.name = UA_STRING("MyOutput");
    outputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
    outputArgument.valueRank = UA_VALUERANK_SCALAR;

    UA_MethodAttributes enableRobot = UA_MethodAttributes_default;
    enableRobot.description = UA_LOCALIZEDTEXT("en-US", "启用机器人");
    enableRobot.displayName = UA_LOCALIZEDTEXT("en-US", "启用机器人");
    enableRobot.executable = true;
    enableRobot.userExecutable = true;
    UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,20),
                            UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
                            UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
                            UA_QUALIFIEDNAME(1, "启用机器人"),
                            enableRobot, &enableRobotMethodCallback,
                            0, NULL, 1, &outputArgument, NULL, NULL);
}

请注意,我已将代码中的HTML实体(如&")还原为其原始形式,以便更清晰地阅读代码。

英文:

I was actually doing something silly and compiling it wrong. Input arguments size can be set to zero and the reference to the input argument can be set to NULL.

My method definition follows:

static UA_StatusCode
enableRobotMethodCallback(UA_Server *server,
                         const UA_NodeId *sessionId, void *sessionHandle,
                         const UA_NodeId *methodId, void *methodContext,
                         const UA_NodeId *objectId, void *objectContext,
                         size_t inputSize, const UA_Variant *input,
                         size_t outputSize, UA_Variant *output) {
    UA_String tmp = UA_STRING_ALLOC(enableRobot());
    UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
    UA_String_clear(&tmp);
    return UA_STATUSCODE_GOOD;
}

static void
addEnableRobotMethod(UA_Server *server) {
    UA_Argument outputArgument;
    UA_Argument_init(&outputArgument);
    outputArgument.description = UA_LOCALIZEDTEXT("en-US", "A String");
    outputArgument.name = UA_STRING("MyOutput");
    outputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
    outputArgument.valueRank = UA_VALUERANK_SCALAR;

    UA_MethodAttributes enableRobot = UA_MethodAttributes_default;
    enableRobot.description = UA_LOCALIZEDTEXT("en-US","Enable Robot");
    enableRobot.displayName = UA_LOCALIZEDTEXT("en-US","Enable Robot");
    enableRobot.executable = true;
    enableRobot.userExecutable = true;
    UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,20),
                            UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
                            UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
                            UA_QUALIFIEDNAME(1, "Enable Robot"),
                            enableRobot, &enableRobotMethodCallback,
                            0, NULL, 1, &outputArgument, NULL, NULL);
}

huangapple
  • 本文由 发表于 2023年6月29日 20:08:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定