IAdsNotifications.AdsNotification Event

Occurs when the ADS device sends a notification to the client.

Namespace:  TwinCAT.Ads
Assembly:  TwinCAT.Ads (in TwinCAT.Ads.dll) Version: 4.3.0.0

Syntax

C#

event AdsNotificationEventHandler AdsNotification

VB

Event AdsNotification As AdsNotificationEventHandler

Value

Type: TwinCAT.Ads.AdsNotificationEventHandler

Examples

The following sample shows how to register/unregister for AdsNotification

Receive AdsNotifications

AdsStream readStream = new AdsStream(sizeof(UInt32));

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

    // Connect to target
    client.Connect("1.2.3.4.5.6", 851);
    int notificationHandle = 0;

    try
    {
        // Notification to a DINT Type (UINT32)
        // Check for change every 200 ms
        notificationHandle = client.AddDeviceNotification("MAIN.nCounter", readStream, 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)
{
    int offset = (int)e.DataStream.Position;
    int length = (int)e.DataStream.Length;

    e.DataStream.Position = offset;
    AdsBinaryReader reader = new AdsBinaryReader(e.DataStream);

    // Read the Unmarshalled data
    //byte[] data = reader.ReadBytes(length);

    // Or here we know about UDINT type --> can be marshalled as UINT32
    uint nCounter = reader.ReadUInt32();
}

Reference

IAdsNotifications Interface

TwinCAT.Ads Namespace