TcAdsClient.AddDeviceNotification Method (UInt32, UInt32, AdsStream, Int32, Int32, AdsTransMode, TimeSpan, TimeSpan, Object)

Connects a variable to the ADS client. The ADS client will be notified by the AdsNotification event.

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

Syntax

C#

public int AddDeviceNotification(
    uint indexGroup,
    uint indexOffset,
    AdsStream dataStream,
    int offset,
    int length,
    AdsTransMode transMode,
    TimeSpan cycleTime,
    TimeSpan maxDelay,
    Object userData
)

VB

Public Function AddDeviceNotification ( 
    indexGroup As UInteger,
    indexOffset As UInteger,
    dataStream As AdsStream,
    offset As Integer,
    length As Integer,
    transMode As AdsTransMode,
    cycleTime As TimeSpan,
    maxDelay As TimeSpan,
    userData As Object
) As Integer

Parameters

indexGroup

Type: System.UInt32
Contains the index group number of the requested ADS service.

indexOffset

Type: System.UInt32
Contains the index offset number of the requested ADS service.

dataStream

Type: TwinCAT.Ads.AdsStream
The stream that should receive the data.

offset

Type: System.Int32
Offset of the data in dataStream.

length

Type: System.Int32
Length of the data in dataStream.

transMode

Type: TwinCAT.Ads.AdsTransMode
Specifies if the event should be fired cyclically or only if the variable has changed.

cycleTime

Type: System.TimeSpan
The ADS server checks whether the variable has changed after this time interval. Unit is in ms.

maxDelay

Type: System.TimeSpan
The AdsNotification event is fired at the latest when this time has elapsed. The unit is ms.

userData

Type: System.Object
This object can be used to store user specific data.

Return Value

Type: Int32
The handle of the notification.

Exceptions

Exception

Condition

AdsErrorException

Thrown when the ADS call fails.

Examples

The following sample shows how to register/unregister for AdsNotifications (asynchronous change messages) via AddDeviceNotification(UInt32, UInt32, AdsStream, AdsTransMode, Int32, Int32, Object) and DeleteDeviceNotification(Int32)

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

TcAdsClient Class

AddDeviceNotification Overload

TwinCAT.Ads Namespace

TcAdsClient.DeleteDeviceNotification(Int32)

TcAdsClient.AdsNotification

TcAdsClient.AdsNotificationEx