0

I have been trying to do a simple OPC UA client server application using the open62541 stack. I can access the value from the open62541 implemented server. But i need to know, how a array value from the server can be interpreted/retrieved by the open62541 client?

Ex: This is how i do for single value-

 UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "value"), &value);
    if(status == UA_STATUSCODE_GOOD &&
       UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32])) {
        printf("value is: %i\n", *(UA_Int32*)value.data);
    }
TechDev
  • 41
  • 2
  • 4

1 Answers1

1

Here is an example how to read the namespace array. It works in the same way as any other array value:

UA_ReadRequest request;
UA_ReadRequest_init(&request);
UA_ReadValueId id;
UA_ReadValueId_init(&id);
id.attributeId = UA_ATTRIBUTEID_VALUE;
id.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACEARRAY);
request.nodesToRead = &id;
request.nodesToReadSize = 1;

UA_ReadResponse response = UA_Client_Service_read(client, request);

UA_StatusCode retval = UA_STATUSCODE_GOOD;
if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
    retval = response.responseHeader.serviceResult;
else if(response.resultsSize != 1 || !response.results[0].hasValue)
    retval = UA_STATUSCODE_BADNODEATTRIBUTESINVALID;
else if(response.results[0].value.type != &UA_TYPES[UA_TYPES_STRING])
    retval = UA_STATUSCODE_BADTYPEMISMATCH;

if(retval != UA_STATUSCODE_GOOD) {
    UA_ReadResponse_deleteMembers(&response);
    return retval;
}

retval = UA_STATUSCODE_BADNOTFOUND;
UA_String *ns = (UA_String *)response.results[0].value.data;
for(size_t i = 0; i < response.results[0].value.arrayLength; ++i){
    printf("The NS is %*.s", (int)ns[i].length, ns[i].data);
}

UA_ReadResponse_deleteMembers(&response);

The important thing is that response.results[0].value.data holds the array, and response.results[0].value.arrayLength the length of the array itself.

Stefan Profanter
  • 6,458
  • 6
  • 41
  • 73