Event driven read with ADS Notifications

Use of ADS Notifications (Async)

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 */
}

Use of ADS Notifications (Synchronous)

Trigger on changed values by ADS Notifications

private void RegisterNotifications()
{
    using (AdsClient client = new AdsClient())
    {
        // Add the Notification event handler
        client.AdsNotification += Client_AdsNotification;

        // Connect to target
        client.Connect(AmsNetId.Local, 851);
        uint notificationHandle = 0;

        try
        {
            // Notification to a DINT Type (UINT32)
            // Check for change every 200 ms

            int size = sizeof(UInt32);
            //byte[] notificationBuffer = new byte[sizeof(UInt32)];

            notificationHandle = client.AddDeviceNotification("MAIN.nCounter", size, new NotificationSettings(AdsTransMode.OnChange, 200, 0), null);
            Thread.Sleep(5000); // Sleep the main thread to get some (asynchronous Notifications)
        }
        finally
        {
            // Unregister the Event / Handle
            client.DeleteDeviceNotification(notificationHandle);
            client.AdsNotification -= Client_AdsNotification;
        }
    }
}

private void Client_AdsNotification(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 */
}

Beckhoff Automation GmbH & Co. KG 2001-2026