Sample Application Server script
- Last UpdatedJul 22, 2024
- 2 minute read
The following sample ApplicationObject script utilizing the script library to call two methods on a OPC UA server is shown for illustration.
The first method (Multiply) accepts two arguments of type double and returns their product as a single argument, also of type double. In pseudo-syntax:
double Multiply(double x, double y)
Declarations
Make the following variable declarations to start building the script:
dim methodHelper as aaMethods.Client;
dim methodCallStatus as aaMethods. MethodCallStatus;
dim inputArgs[2] as aaMethods.MethodArgument;
dim outputArgs[1] as aaMethods.MethodArgument;
dim multiplyResult as aaMethods.Value;
Initialize the script library
Initialize the script library with connection information prior to calling methods:
methodHelper = CreateMethod.Client(PCSScopeName);
or
methodHelper = CreateMethodClient("PCSScopeName", "OIGatewayServerNodeName")
The CreateMethodClient() method requires the mandatory argument PCSScopeName. The argument OIGatewayServerNodeName is optional; this is the name of the machine that is hosting the instance of the Gateway Communication Driver that is connected to the remote OPC UA server. Refer to the Gateway Communication Driver User Guide for details on configuring a Gateway instance.
The CreateMethodClient() method returns an instance of aaMethods.Client, that is the handler for all Method calls to the Gateway Communication Driver. It returns a null value in case of failure to establish a connection.
Call a method with simple arguments
Next, the script calls a simple method (Multiply) on the OPC UA server, with two input arguments of type double and a single output argument, also of type double.
aaMethods supports two types of method calls:
-
CallMethod: This method is recommended when the script is executing in Asynchronous mode.
-
CallMethodAsync: This method is recommended when the script is executing in Synchronous mode.
Note that CallMethodAsync is not supported in Visual Elements, i.e., in graphics and layouts.
CallMethod example:
inputArgs[1] = new aaMethods.MethodArgument("x",
aaMethods.Value.Create(aaMethods.ValueType.Double, Me.X));
inputArgs[2] = new aaMethods.MethodArgument("y",
aaMethods.Value.Create(aaMethods.ValueType.Double, Me.Y));
outputArgs = methodHelper.CallMethod("OPCUA_DeviceGroup",
"OPCUA_DeviceGroup./DemoServer/s=Demo.Method", "Multiply",
"Joe Operator", inputArgs);
The two input arguments are created and stored in the inputArgs array (note that in Application Server scripts, arrays are indexed starting at 1), using a static method in the script library (aaMethods.Value.Create) to generate a value of the specified type, in this case double. The references Me.X and Me.Y assume that the Application Object hosting the script has two attributes X and Y, both of type double.
The script library method CallMethod() instructs the Gateway Communication Driver to issue the specified method call to the OPC UA server. The CallMethod() arguments are:
-
OPCUA_DeviceGroup and OPCUA_DeviceGroup./DemoServer/s=Demo.Method
These uniquely identify the object in the OPC UA server hosting the method to be called. -
Multiply is the name of the method exposed by the OPC UA server object.
-
JoeOperator is the name of the user calling the method.
-
inputArgs is the array containing the method input arguments.
-
outputArgs is the array containing the arguments returned from the method call.
The script stores the result of the method call in an object attribute Result of type double:
multiplyResult = outputArgs[1][0]
Me.Result = multiplyResult.GetAsDouble();