ValueSymbolExtensions Class

Extension class for IAdsConnection to provide reactive ADS extensions for accessing symbols that are loaded by the IAdsSymbolLoaderFactory

Inheritance Hierarchy

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

Syntax

C#

public static class ValueSymbolExtensions

Methods

 

Name

Description

ValueSymbolExtensions Class 1:

ValueSymbolExtensions Class 2:

ValueSymbolExtensions Class 3:

PollValues(ISumRead, IObservable.Unit.)

Polls a series of symbols via a ISumRead command. The SumCommand will read all contained values with every trigger.

ValueSymbolExtensions Class 4:

ValueSymbolExtensions Class 5:

ValueSymbolExtensions Class 6:

PollValues(ISumRead, TimeSpan)

Polls a series of symbols via a ISumRead2.S. command. The SumCommand will read all contained values with every trigger in one roundtrip.

ValueSymbolExtensions Class 7:

ValueSymbolExtensions Class 8:

PollValues(IValueSymbol, IObservable.Unit.)

Poll symbol values on trigger signals.

ValueSymbolExtensions Class 9:

ValueSymbolExtensions Class 10:

ValueSymbolExtensions Class 11:

PollValues(IValueSymbol, TimeSpan)

Polls the symbol as value sequence of object values with a specified period time.

ValueSymbolExtensions Class 12:

ValueSymbolExtensions Class 13:

PollValues(IValueSymbol, IObservable.Unit., Boolean)

Polls symbol values on trigger signals.

ValueSymbolExtensions Class 14:

ValueSymbolExtensions Class 15:

ValueSymbolExtensions Class 16:

PollValues(IValueSymbol, TimeSpan, Boolean)

Polls the symbol as value sequence of object values with a specified period time.

ValueSymbolExtensions Class 17:

ValueSymbolExtensions Class 18:

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

Poll symbol values as a value sequence on trigger signals (typed)

ValueSymbolExtensions Class 19:

ValueSymbolExtensions Class 20:

ValueSymbolExtensions Class 21:

PollValues.T.(IValueSymbol, TimeSpan)

Polls the symbol as value sequence of object values with a specified period time (typed)

ValueSymbolExtensions Class 22:

ValueSymbolExtensions Class 23:

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

Poll symbol values on trigger signals (typed)

ValueSymbolExtensions Class 24:

ValueSymbolExtensions Class 25:

ValueSymbolExtensions Class 26:

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

Poll symbol values on trigger signals (typed)

ValueSymbolExtensions Class 27:

ValueSymbolExtensions Class 28:

PollValues.T.(IValueSymbol, TimeSpan, Boolean)

Polls the symbol as value sequence of object values with a specified period time (typed)

ValueSymbolExtensions Class 29:

ValueSymbolExtensions Class 30:

PollValues2(IValueSymbol, IObservable.Unit.)

Poll symbol values as a sequence of annotated results (Value + ErrorCode)

ValueSymbolExtensions Class 31:

ValueSymbolExtensions Class 32:

PollValues2(IValueSymbol, TimeSpan)

Poll symbol values with communication return codes.

ValueSymbolExtensions Class 33:

ValueSymbolExtensions Class 34:

ValueSymbolExtensions Class 35:

PollValues2.S.(ISumRead2.S., IObservable.Unit.)

Polls a series of symbols via a ISumRead2.S. command. The SumCommand will read all contained values with every trigger in one roundtrip.

ValueSymbolExtensions Class 36:

ValueSymbolExtensions Class 37:

ValueSymbolExtensions Class 38:

PollValues2.S.(ISumRead2.S., TimeSpan)

Polls a series of symbols via a ISumRead2.S. command. The SumCommand will read all contained values with every trigger.

ValueSymbolExtensions Class 39:

ValueSymbolExtensions Class 40:

ValueSymbolExtensions Class 41:

WhenValueChanged(IValueSymbol)

Gets an observable sequence when the value of the IValueSymbol has changed.

ValueSymbolExtensions Class 42:

ValueSymbolExtensions Class 43:

ValueSymbolExtensions Class 44:

WhenValueChanged(IAdsConnection, IEnumerable.ISymbol.)

Observable sequence of Values driven by ADS Notifications on the specified symbol.

