Converting UaDataValue to string

Questions regarding the use of the C++ SDK for Server or Client development or integration into customer products ...

Moderator: uasdkcpp

Post Reply
RafaelCrespi
Hero Member
Hero Member
Posts: 26
Joined: 22 Jun 2017, 09:41

Converting UaDataValue to string

Post by RafaelCrespi »

Hi,

I want to printf the value of a BaseDataVariableType node so I am implementing the next code on the Server's side:

Code: Select all

...
UaDataValue dataValue = p_BaseDataVariable->value(NULL);
printf("\nNode content: %s \n",UaVariant(dataValue.value(),OpcUa_False).toString().toUtf8()));
...
I get an error because it expects UaVariant(OpcUa_Variant*) and dataValue.value() returns 'const OpcUa_Variant*'

I am shure it has a very simply solution but I don't see it.

Thank you in advance,

Rafa Crespí

mantela
Hero Member
Hero Member
Posts: 21
Joined: 28 Mar 2017, 07:20

Re: Converting UaDataValue to string

Post by mantela »

Hi,

I think you need the same function as it is used in the read method which is implemented in the Client tutorial. This is the code I use for converting UaDataValue to String (the variable values is from type UaDataValues, that's why i use values):

qDebug("Read succeeded for node %s with value %s", UaNodeId(readValues.NodeId).toXmlString().toUtf8(),
UaVariant(values.Value).toString().toUtf8());

Andre

RafaelCrespi
Hero Member
Hero Member
Posts: 26
Joined: 22 Jun 2017, 09:41

Re: Converting UaDataValue to string

Post by RafaelCrespi »

Hi Andre,

I have tried that and it doesn't work. The difference is that values.Value returns OpcUa_Variant type, while dataValue.value() returns const OpcUa_Variant* type, so I think that's the main problem.

Thank you for reply

Rafa

RafaelCrespi
Hero Member
Hero Member
Posts: 26
Joined: 22 Jun 2017, 09:41

Re: Converting UaDataValue to string

Post by RafaelCrespi »

Hi everyone,

I have fixed it converting the 'const OpcUa_Variant* ' to ' OpcUa_Variant* ' with the next code:

Code: Select all

printf("\nNode content: %s \n",UaVariant(const_cast<OpcUa_Variant*>(dataValue.value())).toString().toUtf8());
I don't know if it is the smartest solution, but it works.

Rafa Crespí

User avatar
Support Team
Hero Member
Hero Member
Posts: 3068
Joined: 18 Mar 2011, 15:09

Re: Converting UaDataValue to string

Post by Support Team »

You find the sample code in the client tutorial e.g. client_cpp_sdk.cpp method read()

Code: Select all

    UaNodeId node
    UaDataValues values;
...
    UaVariant tempValue = values[i].Value;
    if (tempValue.type() == OpcUaType_ExtensionObject)
    {
        printExtensionObjects(tempValue, UaString("Variable %1").arg(node.toString()));
    }
    else
    {
        printf("  Variable %s value = %s\n", node.toString().toUtf8(), tempValue.toString().toUtf8());
    }
Best regards
Unified Automation Support Team

RafaelCrespi
Hero Member
Hero Member
Posts: 26
Joined: 22 Jun 2017, 09:41

Re: Converting UaDataValue to string

Post by RafaelCrespi »

Hi,

I have tried that way but it doesn't works. The difference is that you are using UaDataValues, and values.Value() returns an OpcUa_Variant. In my case, I am using UaDataValue, and value.value() returns OpcUa_Variant*.


Thanks,


Rafa

Post Reply