Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
Enums or VariableIdentifiers.MultiStateDiscreteType_EnumStrings do not handle negative values.
September 17, 2024
16:58, EEST
Avatar
mithun
New Member
Members
Forum Posts: 1
Member Since:
September 11, 2024
sp_UserOfflineSmall Offline

In our application, we create a OPCUa server using com.prosysopc.ua.server. Enums in our application are converted to OPCUA enums using the java sdk as follows:

“`
NodeBuilderConfiguration conf = new NodeBuilderConfiguration();
conf.addOptional(VariableIdentifiers.MultiStateDiscreteType_EnumStrings);
conf.addOptional(DataItemType.DEFINITION);

MultiStateDiscreteType node = uaNodeManager.createNodeBuilder(MultiStateDiscreteType.class, conf).setName(addName).build();
node.setValue(new DataValue(new Variant(myEnum.getOrdinal(), StatusCode.GOOD));
UaNode uaNode = uaNodeManager.addNodeAndReference(uaFolder, node, ReferenceTypeIdentifiers.Organizes);
“`

When ENUM points are added in our application with possible negative values/cardinals, it causes an error when trying to set the enum to negative values in the UABrowser/UAExpert as it sees them as UInt32.
See images:
https://gcdnb.pbrd.co/images/5EWq3ZREBXkT.png?o=1
https://gcdnb.pbrd.co/images/OcZSaY4XrSKw.png?o=1

My question is:
1. How do I create a node with negative enum values (Int and not UInt) in the SDK?

Thank you so much!

September 18, 2024
10:13, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1016
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

There is 2 parts to this answer.

1. “.ordinal”, Codegen types and general Enumerations in OPC UA

There is no method ‘getOrdinal’, did you mean .ordinal as in literally https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html#ordinal– ?? As the core Java’s javadoc reads “… Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap. …”, you should NOT use this when using the SDK. Instead you can give com.prosysopc.ua.stack.builtintypes.Enumeration implementations directly to Variant (as scalar or anydimensional array), Variant will then convert that as an equivalent (scalar or same dimensional array) of Integer.

Also, the .ordinal would also give the wrong value for Enumerations that are not 0-based (and each constant is +1), since it is just the position of the constant in the .java file (this is why it shouldn’t be used). The Codegen of SDK will generate the proper Value of the Enumeration to a field (enumerations are just normal classes that have fixed number of instances). This value can also be obtained via com.prosysopc.ua.stack.builtintypes.Enumeration.getValue().

2. MultiStateDiscreteType

https://reference.opcfoundation.org/Core/Part8/v105/docs/5.3.3.3
“DataType UInteger”
+
“HasProperty Variable EnumStrings LocalizedText[] PropertyType Mandatory”

Thus firstly there shouldn’t be need to mark it as optional-instatiated. But since this TypeDefinition says UInteger (base for all unsigned integer types), it is impossible to have negative values here (but please read until the end).

And then the text does say “EnumStrings is a string lookup table corresponding to sequential numeric values (0, 1, 2, etc.)”
Thus the value within the node must directly index-reference the EnumStrings table of _that node_. Therefore you cannot have negative values. I think this type originates from a time when OPC UA only had “0-based +1 with each constant” type of Enumerations.

Where you looking instead for https://reference.opcfoundation.org/Core/Part8/v105/docs/5.3.3.4 MultiStateValueDiscreteType (similar name), it’s DataType is Number and has “EnumValues EnumValueType[]”, while looking the same this allows “This represents enumerations with integers that are not zero-based or have gaps (e.g. 1, 2, 4, 8, 16).” (thus negative numbers should be also possible since it EnumValueType uses Int64 to define the value).

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Heikki Tahvanainen: 402

hbrackel: 144

rocket science: 86

pramanj: 86

Francesco Zambon: 83

Ibrahim: 78

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

Member Stats:

Guest Posters: 0

Members: 732

Moderators: 7

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1508

Posts: 6396

Newest Members:

elainesever, powhephenry, mamiecaldwell5, Lamasom, scsneed, berrybulcock, icerdraizomma, athenasummy5, vtaletbhcx, HaroldaDob

Moderators: Jouni Aro: 1019, Pyry: 1, Petri: 0, Bjarne Boström: 1016, Jimmy Ni: 26, Matti Siponen: 340, Lusetti: 0

Administrators: admin: 1