Open62541 - read simple UA_String attribute

2,202 views
Skip to first unread message

levon....@g-mail.fr

unread,
Oct 4, 2017, 10:55:15 AM10/4/17
to open62541
Hello !

I'm tying to create a OPC UA Client using open62541  implementation of OPC UA with Qt5.

As I am a beginner I have trouble to read simple String on the server and cast it to QString so i can display it ...

I have created a custom function  'readStringValue()'  :

UA_StatusCode MyUaClient::readStringValue(UA_UInt16 nsIndex, QString nodeId)
{
               
UA_StatusCode retval;
 
UA_NodeId node = UA_NODEID_STRING_ALLOC(nsIndex, nodeId.toLatin1().data()); UA_Variant value; // UA_Variant_init(&value);
UA_Variant *val = UA_Variant_new(); UA_StatusCode retval; retval = UA_Client_readValueAttribute(client,node, val);
if(retval == UA_STATUSCODE_GOOD && val->type == &UA_TYPES[UA_TYPES_STRING]) { value = *(UA_Variant*)val->data;
printf("the new value is: %i\n", value); } UA_Variant_delete(val); return retval; }


On the server my "PLC_IO_String_1" value is 'test' and type is String

But when i call my function 'readStringValue' : readStringValue(1,"PLC_IO_String_1");

it returns : the new value is : 4

So i think i have data type conversion probleme.

Does anyone know where the problem comes from and how to fix this please ?

Thank you in advance

LA

Stefan Profanter

unread,
Oct 4, 2017, 11:12:45 AM10/4/17
to open62541
Hi,
by quickly looking at your code I think there are 3 problems:

1. Change value to be of type String:
UA_String value;

2. The cast should be a string (since you are checking it in the if before):
value = *(UA_String*)val->data;

3. Your printf uses '%i' as the format which cannot be used for strings. It needs to be %s in general. Since you try to output a UA_String, you need to explicitly give the length of the string you want to print. E.g.
printf("New value: %.*s", value.lenght, value.data);

Also note that if you call UA_Variant_delete on val, it may also delete the content of value. Thus it's better to use UA_String_copy for point 2 or create a QtString in that line.

Hope this helps?
Stefan

basyskom.fran...@gmail.com

unread,
Dec 6, 2017, 5:38:19 AM12/6/17
to open62541
I would like to encourage you to have a look at the QtOpcua module.

It will offer you a default translation of OPC-UA types into Qt types.
It also implements an asynchronous API which prevents you from blocking your Qt event loop.

Cheers,
Frank


Am Mittwoch, 4. Oktober 2017 16:55:15 UTC+2 schrieb LEVON AGAYEV:

LEVON AGAYEV

unread,
Dec 12, 2017, 3:53:15 AM12/12/17
to open62541
Hello Frank,

Is there a documentation on how can i use QtOpcua module ?
Do i have to compile your project ? or is there a .h/.cpp to include just like open62541 ?

It is failing to clone your project from http://code.qt.io/cgit/qt/qtopcua.git/tree/ do you know why ?

Thank you very much,

LA

basyskom.fran...@gmail.com

unread,
Dec 18, 2017, 8:36:41 AM12/18/17
to open62541
Hello Levon,

QtOpcua will be part of the next official Qt release (5.11) as a Tech-Preview (aka. a module which doesn't provide API and ABI stability yet).
Given the current release plan (https://wiki.qt.io/Qt_5.11_Release) alpha builds would be available end of February.

Till then you have to compile the "dev" branch of Qt yourself.

Which platform are you on?

Kind Regards,
Frank
Reply all
Reply to author
Forward
0 new messages