ValueSymbolExtensions Class

Extension class for TcAdsClient respective 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: 4.3.0.0 (4.3.7.0)

Syntax

C#

public static class ValueSymbolExtensions

VB

<ExtensionAttribute>
Public NotInheritable Class ValueSymbolExtensions

Methods

 

Name

Description

PollValuesAnnotated(IValueSymbol, IObservable.Unit.)

Polls the values as ValueChangedArgs sequence annotated value on trigger sequence

PollValuesAnnotated(IValueSymbol, TimeSpan)

Polls the values as ValueChangedArgs sequence with a specified period time.

WhenValueChanged(IValueSymbol)

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

WhenValueChanged(IAdsConnection, IEnumerable.ISymbol.)

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

WriteValues(IValueSymbol, IObservable.Object.)

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

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

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

WriteValues(IValueSymbol, IObservable.Object., CancellationToken)

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

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 (TcAdsClient client = new TcAdsClient())
{
    // 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 (TcAdsClient client = new TcAdsClient())
{
    // 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 (TcAdsClient client = new TcAdsClient())
{
    // 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 (TcAdsClient client = new TcAdsClient())
{
    // 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 (TcAdsClient client = new TcAdsClient())
{
    // 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
}

Reference

TwinCAT.Ads.Reactive Namespace

TwinCAT.Ads.Reactive.AdsClientExtensions

TwinCAT.Ads.Reactive.AnyTypeExtensions