11:42, EET
November 11, 2024
I followed the documentation on how to read custom datastructures, but I’m still getting the following error
class com.prosysopc.ua.types.opcua.client.BaseDataVariableTypeImpl cannot be cast to class …
I have generated the Java code using the included codegen tool, I registered the ClientInformationModel.MODEL. But it’s still giving me that error when I try to cast UaNode to my DataType. As far as I can see I followed the steps given in the documentation.
13:45, EET
Moderators
February 11, 2020
Hello,
In OPC UA, Variable Nodes contain values in their Value Attribute. In the SDK, such Nodes are represented with classes implementing the UaVariable interface, such as BaseDataVariableTypeImpl. But this Node itself is not the value and thus it can’t be cast to any class represeting a DataType.
You need to call getValue() for the UaVariable to access its Value as a DataValue. Then you can call getValue for the DataValue to access its values as a Variant. Finally you can call getValue for the Variant to access its value as an object and cast it to proper class. Naturally, you should first confirm that the DataValue isn’t null with com.prosysopc.ua.stack.builtintypes.DataValue.isNull(DataValue) before calling getValue for it or the Variant.
13:53, EET
November 11, 2024
Hi Matti,
I was under the impression that the cast should work due to this being the documentation
“`
// 1. Register the generated classes in your UaClient object by
// using the ClientInformationModel class that is generated in the client package.
client.registerModel(example.packagename.client.ClientInformationModel.MODEL);
// 2. Get a node from the server using an AddressSpace object.
// Give the nodeId of the instance as a parameter.
// Cast the return value to the corresponding generated class
ValveObjectType sampleValve =
(ValveObjectType) client.getAddressSpace().getNode(nodeId);
// 3. Use the instance.
// e.g. get the cached value of the PowerInput Property
sampleValve.getPowerInput();
I tried what was suggested using the following code:
“`
private static void readId(UaClient client) throws Exception {
client.registerModel(ClientInformationModel.MODEL);
var value = client.readValue(NodeId.parseNodeId(“ns=3;s=\”Compressor\”.\”ID\””)).getValue().getValue();
System.out.println( value );
UDT_IdentificationData id = (UDT_IdentificationData) value;
System.out.println(“Why ” + id.getSerialNumber());
}
“`
I can see that the value inside of value is correct but I still get:
class com.prosysopc.ua.typedictionary.DynamicStructure cannot be cast to class org.UaResato.datatypes.UDT_IdentificationData (com.prosysopc.ua.typedictionary.DynamicStructure and org.UaResato.datatypes.UDT_IdentificationData are in unnamed module of loader ‘app’)
when trying to cast
15:18, EET
Moderators
February 11, 2020
Hello,
In generated code, calling getX, such as getPowerInput in the tutorial, should provide the Value contained by the Variable Node (and getXNode would return the Node containing the Value). Where exactly is the ClassCastException happening and what is the exact class the cast is failing to?
If you have generated code from multiple information models, make sure you have registered all of them. Depending on settings of CodeGen, the classes could all be named ClientInformationModel, but they would be in different packages.
16:37, EET
November 11, 2024
The ClassCastException is happening in the following code when I try to cast the Object to the UDT_IdentificationData
“`
private static void readId(UaClient client) throws Exception {
client.registerModel(ClientInformationModel.MODEL);
DataValue idDataValue = client.readValue(NodeId.parseNodeId(“ns=3;s=\”Compressor\”.\”ID\””));
Variant idVariant = idDataValue.getValue();
Object id = idVariant.getValue();
UDT_IdentificationData idStructure = (UDT_IdentificationData) id;
System.out.println(idStructure.getSerialNumber());
}
“`
Also how do I create codeblocks on this forum, I’m used to using “` but that isn’t working
11:26, EET
April 3, 2012
Hi,
Html tags ‘pre’ and ‘code’ can be used for a “code block”, but sometimes they produce unwanted behavior (and no indentation). Though sometimes XML is problematic regardless if you use those or not. Maybe some day we get something better..
Regarding the issue. Please double and triplecheck that the ClientInformationModel import is correct. There is already one within the SDK, since we also use the Codegen for the core information model, i.e. ensure it is NOT ‘com.prosysopc.ua.types.opcua.client.ClientInformationModel’, but instead for the package name you made in the Codegen configuration xml. For that reason I recommend adding a prefix in the namespace mapping configuration, e.g.
<namespaceMappings>
…
<namespaceMapping>
<uri>your_model_uri_here<uri>
<packageName>your.package.name.here</packageName>
<prefix>XXX</prefix>
</namespaceMapping>
…
…
Would result in ‘your.package.name.here.client.ClientInformationModel’ getting prefix i.e. be named ‘your.package.name.here.client.XXXClientInformationModel’ to avoid confusion. Also it is useful if you have more than a single generated model, since in java you can import a name only once (others would need to use fqn version).
Additionally, if you have more than one generated model, please ensure you call registerModel for all of them.
You cannot get a “class com.prosysopc.ua.types.opcua.client.BaseDataVariableTypeImpl cannot be cast to class” from your code line “UDT_IdentificationData id = (UDT_IdentificationData) value;”, the only classes it could be is DynamicStructure, in case the registration was not done correctly (and SDK was able to read metadata from the server to know the structure types encoding) or ExtensionObject if it cannot even be decoded (though this is rare nowadays since SDK can mostly just read the metadata during connect, regardless of Codegen, it just basically gives a nicer API to work with). Alternatively it could be some other class (but in that case you would have used the wrong NodeId for the Read).
You can also try checking can you see the Structure value using https://prosysopc.com/products/opc-ua-browser/, if yes, then most likely it is just some mistake in your code. However if not, then it is possible there is some inter-operability problem or something else. Note that Browser doesn’t use Codegen nor the models and just relies on the meta information from the server’s addressspace for the encodings of the custom Structures.
P.S.
In some cases if you have put the Codegen outputted resource files on the classpath, SDK can automatically register the generated models.
P.S.2
Mentioning just in case, the com.prosysopc.ua.types.opcua.client.ClientInformationModel doesn’t need to be registerModel:ed, that happens automatically. You only need to do that for your own models (once per model/namespace), unless the above automatic registration works.
Most Users Ever Online: 1919
Currently Online:
54 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 88
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 734
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1523
Posts: 6449
Newest Members:
christamcdowall, redaahern07571, nigelbdhmp, travistimmons, AnnelCib, dalenegettinger, howardkennerley, Thomassnism, biancacraft16, edgardo3518Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1026, Jimmy Ni: 26, Matti Siponen: 346, Lusetti: 0
Administrators: admin: 1