Hello,
I want to read nodes from my code in the OPC UA server. The OPC UA server was developed with the C++ SDK from Unified Automation.
Is there an easy way to read, write or subscribe nodes in the server.
Thank you
How do I read or subscribe for data internally in the server
Moderator: uasdkcpp
-
- Hero Member
- Posts: 20
- Joined: 07 Feb 2013, 11:01
- Support Team
- Hero Member
- Posts: 3072
- Joined: 18 Mar 2011, 15:09
Re:How do I read or subscribe for data internally in the ser
Hi,
There are two ways to do this.
(1) Simple but limited option
If the node you want to access was created in the toolkit layer and is present in memory, you can get the node and access the attributes directly. But this option does not work for all nodes depending on the selected implementation for the node.
(2) The option that always works.
There is an internal interface ServerManager provided by the SDK. This interface defines methods to browse, read, write, call a method or to create monitored items for data or event monitoring. This option works always independent of the way the data acquisition is implemented in the server.
The sample code for Historical Access in \"Server Getting Started\" -> \"Lesson07\" provides an example for creating an internal session and to create internal data monitored items:
Best Regards,
Unified Automation Support Team
There are two ways to do this.
(1) Simple but limited option
If the node you want to access was created in the toolkit layer and is present in memory, you can get the node and access the attributes directly. But this option does not work for all nodes depending on the selected implementation for the node.
Code: Select all
UaNodeId nodeId(OpcUaId_ServerState);
// Get root NodeManager
NodeManagerRoot* pNMRoot = (NodeManagerRoot*)m_pServerManager->getNodeManagerRoot();
// Try to find the node
UaNode* pNode = pNMRoot->getNode(nodeId);
if ( pNode )
{
// Get any attribute value from the node
// Check for node class before casting to the interface like UaVariable for specific attributes
OpcUa_NodeClass nodeClass = pNode->nodeClass();
// Do not forget to release the reference to the node created by getNode()
pNode->releaseReference();
}
(2) The option that always works.
There is an internal interface ServerManager provided by the SDK. This interface defines methods to browse, read, write, call a method or to create monitored items for data or event monitoring. This option works always independent of the way the data acquisition is implemented in the server.
The sample code for Historical Access in \"Server Getting Started\" -> \"Lesson07\" provides an example for creating an internal session and to create internal data monitored items:
Code: Select all
UaStatus status;
// Create internal session
m_pSession = m_pServerManager->getServerConfig()->createSession(0, UaNodeId());
if ( m_pSession )
{
status = m_pSession->open(\"InternalHistorizing\", UaByteString(), OpcUa_UInt32_Max).statusCode();
if ( status.isGood() )
{
UaStringArray localeIDs;
localeIDs.create(1);
UaString sTemp(\"en\");
sTemp.copyTo(&localeIDs[0]);
status = m_pSession->activate(0, NULL, localeIDs).statusCode();
}
}
// Create internal monitored items for historizing
if ( status.isGood() )
{
DataMonitoredItemSpecArray dataMonitoredItems;
OpcUa_UInt32 i = 0;
std::map<UaNodeId, HistorizedVariable*>::iterator it;
dataMonitoredItems.create(m_mapVariables.size());
// Collect information for the monitored items to create
for(it=m_mapVariables.begin(); it!=m_mapVariables.end(); it++)
{
HistorizedVariable* pVariable = it->second;
pVariable->m_pVariable->nodeId().copyTo(&dataMonitoredItems[i].m_itemToMonitor.NodeId);
dataMonitoredItems[i].m_itemToMonitor.AttributeId = OpcUa_Attributes_Value;
dataMonitoredItems[i].m_requestedSamplingInterval = HISTORYMANAGERBA_SAMPLING_INTERVAL;
dataMonitoredItems[i].m_pDataCallback = pVariable;
i++;
}
// Create the monitored items
status = m_pServerManager->createDataMonitoredItems(m_pSession, dataMonitoredItems);
if ( status.isGood() )
{
i = 0;
// Store the create results
for(it=m_mapVariables.begin(); it!=m_mapVariables.end(); it++)
{
HistorizedVariable* pVariable = it->second;
if ( dataMonitoredItems[i].m_createResult.isGood() )
{
pVariable->m_isValid = OpcUa_True;
pVariable->m_monitoredItemId = dataMonitoredItems[i].m_monitoredItemId;
if ( dataMonitoredItems[i].m_isInitialValueProvided != OpcUa_False )
{
pVariable->dataChange(dataMonitoredItems[i].m_initialValue);
}
}
i++;
}
}
}
Unified Automation Support Team
Last edited by Support Team on 07 Feb 2013, 10:48, edited 1 time in total.
Best regards
Unified Automation Support Team
Unified Automation Support Team
-
- Hero Member
- Posts: 20
- Joined: 07 Feb 2013, 11:01
Re:How do I read or subscribe for data internally in the server
Thank you very much for your excellent support.
This was exactly what I was looking for.
This was exactly what I was looking for.