UA Bundle SDK .NET  2.2.3.276
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Groups Pages
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.

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

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.

Double-click on a parameter to edit it.

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.

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 = new List<Variant>();
foreach (ListViewItem item in InputArgumentsLV.Items)
{
if (item.Tag != null)
{
inputArguments.Add(((Argument)item.Tag).Value);
}
}

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 = 10000 },
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))
{
m_parent.ShowError(this, "Server returned an error whle calling method: " + error.ToString());
return;
}
// update the output arguments.
for (int ii = 0; ii < outputArguments.Count; ii++)
{
Argument argument = OutputArgumentsLV.Items[ii].Tag as Argument;
// Extract value from method call output argument list
argument.Value = outputArguments[ii];
OutputArgumentsLV.Items[ii].SubItems[1].Text = argument.Value.ToString();
}

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.

NodeId objectId = new NodeId("MyDemoObject", 4);
NodeId methodId = null;
byte[] continuationPoint;
// Reduce the browse result to Method nodes
BrowseContext browseContext = new BrowseContext();
browseContext.NodeClassMask = (uint)NodeClass.Method;
// Browse the node MyDemoObject for methods
List<ReferenceDescription> results = session.Browse(objectId, browseContext, out continuationPoint);
if (results.Count > 0)
{
// Use the first method returned by server
methodId = (NodeId)results[0].NodeId;
}

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.

// Use Method nodeId returned from Browse to get argument lists
MethodDescription method = session.Model.GetMethodDescription(methodId);
// Access list of input arguments
if (method.InputArguments != null)
{
foreach (Argument argument in method.InputArguments)
{
Variant defaultValue = TypeUtils.GetDefaultValue(argument.DataType, argument.ValueRank, session.Cache);
string argumentName = argument.Name;
string argumentDescription = argument.Description.ToString();
// Display arguments
}
}
else
{
// No input arguments
}
// Access list of output arguments
if (method.InputArguments != null)
{
foreach (Argument argument in method.OutputArguments)
{
string argumentName = argument.Name;
string argumentDescription = argument.Description.ToString();
// Display arguments
}
}
else
{
// No output arguments
}

Method Call simple example

The following self contained code connects to the C++ 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 C++ Demo Server
session.Connect("opc.tcp://localhost:4841", SecuritySelection.None);
// Input argument
Int32 newValue = 100;
// Output argument
UInt32 numberOfUpdatedVariables = 0;
// Method SetAllValues of MyDemoObject provided by C++ Demo Server
NodeId objectId = new NodeId("MyDemoObject", 4);
NodeId methodId = new NodeId("MyDemoObject.SetAllValues", 4);
// One input argument of data type Int32
List<Variant> inputArguments = new List<Variant>();
inputArguments.Add(new Variant(newValue));
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
numberOfUpdatedVariables = outputArguments[0].ToUInt32();
}
// Disconnect from server
session.Disconnect();