英文:
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(&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);
}
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 >= 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);
}
The opc/ua server is setup like this:
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()
答案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(&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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论