ValueSymbolExtensions Class 45:

ValueSymbolExtensions Class 46:

ValueSymbolExtensions Class 47:

WriteValues(IValueSymbol, IObservable.Object.)

Subscribes the IValueSymbol to an observable sequence of values and writes them to the IValueSymbol.

ValueSymbolExtensions Class 48:

ValueSymbolExtensions Class 49:

WriteValues(IValueSymbol, IObservable.Object., Action.Exception.)

Subscribes the IValueSymbol to an observable sequence of values and writes them to the IValueSymbol.

ValueSymbolExtensions Class 50:

ValueSymbolExtensions Class 51:

WriteValues(IValueSymbol, IObservable.Object., CancellationToken)

Subscribes the IValueSymbol to an observable sequence of values and writes them to the IValueSymbol.

ValueSymbolExtensions Class 52:

ValueSymbolExtensions Class 53:

WriteValues(IValueSymbol, IObservable.Object., Action.Exception., CancellationToken)

Subscribes the IValueSymbol to an observable sequence of values and writes them to the IValueSymbol.

Remarks

Reactive Extensions (Rx) are a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developers represent asynchronous data streams with Observables, query asynchronous data streams using LINQ operators, and parameterize the concurrency in the asynchronous data streams using Schedulers. Simply put, Rx = Observables + LINQ + Schedulers. The ADS reactive extensions are build on top of this library to enable ADS Symbol and State Observables, seamlessly bound to the reactive extensions. To use the ADS reactive extensions the TwinCAT.Ads.Reactive Nuget package (or the included TwinCAT.Ads.Reactive.dll) must be referenced from All types within are contained in the ADS companion package "Beckhoff.TwinCAT.Ads.Reactive" which must be referenced separately. (Beckhoff.TwinCAT.Ads.Reactive package on Nuget).

Examples

The following sample shows how to observe Value changed Notifications with the reactive ValueSymbolExtensions from an IValueSymbol.

Observe a single changing ADS Symbol (ADS Notifications)

// 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<object>(val =>
    {
    Console.WriteLine(string.Format("Instance: {0}, Value: {1}", cycleCount.InstancePath, val.ToString()));
    }
    );

    cycleCount.NotificationSettings = new NotificationSettings(AdsTransMode.OnChange, 500, 5000); // optional: Change NotificationSettings on Symbol

    // Turning ADS Notifications into sequences of Value Objects (Taking 20 Values)
    // and subscribe to them.
    IDisposable subscription = cycleCount.WhenValueChanged().Take(20).Subscribe(valueObserver);

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

Examples

The following sample shows how to observe Value changed Notifications with the reactive ValueSymbolExtensions from an DynamicSymbol.

Observe a single changing ADS Symbol (ADS Notifications) with the dynamic language runtime (.NET DLR)

// 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<object>(val =>
    {
    Console.WriteLine(string.Format("Instance: {0}, Value: {1}", cycleCount.InstancePath, val.ToString()));
    }
    );

    cycleCount.NotificationSettings = new NotificationSettings(AdsTransMode.OnChange, 500, 5000); // optional: Change NotificationSettings on Symbol

    // Turning ADS Notifications into sequences of Value Objects (Taking 20 Values)
    // and subscribe to them.
    IDisposable subscription = cycleCount.WhenValueChanged().Take(20).Subscribe(valueObserver);

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

Examples

The same for more than one IValueSymbol.

Observe changing ADS Symbols (ADS Notifications)

// 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.CycleCount"]; // UShort Type
    IValueSymbol lastExecTime = (IValueSymbol)symbolLoader.Symbols["TwinCAT_SystemInfoVarList._TaskInfo.LastExecTime"]; // UInt Type

    SymbolCollection symbols = new SymbolCollection();
    symbols.Add(cycleCount);
    symbols.Add(lastExecTime);

    // Reactive Notification Handler
    var valueObserver = Observer.Create<object>(val =>
    {
    Console.WriteLine(string.Format("Instance: {0}, Value: {1}", cycleCount.InstancePath, val.ToString()));
    }
    );

    cycleCount.NotificationSettings = new NotificationSettings(AdsTransMode.OnChange,500,5000); // optional: Change NotificationSettings on Symbol

    // Turning ADS Notifications into sequences of Value Objects (Taking 20 Values)
    // and subscribe to them.
    IDisposable subscription = client.WhenValueChanged(symbols).Take(20).Subscribe(valueObserver);

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

