ValueSymbolExtensions.PollValues.T. Method (IValueSymbol, IObservable.Unit., Func.ResultReadValueAccess2.IValueSymbol, Object., T.)

Poll symbol values on trigger signals (typed)

Namespace:  TwinCAT.Ads.Reactive
Assembly:  TwinCAT.Ads.Reactive (in TwinCAT.Ads.Reactive.dll) Version: 6.0.328+39e3229

Syntax

C#

public static IObservable<T> PollValues<T>(
    this IValueSymbol symbol,
    IObservable<Unit> trigger,
    Func<ResultReadValueAccess2<IValueSymbol, Object?>, T>? errorHandler
)

Parameters

symbol

Type: TwinCAT.TypeSystem.IValueSymbol
The symbol.

trigger

Type: System.IObservable.Unit.
The Polling trigger

errorHandler

Type: System.Func.ResultReadValueAccess2.IValueSymbol, Object., T.
The error handler

Type Parameters

T

The type of the values

Return Value

Type: IObservable.T.
IObservable<System.Object>.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IValueSymbol. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

If an read error occurs an optional error handler will be called. This decides the return value. If no errorHandler is specified, an AdsException is thrown on error.

Examples

Example for polling typed values and usage of an error handler.

Observe changing ADS Symbols (Read Polling)

// To Test the Observer run a project on the local PLC System (Port 851)
using (AdsClient client = new AdsClient())
{
    // Connect to target
    client.Connect(new AmsAddress(AmsNetId.Local, 851));

    // Create Symbol information
    var symbolLoader = SymbolLoaderFactory.Create(client, SymbolLoaderSettings.Default);
    IValueSymbol cycleCount = (IValueSymbol)symbolLoader.Symbols["TwinCAT_SystemInfoVarList._TaskInfo[1].CycleCount"];

    // Reactive Notification Handler
    var valueObserver = Observer.Create<ushort>(val =>
    {
        Console.WriteLine($"Instance: {cycleCount}, Value: {val}");
    }
    );

    // Creating an interval trigger.
    IObservable<Unit> interval = Observable.Interval(TimeSpan.FromMilliseconds(500)).StartWith(-1L).Select(l => Unit.Default);

    // Take 20 Values in an Interval of 500ms
    IDisposable subscription = cycleCount.PollValues<ushort>(interval,
        // Usage of an error handler
        (e) =>
        {
        if (e.ErrorCode == (int) AdsErrorCode.ClientSyncTimeOut)
        {
            // On Timeout return Value 0!
            return 0; 
        }
        else
        {
            // Finish observer
            throw new AdsException($"Internal Error Code: {e.ErrorCode} (Source: {e.Source})");
        }
        }
    )
    .Take(20).Subscribe(valueObserver);

    Console.ReadKey(); // Wait for Key press
    subscription.Dispose(); // Dispose the Subscription
}

Reference

ValueSymbolExtensions Class

PollValues Overload

TwinCAT.Ads.Reactive Namespace

ValueSymbolExtensions.PollValues.T.(IValueSymbol, IObservable.Unit.)

ValueSymbolExtensions.PollValues.T.(IValueSymbol, TimeSpan)