TcAdsClient.AddDeviceNotificationEx Method (UInt32, UInt32, AdsTransMode, Int32, Int32, Object, Type, .Int32.)

Connects a variable to the ADS client. The ADS client will be notified by the AdsNotification event. If type is a string type, the first element of the parameter args specifies the number of characters of the string. If type is an array type, the number of elements for each dimension has to be specified in the parameter args. At the moment only 1 dimensional Arrays are supported.

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

Syntax

C#

public int AddDeviceNotificationEx(
    uint indexGroup,
    uint indexOffset,
    AdsTransMode transMode,
    int cycleTime,
    int maxDelay,
    Object userData,
    Type type,
    int[] args
)

VB

Public Function AddDeviceNotificationEx ( 
    indexGroup As UInteger,
    indexOffset As UInteger,
    transMode As AdsTransMode,
    cycleTime As Integer,
    maxDelay As Integer,
    userData As Object,
    type As Type,
    args As Integer()
) 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.

transMode

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

cycleTime

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

maxDelay

Type: System.Int32
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.

type

Type: System.Type
Type of the object stored in the event argument.

args

Type: .System.Int32.
Additional arguments.

Return Value

Type: Int32
The handle of the notification.

Implements

IAdsNotifications.AddDeviceNotificationEx(UInt32, UInt32, AdsTransMode, Int32, Int32, Object, Type, .Int32.)

Exceptions

Exception

Condition

AdsErrorException

Thrown when the ADS call fails.

Examples

The following sample shows how to use AdsNotificationEx events.

Receive AdsNotifications

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

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

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

    try
    {
        // Notification to a ZDINT Type (UINT32)
        // Check for change every 200 ms
        notificationHandle = client.AddDeviceNotificationEx("MAIN.nCounter",AdsTransMode.OnChange, 200, 0, null,typeof(uint));
        Thread.Sleep(5000); // Sleep the main thread to get some (asynchronous Notifications)
    }
    finally
    {
        // Unregister the Event / Handle
        client.DeleteDeviceNotification(notificationHandle);
        client.AdsNotificationEx -= Client_AdsNotification;
    }
    }
}

private void Client_AdsNotification(object sender, AdsNotificationExEventArgs e)
{
    // Or here we know about UDINT type --> can be marshalled as UINT32
    uint nCounter = (uint)e.Value;
}

Reference

TcAdsClient Class

AddDeviceNotificationEx Overload

TwinCAT.Ads Namespace

TcAdsClient.DeleteDeviceNotification(Int32)

TcAdsClient.AdsNotification

TcAdsClient.AdsNotificationEx