Examples

Here, the values are polled in a specific time period and sequential Reads are triggered (in opposite to ADS Notification in the latter example)

Observe changing ADS Symbols by polling (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<object>(val =>
    {
    Console.WriteLine(string.Format("Instance: {0}, Value: {1}", cycleCount.InstancePath, val.ToString()));
    }
    );

    // Take 20 Values in an Interval of 500ms
    IDisposable subscription = cycleCount.PollValues(TimeSpan.FromMilliseconds(500)).Take(20).Subscribe(valueObserver);

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

Examples

In the following example it is demonstrated how to write Values sequentially to a IValueSymbol with the help of the reactive extensions.

Write sequences of values to the target

using (AdsClient client = new AdsClient())
{
    // Connect to target
    client.Connect(new AmsAddress(AmsNetId.Local, 851));

    // Create Symbol information (Symbol 'i : INT' in PLC Global Variables list.
    var symbolLoader = SymbolLoaderFactory.Create(client, SymbolLoaderSettings.Default);
    IValueSymbol cycleCount = (IValueSymbol)symbolLoader.Symbols["GVL.i"];

    // Produces object Values 0,1,2,3 ... in seconds period
    IObservable<object> timerObservable = Observable.Interval(TimeSpan.FromSeconds(1.0)).Select(i => (object)(short)i);

    // Take 10 Values (0..9) and write them to GVL.i
    IDisposable dispose = cycleCount.WriteValues(timerObservable.Take(10));

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

Examples

Polling of values as SumCommands (reading of multiple symbols/values in one ADS roundtrip)

SumCommand polling (Symbols)

// 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"];
    IValueSymbol lastExecTime = (IValueSymbol)symbolLoader.Symbols["TwinCAT_SystemInfoVarList._TaskInfo.LastExecTime"];
    List<ISymbol> symbols = new List<ISymbol>() { cycleCount, lastExecTime };

    //Create the SumCommand
    SumSymbolRead sumRead = new SumSymbolRead(client, symbols);

    // Reactive Notification Handler
    var sumCommandObserver = Observer.Create<ResultSumValues2<ISymbol>>(result =>
    {
        Console.WriteLine($"SumCommand ErrorCode: {result.ErrorCode}");

        if (result.Succeeded)
        {
        Console.WriteLine($"SumCommand OverallSucceeded: {result.OverallSucceeded}");
        foreach (var subResult in result.ValueResults)
        {
            Console.WriteLine($"Source: {subResult.Source}, ErrorCode: {subResult.ErrorCode}, Value: {subResult.Value}");
        }
        }
    }
    );

    // Take 20 Values/Result in an Interval of 500ms
    IDisposable subscription = sumRead.PollValues2(TimeSpan.FromMilliseconds(500)).Take(20).Subscribe(sumCommandObserver);

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

SumCommand polling (via InstancePath)

// 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"];
    IValueSymbol lastExecTime = (IValueSymbol)symbolLoader.Symbols["TwinCAT_SystemInfoVarList._TaskInfo.LastExecTime"];
    List<ISymbol> symbols = new List<ISymbol>() { cycleCount, lastExecTime };

    //Create the SumCommand
    SumSymbolRead sumRead = new SumSymbolRead(client, symbols);

    // Reactive Notification Handler
    var sumCommandObserver = Observer.Create<ResultSumValues>(result =>
    {
    Console.WriteLine($"SumCommand Succeeded: {result.OverallSucceeded}, CycleCount: {result.Values[0]}, LastExecTime: {result.Values[1]}");
    }
    );

    // Take 20 Values/Result in an Interval of 500ms
    IDisposable subscription = sumRead.PollValues(TimeSpan.FromMilliseconds(500)).Take(20).Subscribe(sumCommandObserver);

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

Reference

TwinCAT.Ads.Reactive Namespace

TwinCAT.Ads.Reactive.AdsClientExtensions

TwinCAT.Ads.Reactive.AnyTypeExtensions