UA Server SDK C++ Bundle  1.3.2.200
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
Lesson 5: Calling Methods

Content:

Step 1: Implementing Call

Add the following code to header,

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

and source file, rsp.:

void callMethod()
{
printf("\n\n****************************************************************\n");
printf("** Try to call a method\n");
if ( g_pUaSession == NULL )
{
printf("** Error: Server not connected\n");
printf("****************************************************************\n");
return;
}
UaStatus status;
CallIn callRequest;
CallOut callResult;
UaVariant vTemp;
ServiceSettings serviceSettings;
g_ObjectNodeIds.create(1);
g_MethodNodeIds.create(1);
g_ObjectNodeIds[0].setNodeId( OBJECT_STRING_NODEID, NAMESPACE_INDEX );
g_MethodNodeIds[0].setNodeId( METHOD_STRING_NODEID, NAMESPACE_INDEX );
callRequest.objectId = g_ObjectNodeIds[0];
callRequest.methodId = g_MethodNodeIds[0];
/* Used for Methods with arguments
callRequest.inputArguments.create(2);
// Set Temperature Set Point
vTemp.setDouble(60);
vTemp.copyTo(&callRequest.inputArguments[0]);
// Set Humitidy Set Point
vTemp.setDouble(50);
vTemp.copyTo(&callRequest.inputArguments[1]);*/
/*********************************************************************
Call Method
**********************************************************************/
status = g_pUaSession->call(
serviceSettings, // Use default settings
callRequest, // In parameters and settings for the method call
callResult); // Out parameters and results returned from the method call
/*********************************************************************/
if ( status.isBad() )
{
printf("** Error: UaSession::call failed [ret=%s] *********\n", status.toString().toUtf8());
}
else
{
printf("** Error: UaSession::call succeeded!\n");
}
printf("****************************************************************\n");
}

call() calls the actual UaSession::call() in order to call a particular Method which belongs to a particular Object. UaSession::call() takes the following parameters:

  • serviceSettings: the general Service settings like timeout,
  • callRequest: the Object and Method to call and input arguments ( if necessary ),
  • callResult: the output arguments and input argument results.

Complete header

Add

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

to header.

Add define

Add

. . .
// ServerURL
#define SERVER_URL "opc.tcp://localhost:4841"
// ++
// NodeIds for a method call
#define OBJECT_STRING_NODEID "AirConditioner_2"
#define METHOD_STRING_NODEID "AirConditioner_2.Stop"
// Namespace
#define NAMESPACE_INDEX 2
#define NAMESPACE_URI "MyUaServer/BuildingAutomation"
// ++
. . .

to source file.

Complete main()

Add

/*********************************************************************
Call Method
**********************************************************************/
callMethod();
/*********************************************************************/

to main().