an object can't be casted

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

Moderator: uasdkcpp

Post Reply
acorbin
Full Member
Full Member
Posts: 6
Joined: 15 Jan 2016, 12:04

an object can't be casted

Post by acorbin »

Hello,
My XML model has got a class named OffNormalAlarmExtType derived from OffNormalAlarmType (a base type from OPC UA) and an object of this type.
My server knows the OffNormalAlarmExtType (header and cpp are present).

I'd like to process a method call but in the beginCall() method of my nodemanager, I can't cast the object like that (my nodeid is a constant for the example) :
UaNode * pNode2 = finfNode(UaNodeId(5006, getNameSpaceIndex()));
OffNormalAlarmExtType* uaVariable2 = (OffNormalAlarmExtType *) (pNode2);
uaVariable2->setActiveState(OpcUa_True); //this method call doens't work !

Note :
If I destroy and recreate the OffNormalAlarmExtType class and the associated object, the code shown above works...

Does it mean something for you ?
thank you in advance for you answer.

regards.

A. CORBIN

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

Re: an object can't be casted

Post by Support Team »

Hi,

The default node creation during XML import is using generic classes for object, variable and other node classes.

If you generate your implementation classes e.g. for OffNormalAlarmExtType with UaModeler, the generated code contains factories for XML import. These factories make sure that the OffNormalAlarmExtType class is instantiated during XML import instead of the generic classes.

Did you use UaModler to generate the class for OffNormalAlarmExtType?

Best Regards,
Unified Automation Support Team

acorbin
Full Member
Full Member
Posts: 6
Joined: 15 Jan 2016, 12:04

Re: an object can't be casted

Post by acorbin »

Hello,

thank you for your answer.

Your question in the previous post was this one :
"Did you use UaModler to generate the class for OffNormalAlarmExtType?"

My answer is this one :
Yes we used UaModeler to generate the OffNormalAlarmExtType in the XML model.
But we don't use the code generated by UaModeler for our server.
we use our own code (OffNormalAlarmExtType.h and OffNormalAlarmExtType.cpp).

Maybe I should study the code generated by UaModeler to find out if there are some omissions or mistakes in our code...

Do you have an idea of some possible omissions or mistakes that a simple user like me can do ?

Or would you mind telling me the characteristics of the code generated by UaModeler ?

to recall , destruction and re-creation of the OffNormalAlarmExtType class nodes enables the server operation.

I hope I was clear as possible in this post.

Thank you in advance.
Regards.

A. CORBIN

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

Re: an object can't be casted

Post by Support Team »

Hi,

If you import a UANodeSet XML file, the default importer creates all nodes with generic classes for the different node types (NodeClass) like Object, Variable or Method.

Therefore "somebody" must instantiate the right classes during import.

The UaModeler generated code creates a factory class that creates the nodes from the right implementation class during XML import. You should either use the generated code (you can use the generated implementation class for your own code) or you should have a look at the factory class and the special constructor created for the XML import in the OffNormalAlarmExtType class.

You can create your own factory class by implementing XmlUaNodeFactoryNamespace.

See the following example as base:
http://documentation.unified-automation ... moServer_3

The factory must be added to an instance of XmlUaNodeFactoryManager. You must create an instance of XmlUaNodeFactoryManager and you must pass this factory in to the constructor of the parser class.

The last parameter in the following line from above example is NULL. You must pass the factory in instead:

Code: Select all

UaNodeSetXmlParserUaNode* pXmlParser = new UaNodeSetXmlParserUaNode(sNodesetFile, pNodeManagerCreator, pBaseNodeFactory, NULL);

// Replace with
XmlUaNodeFactoryManager* pFactoryManager = new XmlUaNodeFactoryManager;
MyXmlUaNodeFactoryNamespace* pMyNamespaceFactory = new XmlUaNodeFactoryManager;
pFactoryManager->addNamespace(pMyNamespaceFactory);
UaNodeSetXmlParserUaNode* pXmlParser = new UaNodeSetXmlParserUaNode(sNodesetFile, pNodeManagerCreator, pBaseNodeFactory, pFactoryManager);
Best Regards,
Unified Automation Support Team

Post Reply