UA_Client_Service_read 只产生空响应。

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

UA_Client_Service_read only yields empty responses

问题

我正在尝试在C语言中创建一个小程序,该程序将通过opc/ua连接使用open62541库轮询数据帧,然后将其转发到kafka服务器。

从单独的节点获取值时一切正常,但我想使用UA_ReadRequest来完成。问题在于我只收到空响应。

opc/ua服务器使用freeopc包在Python中编写。

这是尝试使用UA_ReadResponse获取指定节点ID的多个值的函数:

void retrieveOPCData(void)
{
    UA_ReadRequest request;
    UA_ReadRequest_init(&request);
    UA_ReadValueId ids[nodeCount];
    
    for (int i = 0; i < nodeCount; i++)
    {
        UA_ReadValueId_init(&ids[i]);
        ids[i].attributeId = UA_ATTRIBUTEID_VALUE;
        ids[i].nodeId = nodesToRead[i];
    }

    request.nodesToRead = ids;

    for (int i = 0; i < nodeCount; i++)
    {
        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "ID%i: %s, %i", i,
            request.nodesToRead[i].nodeId.identifier.string.data,
            request.nodesToRead[i].nodeId.namespaceIndex);
    }

    UA_ReadResponse response = UA_Client_Service_read(client, request);

    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Status: %i",
        response.responseHeader.serviceResult);
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Responses: %li", response.resultsSize);
}

结果值为UA_STATUSCODE_GOOD,但响应数为0。当像这样一个接一个地获取值时,它可以正常工作:

void readNodeAtIndex(int index)
{
    if (index >= nodeCount)
    {
        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Index out of Range");
        return;
    }

    UA_Variant variant;
    UA_Variant_init(&variant);

    const UA_NodeId nodeId = nodesToRead[index];

    UA_StatusCode retval = UA_Client_readValueAttribute(client, nodeId, &variant);

    if (retval == UA_STATUSCODE_GOOD && UA_Variant_hasScalarType(&variant,
        &UA_TYPES[UA_TYPES_DOUBLE]))
    {
        UA_Double value = *(UA_Double*)variant.data;

        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Double-Value: %f", value);
    }
    else if (retval == UA_STATUSCODE_GOOD && UA_Variant_hasScalarType(&variant,
        &UA_TYPES[UA_TYPES_BOOLEAN]))
    {
        UA_Boolean value = *(UA_Boolean*)variant.data;

        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Boolean-Value: %i", value);
    }
    

    UA_Variant_clear(&variant);
}

opc/ua服务器的设置如下:

server = Server()
space_url = "opc.tcp://localhost:61032"
server.set_endpoint(space_url)
server.set_security_policy([ua.SecurityPolicyType.NoSecurity])
node = server.get_objects_node()

希望这些信息对你有所帮助。

英文:

I am trying to create a small program in C which will poll a frame of data over a opc/ua connection using the open62541 library and the forward it to a kafka server.

Everything works fine when fetching the values from the nodes separately but I would like to use a UA_ReadRequest for that. The problem is that I am only receiving empty responses.

The opc/ua server is coded with in python using the freeopc package.

This is the function that tries tu use a UA_ReadResponse to fetch a several values for specified nodeIDs:

void retrieveOPCData(void)
{
    UA_ReadRequest request;
    UA_ReadRequest_init(&amp;request);
    UA_ReadValueId ids[nodeCount];
    
    for (int i = 0; i &lt; nodeCount; i++)
    {
        UA_ReadValueId_init(&amp;ids[i]);
        ids[i].attributeId = UA_ATTRIBUTEID_VALUE;
        ids[i].nodeId = nodesToRead[i];
    }

    request.nodesToRead = ids;

    for (int i = 0; i &lt; nodeCount; i++)
    {
        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, &quot;ID%i: %s, %i&quot;, i,
            request.nodesToRead[i].nodeId.identifier.string.data,
            request.nodesToRead[i].nodeId.namespaceIndex);
    }

    UA_ReadResponse response = UA_Client_Service_read(client, request);

    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, &quot;Status: %i&quot;,
        response.responseHeader.serviceResult);
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, &quot;Responses: %li&quot;, response.resultsSize);
}

The result value is UA_STATUSCODE_GOOD but the number of responses is 0. It works fine when fetching the values one after the other like this:

void readNodeAtIndex(int index)
{
    if (index &gt;= nodeCount)
    {
        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, &quot;Index out of Range&quot;);
        return;
    }

    UA_Variant variant;
    UA_Variant_init(&amp;variant);

    const UA_NodeId nodeId = nodesToRead[index];

    UA_StatusCode retval = UA_Client_readValueAttribute(client, nodeId, &amp;variant);

    if (retval == UA_STATUSCODE_GOOD &amp;&amp; UA_Variant_hasScalarType(&amp;variant,
        &amp;UA_TYPES[UA_TYPES_DOUBLE]))
    {
        UA_Double value = *(UA_Double*)variant.data;

        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, &quot;Double-Value: %f&quot;, value);
    }
    else if (retval == UA_STATUSCODE_GOOD &amp;&amp; UA_Variant_hasScalarType(&amp;variant,
        &amp;UA_TYPES[UA_TYPES_BOOLEAN]))
    {
        UA_Boolean value = *(UA_Boolean*)variant.data;

        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, &quot;Boolean-Value: %i&quot;, value);
    }
    

    UA_Variant_clear(&amp;variant);
}

The opc/ua server is setup like this:

server = Server()
space_url = &quot;opc.tcp://localhost:61032&quot;
server.set_endpoint(space_url)
server.set_security_policy([ua.SecurityPolicyType.NoSecurity])
node = server.get_objects_node()

答案1

得分: 1

你需要设置 nodesToReadSize:

UA_ReadRequest request;
UA_ReadRequest_init(&request);
UA_ReadValueId ids[nodeCount];

for (int i = 0; i < nodeCount; i++)
{
    UA_ReadValueId_init(&ids[i]);
    ids[i].attributeId = UA_ATTRIBUTEID_VALUE;
    ids[i].nodeId = nodesToRead[i];
}
request.nodesToReadSize = nodeCount;
request.nodesToRead = ids;
英文:

You need to set nodesToReadSize:

    UA_ReadRequest request;
    UA_ReadRequest_init(&amp;request);
    UA_ReadValueId ids[nodeCount];
    
    for (int i = 0; i &lt; nodeCount; i++)
    {
        UA_ReadValueId_init(&amp;ids[i]);
        ids[i].attributeId = UA_ATTRIBUTEID_VALUE;
        ids[i].nodeId = nodesToRead[i];
    }
    request.nodesToReadSize = nodeCount;
    request.nodesToRead = ids;

huangapple
  • 本文由 发表于 2023年6月1日 21:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382245.html
匿名

发表评论

匿名网友

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

确定