DynamicSymbol.ValueChanged Event
Occurs when the (Primitive) value of the IValueSymbol has changed.
Namespace: TwinCAT.TypeSystem
Assembly: TwinCAT.Ads (in TwinCAT.Ads.dll)
Version: 4.3.0.0
Syntax
C#
public event EventHandler<ValueChangedArgs> ValueChanged
VB
Public Event ValueChanged As EventHandler(Of ValueChangedArgs)
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;
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 (TcAdsClient client = new TcAdsClient())
{
// 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.TwinCAT_SystemInfoVarList._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<ValueChangedArgs>(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, ValueChangedArgs args)
{
Interlocked.Increment(ref _cycleCountEvents);
// Use Value as dynamic (type safe: UINT) object.
dynamic val = args.Value;
uint intVal = val;
DateTime changedTime = args.UtcRtime.ToLocalTime(); // Convert UTC to local time
Console.WriteLine("CycleCount changed to: {0}, TimeStamp: {1}", intVal, changedTime.ToString("HH:mm:ss:fff"));
}
}
}