Content:
Step 1: Implementing Read
Add the following code to header,
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;
UaReadValueIds nodesToRead;
UaDataValues values;
UaDiagnosticInfos diagnosticInfos;
ServiceSettings serviceSettings;
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;
}
status = g_pUaSession->read(
serviceSettings,
0,
OpcUa_TimestampsToReturn_Both,
nodesToRead,
values,
diagnosticInfos);
{
printf(
"** Error: UaSession::read failed [ret=%s] *********\n", status.
toString().
toUtf8());
}
else
{
printf("** UaSession::read result **************************************\n");
for ( i=0; i<count; i++ )
{
if ( OpcUa_IsGood(values[i].StatusCode) )
{
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
to header.
Complete main()
Add
to main().
Complete disconnect()
Add the highlighted cocde snippet to disconnect():
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;
g_pUaSession->disconnect(
serviceSettings,
OpcUa_True);
printf("****************************************************************\n");
delete g_pUaSession;
g_pUaSession = NULL;
g_VariableNodeIds.clear();
}
Step 2: Implementing Write
Add the following code to header,
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;
UaWriteValues nodesToWrite;
UaStatusCodeArray results;
UaDiagnosticInfos diagnosticInfos;
ServiceSettings serviceSettings;
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.
copyTo(&nodesToWrite[i].Value.Value);
}
status = g_pUaSession->write(
serviceSettings,
nodesToWrite,
results,
diagnosticInfos);
{
printf(
"** Error: UaSession::write failed [ret=%s] *********\n", status.
toString().
toUtf8());
}
else
{
printf("** UaSession::write result **************************************\n");
for ( i=0; i<count; i++ )
{
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
to header.
Complete main()
Add
to main().
Complete disconnect()
Add this to disconnect():
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;
g_pUaSession->disconnect(
serviceSettings,
OpcUa_True);
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;
while(!WaitForKeypress(action))
{
Sleep(100);
}
}