AdsClientExtensions Class
Extension class for AdsClient respective IAdsConnection to provide reactive ADS extensions.
Inheritance Hierarchy
System.Object
TwinCAT.Ads.Reactive.AdsClientExtensions
Namespace: TwinCAT.Ads.Reactive
Assembly: TwinCAT.Ads.Reactive (in
TwinCAT.Ads.Reactive.dll) Version: 6.0.328+39e3229
Syntax
C#
public static class AdsClientExtensions
The AdsClientExtensions type exposes the following members.
Methods
|
Name |
Description |
---|---|---|
|
Gets an observable sequence of AdsStates via Polling. | |
|
Gets an observable sequence of AdsStates via Polling. | |
|
Gets an observable sequence of ResultReadAdsStates via Polling. | |
|
Gets an observable sequence of ResultReadAdsStates via Polling. | |
|
PollAdsState2Async(IAdsConnection, IObservable.Unit., CancellationToken) |
Gets an observable sequence of AdsStates via Polling. |
|
PollAdsState2Async(IAdsConnection, TimeSpan, CancellationToken) |
Gets an observable sequence of ResultReadAdsStates via Polling. |
|
PollAdsStateAsync(IAdsConnection, IObservable.Unit., CancellationToken) |
Gets an observable sequence of AdsStates via Polling. |
|
PollAdsStateAsync(IAdsConnection, TimeSpan, CancellationToken) |
Gets an observable sequence of AdsStates via Polling. |
|
Gets an observable sequence of ResultReadDeviceStates via Polling. | |
|
Gets an observable sequence of ResultReadDeviceStates via Polling. | |
|
PollDeviceStateAsync(IAdsConnection, IObservable.Unit., CancellationToken) |
Gets an observable sequence of ResultReadDeviceStates via Polling. |
|
PollDeviceStateAsync(IAdsConnection, TimeSpan, CancellationToken) |
Gets an observable sequence of ResultReadDeviceStates via Polling. |
|
Gets an observable sequence of AdsStates. | |
|
Gets an observable sequence of Notifications. | |
|
Gets an observable sequence of Notification objects. | |
|
WhenNotification(IAdsConnection, IList.ISymbol., NotificationSettings) |
Gets an observable sequence of Notification objects. |
|
WhenNotification(IAdsConnection, ISymbol, NotificationSettings) |
Gets an observable sequence of SymbolValueNotifications. |
|
Gets an observable sequence of SymbolVersion changed counts. | |
|
Gets an observable sequence of SymbolVersion changed counts. |
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. (Beckhoff.TwinCAT.Ads.Reactive package on Nuget).
Examples
The following sample shows how observe Value changed Notifications with the reactive AdsClientExtensions
Observe changing ADS Symbols with reactive extensions.
// 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);
int eventCount = 1;
// Reactive Notification Handler
var valueObserver = Observer.Create<SymbolValueNotification>(not =>
{
Console.WriteLine(string.Format("{0} {1:u} {2} = '{3}' ({4})", eventCount++, not.TimeStamp, not.Symbol.InstancePath, not.Value, not.Symbol.DataType));
}
);
// Collect the symbols that are registered as Notification sources for their changed values.
SymbolCollection notificationSymbols = new SymbolCollection();
IArrayInstance taskInfo = (IArrayInstance)symbolLoader.Symbols["TwinCAT_SystemInfoVarList._TaskInfo"];
foreach(ISymbol element in taskInfo.Elements)
{
ISymbol cycleCount = element.SubSymbols["CycleCount"];
ISymbol lastExecTime = element.SubSymbols["LastExecTime"];
notificationSymbols.Add(cycleCount);
notificationSymbols.Add(lastExecTime);
}
// Create a subscription for the first 200 Notifications on Symbol Value changes.
IDisposable subscription = client.WhenNotification(notificationSymbols,NotificationSettings.Default).Take(200).Subscribe(valueObserver);
Console.ReadKey(); // Wait for Key press
subscription.Dispose(); // Dispose the Subscription
}
Examples
The following sample shows how observe AdsState changed Notifications with the reactive AdsClientExtensions
Observe changing ADS states with reactive extensions.
// To Test the observer, Start/Stop the local PLC
using (AdsClient client = new AdsClient())
{
// Connect to target
client.Connect(new AmsAddress(AmsNetId.Local, 851));
// Create Symbol information
var symbolLoader = SymbolLoaderFactory.Create(client, SymbolLoaderSettings.DefaultDynamic);
// Reactive Notification Handler
var valueObserver = Observer.Create<IList<AdsState>>(not =>
{
// OnNext handler (handling the StateChange)
AdsState oldValue = not[0];
AdsState newValue = not[1];
Console.WriteLine(string.Format("Changed ADSState from '{0}' --> '{1}!", oldValue, newValue));
},
ex => Console.WriteLine($"Error: '{ex.Message}'"), // Error Handling
() => Console.WriteLine("WhenAdsStateChanges completed!") // Observer has completed
);
// Create a subscription for the AdsState change and buffering 2 Values (for oldValue --> newValue output).
IDisposable subscription = client.WhenAdsStateChanges().Buffer(2,1).Subscribe(valueObserver);
Console.ReadKey(); // Wait for Key press
subscription.Dispose(); // Dispose the Subscription
}