DynamicRpcStructInstance.TryInvokeRpcMethod Method (String, .Object., .Object.., Object.)

Invokes the specified RPC Method

Namespace:  TwinCAT.TypeSystem
Assembly:  TwinCAT.Ads (in TwinCAT.Ads.dll) Version: 6.0.95+cf8c3f5

Syntax

C#

public int TryInvokeRpcMethod(
    string methodName,
    Object[]? inParameters,
    out Object[]? outParameters,
    out Object?? retValue
)

Parameters

methodName

Type: System.String
The method name.

inParameters

Type: .System.Object.
The parameters.

outParameters

Type: .System.Object..
The out parameters.

retValue

Type: System.Object.
The return value of the RPC method as object.

Return Value

Type: Int32
The ADS Error Code.

Implements

IRpcCallableInstance.TryInvokeRpcMethod(String, .Object., .Object.., Object.)

Remarks

Because this overload doesn't provide any AnyTypeSpecifier specifications, only primitive datatypes will be correctly marshalled by this method. Complex types will fall back to byte[] arrays.

Examples

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)

        IRpcStructInstance main = (IRpcStructInstance)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

DynamicRpcStructInstance Class

TryInvokeRpcMethod Overload

TwinCAT.TypeSystem Namespace