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
UaClient: Reading custom structured DataType without generated code
February 13, 2020
16:56, EET
Avatar
hbrackel
Member
Members
Forum Posts: 93
Member Since:
February 21, 2014
sp_UserOfflineSmall Offline

Good afternoon,

I am looking for a way for an UaClient to read variable values from a server, where the DataType is based on a custom structure DataType. Unfortunately, the namespaces of the type definitions are occasionally updated (they contain the date/month of release), so I cannot use generated code across multiple versions of that server.

Is there a way for a client to dynamically “decode” a custom structure variable value at runtime using SDK support? I remember that v4.x.x of the SDK provides improved support for complex datatypes. It would be great, if you could provide a little code snippet, demonstrating how to decode a DataValue containing a custom structure.

Many thanks,
Hans-Uwe

February 13, 2020
18:25, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 469
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

(Long post, sorry, and I think there is a lot more that should be said, but maybe this will do for now, but yes, should be documented a lot better…)

This has been improving with each release. You’ll want version 4.2.0, that would be the best (https://downloads.prosysopc.com/opcua/Prosys_OPC_UA_SDK_for_Java_4_Release_Notes.html#version-4-2-0), unless you connect to an S7 device (see other latest threas of this forum since we have problems with that currently, in case try 4.1.4). Additionally it should be mentioned that with 4.2.0 connecting might take longer (this is currently directly related to the number of custom Structures since we need to read them all), working on improvements on that for the next release.. (which should be soon).

Basically as it is, any Structure can be handled like this, i.e. “dynamically”, since we enhanced the Structure interface to contain all relevant APIs from SDK’s DynamicStructure in 4.x. Note that this example is missing all error handling etc. This is something I can give right now, making a proper full example will take some time. Let me know if you encounter any issues.

NodeId nodeId = …
UaClient client = …
Structure customStructure = (Structure ) client.readValue(nodeId).getValue().getValue();
StructureSpecification customStructureSpecification = customStructure.specification();
for (FieldSpecification fieldSpecification : customStructureSpecification.getFields()) {
// Getting values
Object fieldValue = customStructure.get(fieldSpecification);

// Setting values, the fieldSpecification contains meta-data for types
Object newValue = …;
customStructure.set(fieldSpecification, newValue);
}

i.e. meaning that the SDK is already doing it dynamically in the latest versions.

Additionally it should be mentioned that Structure has toBuilder which will return a Structure.Builder that will build instances of the same type (initializes with current values of fields). The same builder can obtained from the StructureSpecification.toStructureBuilder(). StructureSpecifications can be obtained from UaClient.getEncoderContext().getStructureSpecification(UaNodeId id) after connecting. And UaNodeId is an “upgraded” NodeId (has UaNamespace object instead of the index, and that in turn contains the namespaceuri; I’ll skip details for now, can be discussed later in some other post if needed), can be made with the static factory methods of that class (via NodeId/ExpandedNodeId + NamespaceTable from the client).

February 14, 2020
14:20, EET
Avatar
hbrackel
Member
Members
Forum Posts: 93
Member Since:
February 21, 2014
sp_UserOfflineSmall Offline

Thanks for your quick reply and the code example. I will implement this over the weekend.

If a have a subscription for a structured value, is it possible to “cache” the structure specification and reuse it or do I need to implement the above code for each individual DataValue?

Thanks,
Hans-Uwe

February 14, 2020
15:04, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 469
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

I might need clarifications on what exactly you mean by “caching” here.

StructureSpecification instances are immutable, SDK will keep track of them with the TypeDictionary and/or EncoderContext (basically EncoderContext will delegate to TypeDictionary if needed). If your value is always the same type, i.e. your MonitoredDataItemListener handler is not shared between items, then the Structure.specification() is the same java object each time.

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 267

Currently Online:
11 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 93

pramanj: 86

ibrahim: 69

kapsl: 57

gjevremovic: 49

TimK: 41

Fransua33: 39

fred: 36

Rainer Versteeg: 32

Thomas Reuther: 26

Member Stats:

Guest Posters: 0

Members: 957

Moderators: 13

Admins: 1

Forum Stats:

Groups: 3

Forums: 14

Topics: 935

Posts: 3932

Newest Members:

mattiee679022481, osvaldolennox12, brandenmallette, mellisafcm, P0001, launastanfill, Oleksandr, Matti Siponen, gselvas92, ashleighfinlay

Moderators: Jouni Aro: 834, Otso Palonen: 32, Tuomas Hiltunen: 5, janimakela: 0, Pyry: 1, Terho: 0, Petri: 0, Bjarne Boström: 469, Heikki Tahvanainen: 402, Jukka Asikainen: 1, Teppo Uimonen: 18, Markus Johansson: 11, Matti Siponen: 6

Administrators: admin: 0