DynamicSymbol.ReadValue Method
Reads the value of this DynamicSymbol.
Namespace: TwinCAT.TypeSystem
Assembly: TwinCAT.Ads (in TwinCAT.Ads.dll)
Version: 4.3.0.0
Syntax
C#
public Object ReadValue()
VB
Public Function ReadValue As Object
Return Value
Type: Object
System.Object.
Remarks
Examples
Dynamic Read access
namespace Sample
{
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;
class SymbolBrowserProgramV2DynamicTree
{
#region CODE_SAMPLE_SIMPLEDYNAMIC
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
// Get the AdsAddress from command-line arguments
AmsAddress address = ArgParser.Parse(args);
using (TcAdsClient client = new TcAdsClient())
{
// Connect to the target device
client.Connect(address);
// Usage of "dynamic" Type and Symbols (>= .NET4 only)
SymbolLoaderSettings settings = new SymbolLoaderSettings(SymbolsLoadMode.DynamicTree);
IAdsSymbolLoader dynLoader = (IAdsSymbolLoader)SymbolLoaderFactory.Create(client, settings);
#endregion
// Set the Default setting for Notifications
dynLoader.DefaultNotificationSettings = new NotificationSettings(AdsTransMode.OnChange, 200, 2000);
// Get the Symbols (Dynamic Symbols)
dynamic dynamicSymbols = ((IDynamicSymbolLoader)dynLoader).SymbolsDynamic;
dynamic adsPort = dynamicSymbols.TwinCAT_SystemInfoVarList._AppInfo.AdsPort;
#region CODE_SAMPLE_SIMPLEDYNAMIC
// Access Main Symbol with Dynamic Language Runtime support (DLR)
// Dynamically created property "Main"
//dynamic symMain = dynamicSymbols.Main;
// Main is an 'VirtualSymbol' / Organizational unit that doesn't have a value
// Calling ReadValue is not allowed
//bool test = symMain.HasValue;
//dynamic invalid = symMain.ReadValue();
//Reading TaskInfo Value
//With calling ReadValue() a 'snapshot' of the Symbols Instance is taken
dynamic vTaskInfoArray = dynamicSymbols.TwinCAT_SystemInfoVarList._TaskInfo.ReadValue();
// Getting the Snapshot time in UTC format
DateTime timeStamp1 = vTaskInfoArray.UtcTimeStamp;
// Getting TaskInfo Symbol for Task 1
dynamic symTaskInfo1 = dynamicSymbols.TwinCAT_SystemInfoVarList._TaskInfo[1];
// Getting CycleCount Symbol
dynamic symCycleCount = symTaskInfo1.CycleCount;
// Take Snapshot value of the ApplicationInfo struct
dynamic vAppInfo = dynamicSymbols.TwinCAT_SystemInfoVarList._AppInfo.ReadValue();
// Get the UTC Timestamp of the snapshot
DateTime timeStamp2 = vAppInfo.UtcTimeStamp;
// Access the ProjectName of the ApplicationInfo Snapshot (type-safe!)
string projectNameValue = vAppInfo.ProjectName;
// Reading the CycleCount Value
uint cycleCountValue = symTaskInfo1.CycleCount.ReadValue(); // Taking a Value Snapshot
#endregion
// Registering for dynamic "ValueChanged" events for the Values
// Using Default Notification settings
symCycleCount.ValueChanged += new EventHandler<ValueChangedArgs>(cycleCount_ValueChanged);
// Override default notification settings
symTaskInfo1.NotificationSettings = new NotificationSettings(AdsTransMode.Cyclic,500,0);
// Register for ValueChanged event.
symTaskInfo1.ValueChanged += new EventHandler<ValueChangedArgs>(taskInfo1Value_ValueChanged); // Struct Type
Thread.Sleep(10000); // Sleep main thread for 10 Seconds
}
Console.WriteLine("CycleCount Changed events received: {0}",_cycleCountEvents);
Console.WriteLine("taskInfo1 Changed events received: {0}", _taskInfo1Events);
Console.WriteLine("");
Console.WriteLine("Press [Enter] for leave:");
Console.ReadLine();
}
static object _notificationSynchronizer = new object();
static int _cycleCountEvents = 0;
/// <summary>
/// Handler function for the CycleCount ValueChanged event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
static void cycleCount_ValueChanged(object sender, ValueChangedArgs e)
{
lock(_notificationSynchronizer)
{
Interlocked.Increment(ref _cycleCountEvents);
// val is a type safe value of int!
dynamic val = e.Value;
uint intVal = val;
DateTime changedTime = e.UtcRtime.ToLocalTime(); // Convert UTC to local time
Console.WriteLine("CycleCount changed to: {0}, TimeStamp: {1}", intVal, changedTime.ToString("HH:mm:ss:fff"));
}
}
static int _taskInfo1Events = 0;
/// <summary>
/// Handler function for the TaskInfo ValueChanged event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
static void taskInfo1Value_ValueChanged(object sender, ValueChangedArgs e)
{
lock (_notificationSynchronizer)
{
Interlocked.Increment(ref _taskInfo1Events);
dynamic val = e.Value;
DateTime changedTime = e.UtcRtime.ToLocalTime(); // Convert to local time
// Val is a during Runtime created struct type and contains
// the same Properties as related PLC object.
int cycleTime = val.CycleTime;
Console.WriteLine("TaskInfo1Value changed TimeStamp: {0}", changedTime.ToString("HH:mm:ss:fff"));
}
}
}
}