ValueSymbolExtensionsPollValuesT(IValueSymbol, IObservableUnit, FuncResultReadValueAccess2IValueSymbol, Object, T) Method

Poll symbol values on trigger signals (typed)


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

Syntax

C#

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

Parameters

symbol  IValueSymbol

The symbol.

trigger  IObservableUnit

The Polling trigger

errorHandler  FuncResultReadValueAccess2IValueSymbol, Object, T

The error handler

Type Parameters

T

The type of the values

Return Value

IObservableT
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.

Example

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 PollValuesT(IValueSymbol, IObservableUnit) PollValuesT(IValueSymbol, TimeSpan)

Beckhoff Automation GmbH & Co. KG 2001-2026