DynamicSymbol.ValueChanged Event
Occurs when the (Primitive) value of the IValueSymbol has changed.
Namespace: TwinCAT.TypeSystem
Assembly: TwinCAT.Ads (in TwinCAT.Ads.dll)
Version: 6.0.328+39e3229
Syntax
C#
public event EventHandler<ValueChangedEventArgs> ValueChanged
Remarks
Examples
Use Dynamic Notifications
using System;
using System.Diagnostics;
using System.Threading;
using TwinCAT;
using TwinCAT.Ads;
using TwinCAT.Ads.TypeSystem;
using TwinCAT.Ads.ValueAccess;
using TwinCAT.TypeSystem;
using TwinCAT.TypeSystem.Generic;
using TwinCAT.ValueAccess;
namespace Sample
{
class SymbolBrowserV2Notifications
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
// Parse the Command Line Parameters.
AmsAddress address = ArgParser.Parse(args);
#region DEFAULTNOTIFICATON_SAMPLE
// Create AdsClient object
using (AdsClient client = new AdsClient())
{
// No automatic Synchronization (necessary for Console applications without message loop)
//client.Synchronize = false;
// Connect to client
client.Connect(address);
// Usage of 'dynamic' type/symbol loader
SymbolLoaderSettings settings = new SymbolLoaderSettings(SymbolsLoadMode.DynamicTree, ValueAccessMode.IndexGroupOffsetPreferred);
IAdsSymbolLoader dynLoader = (IAdsSymbolLoader)SymbolLoaderFactory.Create(client, settings);
// Set the DefaultNotification Properties
dynLoader.DefaultNotificationSettings = new NotificationSettings(AdsTransMode.ClientOnChange, 200, 2000);
// Determine the symbols
dynamic dynamicSymbols = ((IDynamicSymbolLoader)dynLoader).SymbolsDynamic;
// Task 1 Symbol (build in symbol)
dynamic task1Symbol = dynamicSymbols._TaskInfo[1];
// CycleCount Symbol
dynamic cycleCountSymbol = task1Symbol.CycleCount;
// Override Notification Setting for Cycle Count Symbol
cycleCountSymbol.NotificationSettings = new NotificationSettings(AdsTransMode.OnChange, 250, 0);
// Register Dynamic Value Changed event.
cycleCountSymbol.ValueChanged += new EventHandler<ValueChangedEventArgs>(cycleCount_ValueChanged);
#endregion
// Sleep main thread to receive notifications
Thread.Sleep(10000);
#region DEFAULTNOTIFICATON_SAMPLE
}
#endregion
Console.WriteLine("CycleCount Changed events received: {0}", _cycleCountEvents);
Console.WriteLine("");
Console.WriteLine("Press [Enter] for leave:");
Console.ReadLine();
}
/// <summary>
/// The cycle count event counter
/// </summary>
static int _cycleCountEvents = 0;
/// <summary>
/// Handler function for CycleCount changed events.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="args">Event arguments.</param>
static void cycleCount_ValueChanged(object sender, ValueChangedEventArgs args)
{
Interlocked.Increment(ref _cycleCountEvents);
// Use Value as dynamic (type safe: INT) object.
dynamic val = args.Value;
int intVal = val;
DateTimeOffset changedTime = args.DateTime.ToLocalTime(); // Convert UTC to local time
Console.WriteLine("CycleCount changed to: {0}, TimeStamp: {1}", intVal, changedTime.ToString("HH:mm:ss:fff"));
}
}
}