AdsConnectionInvokeRpcMethodAsync(IRpcCallableInstance, IRpcMethod, Object, AnyTypeSpecifier, AnyTypeSpecifier, CancellationToken) Method

Invokes the specified RPC Method asynchronously


Namespace: TwinCAT.Ads
Assembly: TwinCAT.Ads (in TwinCAT.Ads.dll) Version: 7.0.0+e56d35ccc4675faac24789a4aab60071fc61d470

Syntax

C#

public Task<ResultRpcMethod> InvokeRpcMethodAsync(
    IRpcCallableInstance symbol,
    IRpcMethod method,
    Object[]? inParameters,
    AnyTypeSpecifier[]? outSpecifiers,
    AnyTypeSpecifier? retSpecifier,
    CancellationToken cancel
)

Parameters

symbol  IRpcCallableInstance

The RPC callable symbol."

method  IRpcMethod

The method.

inParameters  Object

The parameters.

outSpecifiers  AnyTypeSpecifier

The out specifiers.

retSpecifier  AnyTypeSpecifier

The ret specifier.

cancel  CancellationToken

The cancellation token

Return Value

Task ResultRpcMethod
A task that represents the asynchronous 'InvokeRpcMethod' operation. The ResultRpcMethod results contains the return value together with the output parameters.

Remarks

The RpcMethod optionally support In-Parameters, Out-Parameters and Return values. Therefore the parameters inParameters are allowed to be empty or NULL.ReturnValue and the ErrorCode of the ADS communication after execution.

Implements

IAdsRpcInvokeInvokeRpcMethodAsync(IRpcCallableInstance, IRpcMethod, Object, AnyTypeSpecifier, AnyTypeSpecifier, CancellationToken)

Exceptions

Exception

Condition

ObjectDisposedException

this.Name

ClientNotConnectedException

Example

The following sample shows how to call (Remote Procedures / Methods) within the PLC.

Dynamic Tree Mode

class RpcCallVirtualProgram
{
    /// <summary>
    /// Defines the entry point of the application.
    /// </summary>
    /// <param name="args">The arguments.</param>
    static void Main(string[] args)
    {
    // Get the AdsAddress from command-line arguments
    AmsAddress address = ArgParser.Parse(args);

    using (AdsClient client = new AdsClient())
    {
        //client.Synchronize = false;

        // Connect to the target device
        client.Connect(address);

        SymbolLoaderSettings settings = new SymbolLoaderSettings(SymbolsLoadMode.VirtualTree);
        ISymbolLoader loader = SymbolLoaderFactory.Create(client, settings);

        // Get the Symbols (Dynamic Symbols)

        IStructInstance main = (IStructInstance)loader.Symbols["MAIN"]; // Gets the MAIN Instance of the PLC Program

        // Call a Method that has the following signature (within MAIN Program)
        /*  {attribute 'TcRpcEnable'}
        METHOD PUBLIC M_Add : INT
        VAR_INPUT
            i1 : INT := 0;
            i2 : INT := 0;
        END_VAR 
        */

        short result = (short)main.InvokeRpcMethod("M_Add", new object[] {(short) 3, (short) 4});

        // Call a Method that has no parameter and returns VOID
        main.InvokeRpcMethod("M_Method1", new object[] {});

        //Browsing RpcMethods
        foreach(IRpcMethod method in main.RpcMethods)
        {
        string methodName = method.Name;

        foreach(IRpcMethodParameter parameter in method.Parameters)
        {
            string parameterName = parameter.Name;
            string parameterType = parameter.TypeName;
        }
        }
    }
    }
}

Reference

AdsConnection Class InvokeRpcMethodAsync Overload TwinCAT.Ads Namespace

Beckhoff Automation GmbH & Co. KG 2001-2026