IAdsNotifications.AdsNotification Event
Occurs when the ADS device sends a notification to the client.
Namespace: TwinCAT.Ads
Assembly: TwinCAT.Ads.Abstractions (in
TwinCAT.Ads.Abstractions.dll) Version: 6.0.328+39e3229
Syntax
C#
event EventHandler<AdsNotificationEventArgs> AdsNotification
Remarks
The Event Argument contains the raw data value of the notification, not marshalled to .NET types.
Examples
Example of receiving AdsNotification events.
Trigger on changed values by ADS Notifications
private async Task RegisterNotificationsAsync()
{
CancellationToken cancel = CancellationToken.None;
using (AdsClient client = new AdsClient())
{
// Add the Notification event handler
client.AdsNotification += Client_AdsNotification2;
// Connect to target
client.Connect(AmsNetId.Local, 851);
uint notificationHandle = 0;
// Notification to a DINT Type (UINT32)
// Check for change every 200 ms
//byte[] notificationBuffer = new byte[sizeof(UInt32)];
int size = sizeof(UInt32);
ResultHandle result = await client.AddDeviceNotificationAsync("MAIN.nCounter", size, new NotificationSettings(AdsTransMode.OnChange, 200, 0), null, cancel);
if (result.Succeeded)
{
notificationHandle = result.Handle;
await Task.Delay(5000); // Wait asynchronously without blocking the UI Thread.
// Unregister the Event / Handle
ResultAds result2 = await client.DeleteDeviceNotificationAsync(notificationHandle, cancel);
}
client.AdsNotification -= Client_AdsNotification2;
}
}
private void Client_AdsNotification2(object sender, AdsNotificationEventArgs e)
{
// Or here we know about UDINT type --> can be marshalled as UINT32
uint nCounter = BinaryPrimitives.ReadUInt32LittleEndian(e.Data.Span);
// If Synchronization is needed (e.g. in Windows.Forms or WPF applications)
// we could synchronize via SynchronizationContext into the UI Thread
/*SynchronizationContext syncContext = SynchronizationContext.Current;
_context.Post(status => someLabel.Text = nCounter.ToString(), null); // Non-blocking post */
}