How to create custom enumerations?

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

Moderator: uasdknet

Post Reply
kPiech
Jr. Member
Jr. Member
Posts: 1
Joined: 06 Jun 2014, 09:13

How to create custom enumerations?

Post by kPiech »

Hi,
I'm struggling to create a custom enumeration. I know I've to create a new DataType which should be referenced to the attribute. But all my trials are not working. The created DataType-Node seems to be working as it is showing up in the DataTypes->Enumeration section along with all the correct enum strings.

Here is the code I tried (only for testing):

Code: Select all

// Create a Folder
            CreateObjectSettings folderSettings = new CreateObjectSettings()
            {
                ParentNodeId = ObjectIds.ObjectsFolder,
                ReferenceTypeId = ReferenceTypeIds.Organizes,
                RequestedNodeId = new NodeId("Test", InstanceNamespaceIndex),
                BrowseName = new QualifiedName("Test", InstanceNamespaceIndex),
                TypeDefinitionId = ObjectTypeIds.FolderType
            };
            CreateObject(Server.DefaultRequestContext, folderSettings);

            //Create an object
            CreateObjectSettings objectSettings = new CreateObjectSettings()
            {
                ParentNodeId = new NodeId("Test", InstanceNamespaceIndex),
                ReferenceTypeId = ReferenceTypeIds.Organizes,
                RequestedNodeId = new NodeId("TestObject", InstanceNamespaceIndex),
                BrowseName = new QualifiedName("TestObject", InstanceNamespaceIndex),
                DisplayName = new LocalizedText("TestObject"),
                TypeDefinitionId = ObjectTypeIds.BaseObjectType
            };
            CreateObject(Server.DefaultRequestContext, objectSettings);

            //Create a DataType for custom enumeration
            CreateDataTypeSettings dataTypeSettings = new CreateDataTypeSettings()
            {
                ReferenceTypeId = ReferenceTypeIds.HasSubtype,
                DisplayName = new LocalizedText("TestEnumeration"),
                BrowseName = new QualifiedName("TestEnumeration", InstanceNamespaceIndex),
                RequestedNodeId = new NodeId("TestEnumeration", InstanceNamespaceIndex),
                SuperTypeId = DataTypeIds.Enumeration
            };
            CreateDataTypeNode(Server.DefaultRequestContext, dataTypeSettings);

            //Create the enum values
            ExtensionObjectCollection enums = new ExtensionObjectCollection();
            EnumValueType enumValueType = new EnumValueType()
            {
                Value = 0,
                DisplayName = "EnumString1"
            };
            enums.Add(new ExtensionObject(enumValueType));

            enumValueType = new EnumValueType()
            {
                Value = 1,
                DisplayName = "EnumString2"
            };
            enums.Add(new ExtensionObject(enumValueType));

            //Create a variant for enumString variable
            Variant variant = new Variant(enums);

            //Create an EnumStrings variable
            CreateVariableSettings enumSettings = new CreateVariableSettings()
            {
                DataType = DataTypeIds.EnumValueType,
                AccessLevel = AccessLevels.CurrentReadOrWrite,
                ReferenceTypeId = ReferenceTypeIds.HasOrderedComponent,
                DisplayName = new LocalizedText("EnumStrings"),
                BrowseName = new QualifiedName("TestEnumeration.EnumStrings", InstanceNamespaceIndex),
                RequestedNodeId = new NodeId("TestEnumeration.EnumStrings", InstanceNamespaceIndex),
                ParentNodeId = new NodeId("TestEnumeration", InstanceNamespaceIndex),
                TypeDefinitionId = VariableTypeIds.PropertyType,
                ValueRank = 1,
                Value = variant
            };
            CreateVariable(Server.DefaultRequestContext, enumSettings);

            //Create a default value
            Variant defaultValue = new Variant((uint)0);

            //Create a variable for the TestObject
            CreateVariableSettings variableSettings = new CreateVariableSettings()
            {
                AccessLevel = AccessLevels.CurrentReadOrWrite,
                ReferenceTypeId = ReferenceTypeIds.HasComponent,
                DisplayName = new LocalizedText("testEnumVariable"),
                BrowseName = new QualifiedName("TestObject.testEnumVariable", InstanceNamespaceIndex),
                RequestedNodeId = new NodeId("TestObject.testEnumVariable", InstanceNamespaceIndex),
                ParentNodeId = new NodeId("TestObject", InstanceNamespaceIndex),
                DataType = dataTypeSettings.RequestedNodeId,
                Value = defaultValue
            };
            CreateVariable(Server.DefaultRequestContext, variableSettings);
This code creates an object including an attribute which has a reference to the custom enumeration. When I try to change the value it creates an BadTypeMismatch error.

Did I miss something to set or to create?
Or what is the correct way to create a custom enumeration and use it within an attribute?

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

Re: How to create custom enumerations?

Post by Support Team »

Hello,

A custom enumeration has to be a subtype of the Enumeration node. This is correct in your code.
The string representation of the enum values are described by a property. There are two different kinds of properties defined of the OPC foundation to describe this.
- EnumStrings. The browse name is "EnumStrings" with namespace index 0, the DataType is LocalizedText and the ValueRank is 1. Each array element represents the string representation of its array index. E.g. the first array element is the text for 0.
- EnumValues. The browse name is "EnumValues" with namespace index 0, the DataType is EnumValueType and the ValueRank is 1. Each array element contains a name value pair for the string representation.
In your code example the BrowseName is not correct.
Enumerations are encoded as Int32. So you have to fix your default value for the variable.

Code: Select all

            //Create a default value
            Variant defaultValue = new Variant((int)0);
Best regards
Support Team

Post Reply