UA Server SDK C++ Bundle  1.3.2.200
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
Lesson 4: Reading and Writing Data

Content:

Step 1: Implementing Read

Add the following code to header,

/* OPC UA service calls */
void read();

and source file, rsp.:

void read()
{
printf("\n\n****************************************************************\n");
printf("** Try to call read for configured node ids\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
OpcUa_UInt32 i;
OpcUa_UInt32 count;
UaStatus status;
UaReadValueIds nodesToRead;
UaDataValues values;
UaDiagnosticInfos diagnosticInfos;
ServiceSettings serviceSettings;
// Initialize IN parameter nodesToRead
count = g_VariableNodeIds.length();
nodesToRead.create(count);
for ( i=0; i<count; i++ )
{
g_VariableNodeIds[i].copyTo(&nodesToRead[i].NodeId);
nodesToRead[i].AttributeId = OpcUa_Attributes_Value;
}
/*********************************************************************
Call Read service
**********************************************************************/
status = g_pUaSession->read(
serviceSettings, // Use default settings
0, // Max age
OpcUa_TimestampsToReturn_Both, // Time stamps to return
nodesToRead, // Array of nodes to read
values, // Returns an array of values
diagnosticInfos); // Returns an array of diagnostic info
/*********************************************************************/
if ( status.isBad() )
{
printf("** Error: UaSession::read failed [ret=%s] *********\n", status.toString().toUtf8());
}
else
{
printf("** UaSession::read result **************************************\n");
for ( i=0; i<count; i++ )
{
UaNodeId node(nodesToRead[i].NodeId);
if ( OpcUa_IsGood(values[i].StatusCode) )
{
UaVariant tempValue = values[i].Value;
printf("** Variable %s value = %s\n", node.toString().toUtf8(), tempValue.toString().toUtf8());
}
else
{
printf("** Variable %s failed with error %s!\n", node.toString().toUtf8(), UaStatus(values[i].StatusCode).toString().toUtf8());
}
}
printf("****************************************************************\n");
}
}

read() calls the actual UaSession::read() using the OPC UA server Read Service in order to read one or more Attributes of one or more Nodes. The Read Service also allows reading subsets or single elements of array values and to define a valid age of values to be returned to reduce the need for device reads. UaSession::read() takes the following parameters:

  • serviceSettings: the general Service settings like timeout,
  • maxAge: the maximum age of the value to be read in milliseconds,
  • timeStamps: parameter which allows the client to define which timestamps (source and server time stamp, rsp.) the server should return with the value,
  • nodesToRead: list of Nodes and Attributes to read,
  • values: list of read results contained in OpcUa_DataValue structures,
  • diagnosticInfos: list of diagnostic information.

Complete header

Add

/* Globals */
UaSession* g_pUaSession;
Callback* g_pCallback;
// ++
UaObjectArray<UaNodeId> g_VariableNodeIds;
// ++

to header.

Complete main()

Add

/*********************************************************************
Read data
**********************************************************************/
read();
/*********************************************************************/

to main().

Complete disconnect()

Add the highlighted cocde snippet to disconnect():

/*============================================================================
disconnect - Disconnect from OPC UA Server
*===========================================================================*/
void disconnect()
{
printf("\n\n****************************************************************\n");
printf("** Disconnect from Server\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
ServiceSettings serviceSettings;
/*********************************************************************
Disconnect from OPC UA Server
**********************************************************************/
g_pUaSession->disconnect(
serviceSettings, // Use default settings
OpcUa_True); // Delete subscriptions
/*********************************************************************/
printf("****************************************************************\n");
delete g_pUaSession;
g_pUaSession = NULL;
// ++
g_VariableNodeIds.clear();
// ++
}

Step 2: Implementing Write

Add the following code to header,

/* OPC UA service calls */
void write();

and source file, rsp.:

void write()
{
printf("\n\n****************************************************************\n");
printf("** Try to call write for configured node ids\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
OpcUa_UInt32 i;
OpcUa_UInt32 count;
UaStatus status;
UaVariant tempValue;
UaWriteValues nodesToWrite;
UaStatusCodeArray results;
UaDiagnosticInfos diagnosticInfos;
ServiceSettings serviceSettings;
// Initialize IN parameter nodesToWrite
count = g_WriteVariableNodeIds.length();
nodesToWrite.create(count);
for ( i=0; i<count; i++ )
{
g_WriteVariableNodeIds[i].copyTo(&nodesToWrite[i].NodeId);
nodesToWrite[i].AttributeId = OpcUa_Attributes_Value;
tempValue.setDouble(71);
tempValue.copyTo(&nodesToWrite[i].Value.Value);
}
/*********************************************************************
Call Write service
**********************************************************************/
status = g_pUaSession->write(
serviceSettings, // Use default settings
nodesToWrite, // Array of nodes to write
results, // Returns an array of status codes
diagnosticInfos); // Returns an array of diagnostic info
/*********************************************************************/
if ( status.isBad() )
{
printf("** Error: UaSession::write failed [ret=%s] *********\n", status.toString().toUtf8());
}
else
{
printf("** UaSession::write result **************************************\n");
for ( i=0; i<count; i++ )
{
UaNodeId node(nodesToWrite[i].NodeId);
if ( OpcUa_IsGood(results[i]) )
{
printf("** Variable %s succeeded!\n", node.toString().toUtf8());
}
else
{
printf("** Variable %s failed!\n", node.toString().toUtf8());
}
}
printf("****************************************************************\n");
}
}

write() calls the actual UaSession::write() using the OPC UA server Write Service in order to write one or more Attribute values to one or more Nodes. UaSession::write() takes the following parameters:

  • serviceSettings: the general Service settings like timeout,
  • nodesToWrite: list of Nodes to write,
  • results: list of OpcUa_StatusCode structures,
  • diagnosticInfos: list of diagnostic information.

Complete header

Add

/* Globals */
UaSession* g_pUaSession;
Callback* g_pCallback;
UaObjectArray<UaNodeId> g_VariableNodeIds;
// ++
UaObjectArray<UaNodeId> g_WriteVariableNodeIds;
// ++

to header.

Complete main()

Add

/*********************************************************************
Write data
**********************************************************************/
write();
/*********************************************************************/

to main().

Complete disconnect()

Add this to disconnect():

/*============================================================================
disconnect - Disconnect from OPC UA Server
*===========================================================================*/
void disconnect()
{
printf("\n\n****************************************************************\n");
printf("** Disconnect from Server\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
ServiceSettings serviceSettings;
/*********************************************************************
Disconnect from OPC UA Server
**********************************************************************/
g_pUaSession->disconnect(
serviceSettings, // Use default settings
OpcUa_True); // Delete subscriptions
/*********************************************************************/
printf("****************************************************************\n");
delete g_pUaSession;
g_pUaSession = NULL;
g_VariableNodeIds.clear();
g_WriteVariableNodeIds.clear();
printf("\n*******************************************************\n");
printf("*******************************************************\n");
printf("** Press x to stop *******\n");
printf("*******************************************************\n");
printf("*******************************************************\n");
int action=-1;
/******************************************************************************/
/* Wait for user command to terminate the client thread. */
while(!WaitForKeypress(action))
{
Sleep(100);
}
}