AdsClientExtensions Class
Extension class for TcAdsClient 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: 4.3.0.0 (4.3.7.0)
Syntax
C#
public static class AdsClientExtensions
VB
<ExtensionAttribute>
Public NotInheritable 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 AdsStates. | |
|
Gets an observable sequence of Notifications. | |
|
Gets an observable sequence of Notification objects. | |
|
WhenNotification(IAdsConnection, ISymbol, NotificationSettings) |
Gets an observable sequence of Notifications. |
|
WhenNotification(IAdsConnection, ISymbolCollection, NotificationSettings) |
Gets an observable sequence of Notification objects. |
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 (TcAdsClient client = new TcAdsClient())
{
// 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<SymbolNotification>(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 (TcAdsClient client = new TcAdsClient())
{
// 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 =>
{
AdsState oldValue = not[0];
AdsState newValue = not[1];
Console.WriteLine(string.Format("Changed ADSState from '{0}' --> '{1}!", oldValue, newValue));
}
);
// 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
}