User Defined Datatype

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

Moderator: uasdkcpp

Post Reply
Utkrist
Hero Member
Hero Member
Posts: 22
Joined: 15 May 2023, 09:50

User Defined Datatype

Post by Utkrist »

First of all, I just want to clarify that I am very new to OPC-UA and to programming in C++ for that matter. I have been trying to create a structure of an Int and a Double (known at compile time) and have been unable to do so.
I looked at the sample code at nmbuildingautomation.cpp, but could not understand much. This question was also answered in 2011 where it was mentioned that in the upcoming updates, newer ways to implement a structure would be introduced. I have also looked at the UaStructureDefinition and UaGenericDataType, but to no success. I would be grateful if someone could outline the steps or provide a sample code to do this.

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

Re: User Defined Datatype

Post by Support Team »

Hello,

If the structure datatype is known at compile time, then you can use the UaModeler to create and generate the code. This is the recommended way.
https://documentation.unified-automation.com/uamodeler/1.6.9/html/howprojwin_sec.html

If you would to create the Structure DataType in the code. Please see the below example code

Structure DataType:

Code: Select all

#define Demo_SimpleStruct               543225
#define Demo_SimpleStruct_DefaultBinary 543226
#define Demo_SimpleStruct_DefaultXml    543227

    UaStructureDefinition simpleStructure;
    simpleStructure.setName("simpleStructure");
    simpleStructure.setDataTypeId(UaNodeId(Demo_SimpleStruct, getNameSpaceIndex()));
    simpleStructure.setBinaryEncodingId(UaNodeId(Demo_SimpleStruct_DefaultBinary, getNameSpaceIndex()));
    simpleStructure.setXmlEncodingId(UaNodeId(Demo_SimpleStruct_DefaultXml, getNameSpaceIndex()));
    UaStructureField simpleField;
    simpleField.setName("IntField");
    simpleField.setDataTypeId(OpcUaId_Int32);
    simpleField.setArrayType(UaStructureField::ArrayType_Scalar);
    simpleStructure.addChild(simpleField);
    simpleField.setName("DoubleField");
    simpleField.setDataTypeId(OpcUaId_Double);
    simpleField.setArrayType(UaStructureField::ArrayType_Scalar);
    simpleStructure.addChild(simpleField);
    addStructuredType(simpleStructure);
Structure Variable with initial values:

Code: Select all

        UaVariant vFieldVal;
        UaGenericStructureValue simpleStructGenVal(structureDefinition(UaNodeId(Demo_SimpleStruct, getNameSpaceIndex())));
        vFieldVal.setInt32(123);
        simpleStructGenVal.setField("IntField", vFieldVal, OpcUa_False); // IntField
        vFieldVal.setDouble(456.7);
        simpleStructGenVal.setField("DoubleField", vFieldVal, OpcUa_False); // DoubleField

        UaVariant vInitialVal;
        simpleStructGenVal.toVariant(vInitialVal);

        OpcUa::BaseDataVariableType* pStructVariable = new OpcUa::BaseDataVariableType(
            UaNodeId("SimpleStructure", getTypeNamespace()),
            "SimpleStructure",
            getNameSpaceIndex(),
            vInitialVal,
            OpcUa_AccessLevels_CurrentReadOrWrite,
            this);
        UaStatus addStructRes = addNodeAndReference(OpcUaId_ObjectsFolder, pStructVariable, OpcUaId_Organizes);
        if (addStructRes.isGood())
        {
            pStructVariable->setDataType(UaNodeId(Demo_SimpleStruct, getNameSpaceIndex()));
        }
Best regards
Unified Automation Support Team

Post Reply