.NET Based OPC UA Client/Server SDK  3.3.3.537
Call – Call Method

Prerequisites

A session with the server must be established.

User Interface Description

The following dialog shows an example implementation of the Call service which invokes a Method on an Object. Press the button “Show Code” to display the corresponding code, and the button “Help” to show this documentation page.

clienttutorials_call_method.png

Press the button “...” to open a browse window and select an object on which a method is defined.

clienttutorials_read_attribute2.png

After selecting an object, the drop down box Method will be filled with the available methods for the object.

After selecting a method, the parameter list for the Request will be filled with the input arguments of the selected method.

clienttutorials_call_method2.png

Double-click on a parameter to edit it.

clienttutorials_call_method3.png

Check the box in front of “Use Asynchronous Pattern” to call “BeginCall” instead of “Call”.

The method call is executed by clicking the “Call” button. The output arguments will be displayed in the parameter list of the response.

clienttutorials_call_method4.png

Method Call Sample Code

The sample code can be found in the CallMethodDlg class. You can open the file from the sample dialog with the “Show Code” button or you can open the complete solution via Examples → GettingStarted. The CallMethodDlg.cs file can be found in the Method folder of the Getting Started solution.

A method in OPC UA is called with

  • NodeId of the object node that provides the method
  • NodeId of the method node to call
  • List of input arguments as list of Variants

The method returns

  • Method result as Status
  • List of output arguments as list of Variants

If the method is well known, it can be called with the known list of arguments. The data types of the arguments and the number of arguments must match the method definition in the OPC UA server.

If the method is not known, the information about the method arguments can be requested from the OPC UA server. Sample code for getting information about a method and available methods can be found in the following section.

Step 1

In a first step the request parameters for the method call are prepared. The object NodeId is parsed from the Object text box in the dialog. The method NodeId is extracted from the reference description attached to the Method drop down box in the dialog. The list of input argument values is collected from the list box in the dialog.

Used SDK classes are

// parse the object id.
NodeId objectId = NodeId.Parse(NodeIdTB.Text);
// get the selected method id.
NodeId methodId = (NodeId)((ReferenceDescription)MethodCB.SelectedItem).NodeId;
// get input arguments.
List<Variant> inputArguments = GetInputArgumentsFromGui();

Step 2

In a second step the method is called on the server

Used SDK classes and methods are

List<StatusCode> inputArgumentErrors = null;
List<Variant> outputArguments = null;
// call the method on the server.
StatusCode error = session.Call(
objectId,
methodId,
inputArguments,
new RequestSettings() { OperationTimeout = 15000 },
out inputArgumentErrors,
out outputArguments);

Step 3

In a third step the result code of the method is checked and the output arguments are filled into the response list box.

// check for error.
// A method always returns a status code indicating success or failure
if (StatusCode.IsBad(error))
{
ShowError(error);
return;
}
// update the output arguments.
SetOutputArgumentsToGui(outputArguments);

Method Call Preparation Sample Code

In this section the NodeId of the Method and the descriptions of the method arguments are not well known. It is shown how to request the needed information for the method call from the SDK.

The following sample code shows how Browse can be used to get the list of Methods of an Object.

private void BrowseMethods(NodeId objectId)
{
// get the current session from the parent form.
Session session = m_parent.Session;
// nothing to do if no session.
if (session == null)
{
return;
}
BrowseDescription description = new BrowseDescription()
{
NodeId = objectId,
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HasComponent,
IncludeSubtypes = true,
NodeClassMask = (uint)NodeClass.Method,
ResultMask = (uint)(BrowseResultMask.BrowseName | BrowseResultMask.DisplayName)
};
List<BrowseDescription> nodesToBrowse = new List<BrowseDescription>();
nodesToBrowse.Add(description);
session.BeginBrowseList(
null,
nodesToBrowse,
0,
new RequestSettings() { OperationTimeout = 10000 },
OnBrowseMethodsComplete,
session);
}
private void OnBrowseMethodsComplete(IAsyncResult result)
{
// need to make sure the results are processed on the correct thread.
if (InvokeRequired)
{
BeginInvoke(new AsyncCallback(OnBrowseMethodsComplete), result);
return;
}
// get the session used to send the request which was passed as the userData in the Begin call.
Session session = (Session)result.AsyncState;
try
{
// get the results.
List<List<ReferenceDescription>> results = session.EndBrowseList(result);

The list of method input and output arguments can be requested using the helper function GetMethodDescription.

The corresponding sample code can be found in CallMethodDlg::MethodCB_SelectedIndexChanged. It is used to display the list of input and output arguments in the dialog.

session.Model.BeginGetMethodDescription(methodId, OnMethodSelectedComplete, session);
MethodDescription method = session.Model.EndGetMethodDescription(result);
foreach (Argument argument in method.InputArguments)
{
Variant defaultValue = TypeUtils.GetDefaultValue(argument.DataType, argument.ValueRank, session.Cache);
string argumentName = argument.Name;

Method Call Simple Example

The following self contained code connects to the .NET Demo Server and calls a method with one input argument and one output argument.

Session session = new Session();
session.SessionName = "Unified Automation .NET SDK sample code";
// Connect to .NET Demo Server
session.Connect("opc.tcp://localhost:48030", SecuritySelection.None);
// Input argument
double a = 42;
double b = 4711;
// Output argument
double result = 0;
// Method Multiply of Method folder provided by .NET Demo Server
NodeId objectId = new NodeId("Demo.Method", 2);
NodeId methodId = new NodeId("Demo.Method.Multiply", 2);
// Two input argument of data type double
List<Variant> inputArguments = new List<Variant>();
inputArguments.Add(new Variant(a));
inputArguments.Add(new Variant(b));
List<StatusCode> inputArgumentErrors = null;
List<Variant> outputArguments = null;
// call the method on the server.
StatusCode result = session.Call(
objectId,
methodId,
inputArguments,
out inputArgumentErrors,
out outputArguments);
// Check result
if (result.IsGood())
{
// Copy output argument
result = outputArguments[0].ToDouble();
}
// Disconnect from server
session.Disconnect();