AdsClient Class
ADS Client / ADS Communication object.
Inheritance Hierarchy
SystemObject
TwinCAT.AdsAdsClient
Namespace: TwinCAT.Ads
Assembly: TwinCAT.Ads (in TwinCAT.Ads.dll)
Version: 7.0.0+e56d35ccc4675faac24789a4aab60071fc61d470
Syntax
C#
public sealed class AdsClient : IAdsDisposableConnection,
IAdsConnectAddress, IAdsConnection, IConnection, IConnectionStateProvider, IAdsNotifications,
IAdsSymbolicAccess, IAdsAnyAccess, IAdsHandle, IAdsReadWrite, IAdsStateProvider,
IAdsStateControl, IAdsSymbolChangedProvider, IAdsRpcInvoke, IRouterNotificationProvider, IDisposable,
ITcAdsRawThe AdsClient type exposes the following members.
Constructors
|
|
Name |
Description |
|---|---|---|
|
|
Initializes a new instance of the AdsClient class. | |
|
|
Initializes a new instance of the AdsClient class with the specified settings. | |
|
|
Initializes a new instance of the AdsClient class. | |
|
|
Initializes a new instance of the AdsClient class. | |
|
|
Initializes a new instance of the AdsClient class bound to a session. | |
|
|
Initializes a new instance of the AdsClient class. | |
|
|
AdsClient(ISession, AdsClientSettings, IConfiguration, ILoggerFactory) |
Initializes a new instance of the AdsClient class. |
Properties
|
|
Name |
Description |
|---|---|---|
|
|
Gets the target AmsAddress of of the established ADS connection (Destination side). | |
|
|
Gets the channel port type. | |
|
|
Gets the the used channel protocol. | |
|
|
Get the AmsAddress of the
ADS client. | |
|
|
Gets the optional Configuration | |
|
|
Gets the default value encoding. | |
|
|
Gets the AdsClient Identifier. | |
|
|
Gets a value indicating whether the local ADS port was opened successfully. It does not indicate if the target port is available. Use the method ReadState to determine if the target port is available. | |
|
|
Gets a value indicating whether this instance is disposed. | |
|
|
Gets a value indicating whether the ADS client is connected to a ADS Server on the local computer. | |
|
|
Gets the logger inteface. | |
|
|
Gets the logger factory. | |
|
|
Gets the target platform pointer size | |
|
|
Gets the session that initiated this IConnection | |
|
|
Get the client/source AmsAddress (Source side). | |
|
|
Gets the symbol encoding. | |
|
|
Sets the timeout for the ads communication. Unit is in ms. |
Methods
|
|
Name |
Description |
|---|---|---|
|
|
AddDeviceNotification(String, Int32, NotificationSettings, Object) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotification event. |
|
|
AddDeviceNotification(UInt32, UInt32, Int32, NotificationSettings, Object) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotification event. |
|
|
AddDeviceNotificationAsync(String, Int32, NotificationSettings, Object, CancellationToken) |
Connects a variable to the ADS client asynchronously. The ADS client will be notified by the AdsNotification event. |
|
|
AddDeviceNotificationAsync(UInt32, UInt32, Int32, NotificationSettings, Object, CancellationToken) |
Adds a device notification as an asynchronous operation. |
|
|
AddDeviceNotificationEx(String, NotificationSettings, Object, Type) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotificationEx event. |
|
|
AddDeviceNotificationEx(String, NotificationSettings, Object, Type, Int32) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotificationEx event. |
|
|
AddDeviceNotificationEx(UInt32, UInt32, NotificationSettings, Object, Type) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotificationEx event. |
|
|
AddDeviceNotificationEx(UInt32, UInt32, NotificationSettings, 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. Only primitive ('AnyType') types are allowed for the parameter type. |
|
|
AddDeviceNotificationExAsync(String, NotificationSettings, Object, Type, Int32, CancellationToken) |
Connects a variable to the ADS client asynchronously. The ADS client will be notified by the AdsNotificationEx event. |
|
|
Connects a variable to the ADS client asynchronously. The ADS client will be notified by the AdsNotificationEx event. | |
|
|
Clears the internal symbol cache. | |
|
|
Closes this AdsClient | |
|
|
Connects/Reconnects to the specified target address. | |
|
|
Connects the specified port. | |
|
|
Connects/Reconnects to the specified port of the local ADS target device. | |
|
|
Connects/Reconnects to the specified target address. | |
|
|
Connects the specified net identifier. | |
|
|
Connects/Reconnects to the specified target address. | |
|
|
Connects/Reconnects to the target address and waits until the IAdsConnection is disconnected asynchronously. | |
|
|
Connects/Reconnects to the specified target address asynchronously. | |
|
|
Connects/Reconnects to the specified port of the local system asynchonously. | |
|
|
Connects/Reconnects to the specified target address asynchronously. | |
|
|
Determines the Symbol handle by its instance path synchronously. | |
|
|
Determines the Symbol handle by its instance path asynchronously. | |
|
|
Deletes a registered notification. | |
|
|
Deletes a registered notification asynchronously. | |
|
|
Releases the specified symbol/variable handle synchronously. | |
|
|
Releases the specified symbol/variable handle asynchronously. | |
|
|
Disconnects this AdsClient from the local ADS router. | |
|
|
Disconnects this AdsClient from the local ADS router. | |
|
|
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | |
|
|
Determines whether the specified object is equal to the current
object. | |
|
|
Finalizes an instance of the AdsClient class. | |
|
|
Serves as the default hash function. | |
|
|
Gets the Type of the current
instance. | |
|
|
InvokeRpcMethodAsync(IRpcCallableInstance, IRpcMethod, Object, CancellationToken) |
invoke RPC method as an asynchronous operation. |
|
|
InvokeRpcMethodAsync(String, String, Object, CancellationToken) |
Invokes the specified RPC Method asynchronously |
|
|
invoke RPC method as an asynchronous operation. | |
|
|
InvokeRpcMethodAsync(String, String, Object, AnyTypeSpecifier, AnyTypeSpecifier, CancellationToken) |
Invokes the specified RPC Method asynchronously |
|
|
Reads any. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads any. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads any. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Reads any as result. | |
|
|
Reads any as result. | |
|
|
Reads any asynchronous. | |
|
|
Reads data synchronously from an ADS device and writes it to an object. | |
|
|
Read any as an asynchronous operation. | |
|
|
Read any as an asynchronous operation. | |
|
|
Reads data asynchronously from an ADS device and writes it to an object. | |
|
|
ReadAnyAsync(UInt32, UInt32, Type, Int32, CancellationToken) |
Read any as an asynchronous operation. |
|
|
Reads any asynchronous. | |
|
|
Read any as an asynchronous operation. | |
|
|
Read any as an asynchronous operation. | |
|
|
Read any as an asynchronous operation. | |
|
|
Read any as an asynchronous operation. | |
|
|
Read any as an asynchronous operation. | |
|
|
Reads any string. | |
|
|
Reads a string from the specified symbol/variable. | |
|
|
Reads as string from a specified address. | |
|
|
ReadAnyStringAsync(String, Int32, Encoding, CancellationToken) |
Reads a string asynchronously from the specified symbol/variable |
|
|
ReadAnyStringAsync(UInt32, Int32, Encoding, CancellationToken) |
Reads a string asynchronously from the specified symbol/variable |
|
|
ReadAnyStringAsync(UInt32, UInt32, Int32, Encoding, CancellationToken) |
Reads a string from a specified address asynchronously. |
|
|
Reads the value synchronously />. | |
|
|
Reads data synchronously from an ADS device. | |
|
|
Reads/Determines the DataType Inforrmation with the specifed name. | |
|
|
Read data type as an asynchronous operation. | |
|
|
Reads the identification and version number of an ADS server. | |
|
|
Reads the identification and version number of an ADS server. | |
|
|
Reads the ADS status and the device status from an ADS server. | |
|
|
Reads the ADS status and the device status from an ADS server. | |
|
|
Reads the symbol. | |
|
|
Read symbol as an asynchronous operation. | |
|
|
Reads the value of a symbol and returns the value as (boxed) object. | |
|
|
Reads the value of a symbol specified with its instance path and returns the value as boxed object. | |
|
|
Reads the value of a symbol specified with its instance path and returns the value as object of the specified type. | |
|
|
Reads the value of a symbol and returns it as an typed object. | |
|
|
Reads the value. | |
|
|
Read value as an asynchronous operation. | |
|
|
Read value as an asynchronous operation. | |
|
|
Read value as an asynchronous operation. | |
|
|
Read value as an asynchronous operation. | |
|
|
Writes data synchronously to an ADS device and then Reads data from that target. | |
|
|
Writes data synchronously to an ADS device and then Reads data from this device into the readBuffer | |
|
|
Writes data synchronously to an ADS device and reads data from that device. | |
|
|
Registers for AdsStateChanged events as an asynchronous operation. | |
|
|
Registers the symbol version changed event (synchronously) | |
|
|
Register symbol version changed as an asynchronous operation. | |
|
|
Resurrects the connection | |
|
|
Returns a string that represents the current object. | |
|
|
TryAddDeviceNotification(String, Int32, NotificationSettings, Object, UInt32) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotification event. |
|
|
TryAddDeviceNotification(UInt32, UInt32, Int32, NotificationSettings, Object, UInt32) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotification event. |
|
|
TryAddDeviceNotificationEx(String, NotificationSettings, Object, Type, Int32, UInt32) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotificationEx event. |
|
|
TryAddDeviceNotificationEx(UInt32, UInt32, NotificationSettings, Object, Type, Int32, UInt32) |
Connects a variable to the ADS client. The ADS client will be notified by the AdsNotificationEx event. |
|
|
Determines the Symbol handle by its instance path synchronously. | |
|
|
Deletes a registered notification. | |
|
|
Deletes a registered notification. | |
|
|
Releases the specified symbol/variable handle synchronously. | |
|
|
Invokes the specified RPC Method | |
|
|
Invokes the specified RPC Method | |
|
|
Tries the invoke RPC method. | |
|
|
TryInvokeRpcMethod(String, String, Object, AnyTypeSpecifier, AnyTypeSpecifier, Object, Object) |
Invokes the RPC Method |
|
|
Tries the read any string. | |
|
|
Tries to Read/Determine the DataType of the specified type. | |
|
|
Reads the ADS status and the device status from an ADS server. Unlike the ReadState method this method does not call an exception on failure. Instead an AdsErrorCode is returned. If the return value is equal to AdsErrorCode.NoError the call was successful. | |
|
|
Tries to read the symbol information object specified by the instance path. | |
|
|
Tries to read the value of a symbol and returns the value as boxed object. | |
|
|
Tries to the value of a symbol specified as instance path and returns the value as (boxed) object. | |
|
|
Reads the value of a symbol and returns the value as typed value. | |
|
|
Tries to reads the value of a symbol specified with instance path and returns the typed value. | |
|
|
ReadWrites value data synchronously to/from the symbol represented by the variableHandle. | |
|
|
TryReadWrite(UInt32, UInt32, MemoryByte, ReadOnlyMemoryByte, Int32) |
Writes data synchronously to an ADS device and reads data from that device. |
|
|
Resurrects the connection | |
|
|
Writes the value data synchronously that is represented in the writeBuffer to the symbol with the specified variableHandle. | |
|
|
Writes data synchronously to an ADS device. | |
|
|
Changes the ADS status and the device status of an ADS server. | |
|
|
Changes the ADS status and the device status of an ADS server. | |
|
|
Tries to write a (boxed) value to the symbol | |
|
|
Tries to write a (boxed) value to the symbol instance specified by its instance/symbol path. | |
|
|
Tries to write a value to the symbol. | |
|
|
Tries to Write a (typed) value to the symbol instance specified by its instance/symbol path. | |
|
|
unregister ads state changed as an asynchronous operation. | |
|
|
Unregisters the symbol version changed event (synchronously) | |
|
|
Unregister symbol version changed as an asynchronous operation. | |
|
|
Writes data synchronously to an ADS device. | |
|
|
Triggers a 'Write' call to the ADS device at the specified address. | |
|
|
Writes data synchronously to an ADS device. | |
|
|
Writes an object synchronously to an ADS device. | |
|
|
Writes an object synchronously to an ADS device. If the Type of the object to be written is a string type, the first element of parameter args specifies the number of characters of the string. | |
|
|
Writes an object synchronously to an ADS device. | |
|
|
Writes an object synchronously to an ADS device. | |
|
|
Writes an object synchronously to an ADS device. If the Type of the object to be written is a string type, the first element of parameter args specifies the number of characters of the string. | |
|
|
Writes an object asynchronously to an ADS device. If the Type of the object to be written is a string type, the first element of parameter args specifies the number of characters of the string. | |
|
|
Writes an object asynchronously to an ADS device. If the Type of the object to be written is a string type, the first element of parameter args specifies the number of characters of the string. | |
|
|
WriteAnyAsync(UInt32, UInt32, Object, Int32, CancellationToken) |
Writes an object asynchronously to an ADS device. If the Type of the object to be written is a string type, the first element of parameter args specifies the number of characters of the string. |
|
|
Writes the string (Potentially unsafe!) | |
|
|
Writes the data / Value into the specified writeBuffer. | |
|
|
Write as an asynchronous operation. | |
|
|
Writes the asynchronous. | |
|
|
WriteAsync(UInt32, UInt32, ReadOnlyMemoryByte, CancellationToken) |
Write as an asynchronous operation. |
|
|
Changes the ADS status and the device status of an ADS server. | |
|
|
Changes the ADS status and the device status of an ADS server. | |
|
|
Changes the ADS status and device status of the ADS server asynchronously. | |
|
|
WriteControlAsync(AdsState, UInt16, ReadOnlyMemoryByte, CancellationToken) |
Writes the state asynchronously |
|
|
Writes the passed object value to the specified ADS symbol.The parameter type must have the same layout as the ADS symbol. | |
|
|
Writes a (boxed) value to the symbol. | |
|
|
Writes a (boxed value) to the symbol instance specified by its instance/symbol path. | |
|
|
Writes a (typed) value to the symbol. | |
|
|
Writes a typed value to the symbol instance specified by its instance/symbol path. | |
|
|
Writes a (boxed) value to the symbol as an asynchronous operation. | |
|
|
Writes a (typed) value to the symbol as an asynchronous operation. | |
|
|
Writes a (typed) value to the symbol instance specified by its instance/symbol path as an asynchronous operation. |
Events
|
|
Name |
Description |
|---|---|---|
|
|
Occurs when the ADS device sends a notification to the client. | |
|
|
Occurs when a exception has occurred during notification management. | |
|
|
Occurs when the ADS devices sends a notification to the client. | |
|
|
Occurs when Notification Unregistrations / Invalidates are received from the AdsServer | |
|
|
Occurs when the ADS state changes. | |
|
|
Occurs when Notifications are send (bundled notifications) | |
|
|
Occurs when the SymbolVersion table of a TwinCAT PLC System has been changed. | |
|
|
Occurs when the connection state has been changed. | |
|
|
(Local) Router state changed event. |
Extension Methods
|
|
Name |
Description |
|---|---|---|
|
|
Gets an observable sequence of ResultReadAdsStates
via Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of ResultReadAdsStates
via Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of ResultReadAdsStates
via Polling. | |
|
|
Gets an observable sequence of ResultReadAdsStates
via Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of ResultReadAdsStates
via Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of AdsStates via
Polling. | |
|
|
Gets an observable sequence of ResultReadDeviceStates
via Polling. | |
|
|
Gets an observable sequence of ResultReadDeviceStates
via Polling. | |
|
|
Gets an observable sequence of ResultReadDeviceStates
via Polling. | |
|
|
Gets an observable sequence of ResultReadDeviceStates
via Polling. | |
|
|
Polls the state of the system service. | |
|
|
Polls the system service state asynchronously | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on time points where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on time points where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on time points where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on time points where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol as value sequence of object values with a
specified period time. | |
|
|
Polls the symbol values on timepoints where the polling
observable streams data / triggers | |
|
|
Polls the symbol values on time points where the polling
observable streams data / triggers | |
|
|
Reads the System Service state (AdsSysServState) | |
|
|
Reads the System Service state (AdsSysServState) (asynchronous) | |
|
|
Read with fallback. | |
|
|
Ads Read with Fallback. | |
|
|
Ads Read with Fallback. | |
|
|
Ads Read with fallback as an asynchronous operation. | |
|
|
Ads Read with fallback as an asynchronous operation. | |
|
|
Ads Read with fallback as an asynchronous operation. | |
|
|
Ads Read with Fallback. | |
|
|
ReadWrite with Fallback. | |
|
|
Ads Read with fallback as an asynchronous operation. | |
|
|
Ads Read with fallback as an asynchronous operation. | |
|
|
Repeated AdsRead. | |
|
|
Repeated AdsRead. | |
|
|
Repeated AdsRead as an asynchronous operation. | |
|
|
Repeated AdsRead as an asynchronous operation. | |
|
|
Repeated AdsRead. | |
|
|
Repeated AdsReadWrite as an asynchronous operation. | |
|
|
Repeated AdsWrie. | |
|
|
Repeated AdsReadWrite as an asynchronous operation. | |
|
|
Sends a SystemService Restart to the target system and waits
until the system has finished the state changes (SystemService,
Port 10000) (asynchronously) | |
|
|
Sends an AdsState/AdsStateCommand Request to an AdsServer and
waits until the StateChange is done or fails. | |
|
|
Sends an AdsState/AdsStateCommand Request to an AdsServer and
waits until the StateChange is done or fails (asynchronously) | |
|
|
Waits until the target AdsServer reaches the requested
state. | |
|
|
Waits until the target AdsServer reaches the requested state
(asynchronous) | |
|
|
Waits until the Restart is detected on the client
(SystemService, Port 10000) | |
|
|
Waits until the SystemService Restart is detectd on the client
(SystemService, Port 10000) (asynchronously) | |
|
|
Gets an observable sequence of AdsStates. | |
|
|
Gets an observable sequence of Notifications. | |
|
|
Gets an observable sequence of Notification
objects. | |
|
|
Gets an observable sequence of Notification
objects. | |
|
|
Gets an observable sequence of SymbolValueNotifications. | |
|
|
Creates an observable sequence of values that are created by
ADS Notifications. | |
|
|
Creates an observable sequence of values that are created by
ADS Notifications. | |
|
|
Gets an observable sequence of SymbolVersion changed
counts. | |
|
|
Gets an observable sequence of SymbolVersion changed
counts. | |
|
|
Observable sequence of Values driven by ADS Notifications on
the specified symbol. | |
|
|
Writes the sequence of values to the symbol specified by the
instance path. | |
|
|
Writes the sequence of values to the symbol specified by the
instance path. |
Remarks
The class AdsClient enables synchronous/asynchronous access to data of an ADS Device.
Example
The following sample shows how to instantiate and use the AdsClient class.
AdsClient Demo (async)
using System;
using System.Buffers.Binary;
using System.Threading;
using System.Threading.Tasks;
using TwinCAT.Ads;
using TwinCAT.TypeSystem;
namespace Sample
{
class ClientAsync
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static async void Main(string[] args)
{
AmsAddress address = ArgParser.Parse(args);
CancellationTokenSource cancelSource = new CancellationTokenSource();
CancellationToken cancel = cancelSource.Token;
using (AdsClient client = new AdsClient())
{
// Connect to Address
client.Connect(address.NetId, address.Port); // Connect to Port (851, first PLC by default)
// Read the identification and version number of the device
ResultDeviceInfo resultDeviceInfo = await client.ReadDeviceInfoAsync(cancel);
if (resultDeviceInfo.Succeeded)
{
DeviceInfo deviceInfo = resultDeviceInfo.DeviceInfo;
Version version = deviceInfo.Version.ConvertToStandard();
Console.WriteLine(string.Format("DeviceName: {0}", deviceInfo.Name));
Console.WriteLine(string.Format("DeviceVersion: {0}", version.ToString(3)));
}
// Read the state of the device
ResultReadDeviceState resultReadDeviceState = await client.ReadStateAsync(cancel);
AdsState state = AdsState.Invalid;
if (resultReadDeviceState.Succeeded)
{
StateInfo stateInfo = resultReadDeviceState.State;
state = stateInfo.AdsState;
short deviceState = stateInfo.DeviceState;
Console.WriteLine(string.Format("DeviceState: {0}", deviceState));
Console.WriteLine(string.Format("AdsState : {0}", state));
}
// Write ADS Commands (write state) to target
// Set PLC to Run
if (state == AdsState.Stop)
{
await client.WriteControlAsync(AdsState.Run, 0, cancel);
}
//create variable handle for Plc Project Name (automatic generated symbol in PLC)
ResultHandle resultHandle = await client.CreateVariableHandleAsync("TwinCAT_SystemInfoVarList._AppInfo.ProjectName", cancel);
if (resultHandle.Succeeded)
{
uint handleProjectName = resultHandle.Handle;
uint handleNotification = 0; // Notification Handle for Task1 CycleCount changes
try
{
// Read value from target and Marshal data into string
ResultReadBytes resultRead = await client.ReadAsync(handleProjectName, 256, cancel);
if (resultRead.Succeeded)
{
PrimitiveTypeMarshaler marshaler = PrimitiveTypeMarshaler.Default;
string projectName = null;
int unmarshaledBytes = marshaler.Unmarshal(resultRead.Data.Span, out projectName);
Console.WriteLine(string.Format("ProjectName : {0}", projectName));
}
//_notificationBuffer = new byte[4]; // Sizeof UDINT
int size = sizeof(UInt32); // Sizeof UDINT
client.AdsNotification += client_NotificationEvent; // Register for Notification event
//Register Notification for Task1 CycleCount symbol (automatic generated symbol in PLC)
resultHandle = await client.AddDeviceNotificationAsync("TwinCAT_SystemInfoVarList._TaskInfo[1].CycleCount", size, NotificationSettings.Default, null, cancel);
if (resultHandle.Succeeded)
{
handleNotification = resultHandle.Handle;
// Sleep 10 Seconds to receive events
await Task.Delay(10000, cancel); // Async Delay (some time for notifications)
}
}
finally
{
// Cleanup all handles
// Dispose all Streams
ResultAds result = await client.DeleteDeviceNotificationAsync(handleNotification, cancel);
// Always delete all variable handles.
result = await client.DeleteVariableHandleAsync(handleProjectName, cancel);
}
}
Console.WriteLine("");
Console.WriteLine("Press [Enter] for leave:");
Console.ReadLine();
}
}AdsClient Demo (sync)
using System;
using System.Buffers.Binary;
using System.Threading;
using System.Threading.Tasks;
using TwinCAT.Ads;
using TwinCAT.TypeSystem;
namespace Sample
{
class Client
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
AmsAddress address = ArgParser.Parse(args);
using (AdsClient client = new AdsClient())
{
// Connect to Address
client.Connect(address.NetId, address.Port); // Connect to Port (851, first PLC by default)
// Read the identification and version number of the device
DeviceInfo deviceInfo = client.ReadDeviceInfo();
Version version = deviceInfo.Version.ConvertToStandard();
Console.WriteLine(string.Format("DeviceName: {0}", deviceInfo.Name));
Console.WriteLine(string.Format("DeviceVersion: {0}", version.ToString(3)));
// Read the state of the device
StateInfo stateInfo = client.ReadState();
AdsState state = AdsState.Invalid;
state = stateInfo.AdsState;
short deviceState = stateInfo.DeviceState;
Console.WriteLine(string.Format("DeviceState: {0}", deviceState));
Console.WriteLine(string.Format("AdsState : {0}", state));
// Write ADS Commands (write state) to target
// Set PLC to Run
if (state == AdsState.Stop)
{
client.WriteControl(new StateInfo(AdsState.Run, 0));
}
//create variable handle for Plc Project Name (automatic generated symbol in PLC)
uint handleProjectName = client.CreateVariableHandle("TwinCAT_SystemInfoVarList._AppInfo.ProjectName");
uint handleNotification = 0; // Notification Handle for Task1 CycleCount changes
try
{
// Read value from target and Marshal data into string
var readResult = client.ReadAsResult(handleProjectName, 256);
readResult.ThrowOnError(); // Throw if error
PrimitiveTypeMarshaler marshaler = PrimitiveTypeMarshaler.Default;
string projectName = null;
int unmarshaledBytes = marshaler.Unmarshal(readResult.Data.Span, client.DefaultValueEncoding, out projectName);
Console.WriteLine(string.Format("ProjectName : {0}", projectName));
//_notificationBuffer = new byte[4]; // Sizeof UDINT
int size = sizeof(UInt32); // Sizeof UDINT
client.AdsNotification += client_NotificationEvent; // Register for Notification event
//Register Notification for Task1 CycleCount symbol (automatic generated symbol in PLC)
handleNotification = client.AddDeviceNotification("TwinCAT_SystemInfoVarList._TaskInfo[1].CycleCount", size, NotificationSettings.Default, null);
// Sleep 10 Seconds to receive events
Thread.Sleep(10000); // Sleep to get notifications
}
finally
{
// Cleanup all handles
// Dispose all Streams
client.DeleteDeviceNotification(handleNotification);
// Always delete all variable handles.
client.DeleteVariableHandle(handleProjectName);
}
Console.WriteLine("");
Console.WriteLine("Press [Enter] for leave:");
Console.ReadLine();
}
}Argument Parser
public static class ArgParser
{
/// <summary>
/// Parses the arguments.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns>AmsAddress.</returns>
public static AmsAddress Parse(string[] args)
{
AmsNetId netId = AmsNetId.Local;
int port = 851;
if (args != null)
{
if (args.Length > 0 && args[0] != null)
netId = AmsNetId.Parse(args[0]);
if (args.Length > 1 && args[1] != null)
port = int.Parse(args[1]);
}
return new AmsAddress(netId, port);
}
}The following sample shows how to call (Remote Procedures / Methods) within the PLC directly from the AdsClient class.
RPC Call Example (async)
namespace Sample
{
using System;
using System.Diagnostics;
using System.Threading;
using TwinCAT.Ads;
using TwinCAT.TypeSystem;
class RpcCallAsync
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static async void Main(string[] args)
{
//Parse the AmsAddress from command-line arguments
AmsAddress address = ArgParser.Parse(args);
// Create the ADS Client
using (AdsClient client = new AdsClient())
{
// Establish Connection
client.Connect(address);
// Call a Method that has the following signature (within the FunctionBlock instance 'fbInst')
/* {attribute 'TcRpcEnable'}
METHOD PUBLIC M_Add : INT
VAR_INPUT
i1 : INT := 0;
i2 : INT := 0;
END_VAR
*/
ResultRpcMethod result1 = await client.InvokeRpcMethodAsync("fbInst", "M_Add", new object[] { (short)1, (short)4 }, CancellationToken.None);
short result = (short)result1.ReturnValue;
// Call a Method that has no parameter and returns VOID
await client.InvokeRpcMethodAsync("fbInst", "M_Method1", new object[] { },CancellationToken.None);
}
}
}
}RPC Call Example (sync)
namespace Sample
{
using System;
using System.Diagnostics;
using TwinCAT.Ads;
using TwinCAT.TypeSystem;
class RpcCall
{
/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
//Parse the AmsAddress from command-line arguments
AmsAddress address = ArgParser.Parse(args);
// Create the ADS Client
using (AdsClient client = new AdsClient())
{
// Establish Connection
client.Connect(address);
// Call a Method that has the following signature (within the FunctionBlock instance 'fbInst')
/* {attribute 'TcRpcEnable'}
METHOD PUBLIC M_Add : INT
VAR_INPUT
i1 : INT := 0;
i2 : INT := 0;
END_VAR
*/
short result = (short)client.InvokeRpcMethod("fbInst", "M_Add", new object[] { (short)1, (short)4 });
// Call a Method that has no parameter and returns VOID
client.InvokeRpcMethod("fbInst", "M_Method1", new object[] { });
}
}
}
}Add a custom logger
static async void Main(string[] args)
{
CancellationTokenSource cancelSource = new CancellationTokenSource();
CancellationToken cancel = cancelSource.Token;
var address = new AmsAddress(AmsNetId.Local, AmsPort.SystemService);
var sessionSettings = SessionSettings.Default;
//https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line
// Create a configuration for the custom logger
var loggerConfig = new AdsLoggerConfiguration
{
LogLevel = LogLevel.Information,
EventId = 0
};
// Create an ILoggerFactory and add the custom logger provider
var loggerFactory = LoggerFactory.Create(builder =>
{
//builder.SetMinimumLevel(LogLevel.Information)
builder.AddProvider(new AdsLoggerProvider(() => loggerConfig))
.AddConsole(); // Adds the console
});
// Use AdsClient with custom logger
using (AdsClient client = new AdsClient(loggerFactory))
{
// Connect
client.Connect(address);
}
// Use AdsSession with custom logger
using (AdsSession session = new AdsSession(address, sessionSettings, loggerFactory))
{
// Connect
session.Connect();
}
}- AdsClient.Address Property
- AdsClient.ClientAddress Property
- AdsClient.DefaultValueEncoding Property
- AdsClient.Id Property
- AdsClient.IsConnected Property
- AdsClient.IsDisposed Property
- AdsClient.IsLocal Property
- AdsClient.Session Property
- AdsClient.SymbolEncoding Property
- AdsClient.Timeout Property
- AdsClient.Logger Property
- AdsClient.AddDeviceNotification Method (String, Int32, NotificationSettings, Object)
- AdsClient.AdsNotification Event
- AdsClient.AddDeviceNotification Method (UInt32, UInt32, Int32, NotificationSettings, Object)
- AdsClient.AddDeviceNotificationAsync Method (String, Int32, NotificationSettings, Object, CancellationToken)
- AdsClient.AddDeviceNotificationAsync Method (UInt32, UInt32, Int32, NotificationSettings, Object, CancellationToken)
- AdsClient.AddDeviceNotificationEx Method (String, NotificationSettings, Object, Type)
- AdsClient.AdsNotificationEx Event
- AdsClient.AddDeviceNotificationEx Method (String, NotificationSettings, Object, Type, .Int32.)
- AdsClient.AddDeviceNotificationEx Method (UInt32, UInt32, NotificationSettings, Object, Type)
- AdsClient.AddDeviceNotificationEx Method (UInt32, UInt32, NotificationSettings, Object, Type, .Int32.)
- AdsClient.AddDeviceNotificationExAsync Method (String, NotificationSettings, Object, Type, .Int32., CancellationToken)
- AdsClient.AddDeviceNotificationExAsync Method (UInt32, UInt32, NotificationSettings, Object, Type, .Int32., CancellationToken)
- AdsClient.CleanupSymbolTable Method
- AdsClient.Close Method
- AdsClient.Connect Method (Int32)
- AdsClient.Connect Method (AmsAddress)
- AdsClient.Connect Method (AmsPort)
- AdsClient.Connect Method (String, Int32)
- AdsClient.Connect Method (AmsNetId, Int32)
- AdsClient.Connect Method (AmsNetId, AmsPort)
- AdsClient.ConnectAndWaitAsync Method
- AdsClient.CreateVariableHandle Method
- AdsClient.CreateVariableHandleAsync Method
- AdsClient.DeleteDeviceNotification Method
- AdsClient.DeleteDeviceNotificationAsync Method
- AdsClient.DeleteVariableHandle Method
- AdsClient.DeleteVariableHandleAsync Method
- AdsClient.Disconnect Method
- AdsClient.Dispose Method
- AdsClient.Finalize Method
- AdsClient.InvokeRpcMethodAsync Method (String, String, .Object., CancellationToken)
- AdsClient.InvokeRpcMethodAsync Method (String, String, .Object., .AnyTypeSpecifier., AnyTypeSpecifier, CancellationToken)
- AdsClient.InvokeRpcMethodAsync Method (IRpcCallableInstance, IRpcMethod, .Object., .AnyTypeSpecifier., AnyTypeSpecifier, CancellationToken)
- AdsClient.ReadAny Method (UInt32, Type)
- AdsClient.ReadAny Method (UInt32, Type, .Int32.)
- AdsClient.ReadAny Method (UInt32, UInt32, Type)
- AdsClient.ReadAny Method (UInt32, UInt32, Type, .Int32.)
- AdsClient.ReadAny.T. Method (UInt32)
- AdsClient.ReadAny.T. Method (UInt32, .Int32.)
- AdsClient.ReadAny.T. Method (UInt32, UInt32)
- AdsClient.ReadAny.T. Method (UInt32, UInt32, .Int32.)
- AdsClient.ReadAnyAsync Method (UInt32, Type, CancellationToken)
- AdsClient.ReadAnyAsync Method (UInt32, Type, .Int32., CancellationToken)
- AdsClient.ReadAnyAsync Method (UInt32, UInt32, Type, CancellationToken)
- AdsClient.ReadAnyAsync Method (UInt32, UInt32, Type, .Int32., CancellationToken)
- AdsClient.ReadAnyAsync.T. Method (UInt32, CancellationToken)
- AdsClient.ReadAnyAsync.T. Method (UInt32, .Int32., CancellationToken)
- AdsClient.ReadAnyAsync.T. Method (UInt32, UInt32, CancellationToken)
- AdsClient.ReadAnyAsync.T. Method (UInt32, UInt32, .Int32., CancellationToken)
- AdsClient.ReadAnyString Method (UInt32, Int32, Encoding)
- AdsClient.ReadAnyString Method (UInt32, UInt32, Int32, Encoding)
- AdsClient.ReadAnyStringAsync Method (UInt32, Int32, Encoding, CancellationToken)
- AdsClient.ReadAnyStringAsync Method (UInt32, UInt32, Int32, Encoding, CancellationToken)
- AdsClient.ReadDataType Method
- AdsClient.ReadDataTypeAsync Method
- AdsClient.ReadDeviceInfo Method
- AdsClient.ReadDeviceInfoAsync Method
- AdsClient.ReadState Method
- AdsClient.ReadStateAsync Method
- AdsClient.ReadSymbol Method
- AdsClient.ReadSymbolAsync Method
- AdsClient.ReadValue Method (ISymbol)
- AdsClient.ReadValue Method (String, Type)
- AdsClient.ReadValue.T. Method (String)
- AdsClient.ReadValue.T. Method (ISymbol)
- AdsClient.ReadValueAsync Method (ISymbol, CancellationToken)
- AdsClient.ReadValueAsync Method (String, Type, CancellationToken)
- AdsClient.ReadValueAsync.TValue. Method (String, CancellationToken)
- AdsClient.ReadValueAsync.TValue. Method (ISymbol, CancellationToken)
- AdsClient.RegisterAdsStateChangedAsync Method
- AdsClient.AdsStateChanged Event
- AdsClient.RegisterSymbolVersionChangedAsync Method
- AdsClient.AdsSymbolVersionChanged Event
- AdsClient.TryAddDeviceNotification Method (String, Int32, NotificationSettings, Object, UInt32.)
- AdsClient.TryAddDeviceNotification Method (UInt32, UInt32, Int32, NotificationSettings, Object, UInt32.)
- IAdsNotifications.AdsNotification Event
- AdsClient.TryAddDeviceNotificationEx Method (String, NotificationSettings, Object, Type, .Int32., UInt32.)
- AdsClient.TryAddDeviceNotificationEx Method (UInt32, UInt32, NotificationSettings, Object, Type, .Int32., UInt32.)
- IAdsNotifications.AdsNotificationEx Event
- AdsClient.TryCreateVariableHandle Method
- AdsClient.TryDeleteDeviceNotification Method (UInt32)
- AdsClient.TryDeleteDeviceNotification Method (UInt32, Int32)
- AdsClient.TryDeleteVariableHandle Method
- AdsClient.TryInvokeRpcMethod Method (String, String, .Object., Object.)
- AdsClient.TryInvokeRpcMethod Method (String, String, .Object., .Object.., Object.)
- AdsClient.TryInvokeRpcMethod Method (String, String, .Object., .AnyTypeSpecifier., AnyTypeSpecifier, .Object.., Object.)
- AdsClient.TryInvokeRpcMethod Method (IRpcCallableInstance, IRpcMethod, .Object., .AnyTypeSpecifier., AnyTypeSpecifier, .Object.., Object.)
- AdsClient.TryReadDataType Method
- AdsClient.TryReadState Method
- AdsClient.TryReadSymbol Method
- AdsClient.TryReadValue Method (ISymbol, Object.)
- AdsClient.TryReadValue Method (String, Type, Object.)
- AdsClient.TryReadValue.T. Method (String, T.)
- AdsClient.TryReadValue.T. Method (ISymbol, T.)
- AdsClient.TryResurrect Method
- AdsClient.TryWriteControl Method (StateInfo)
- AdsClient.TryWriteValue Method (String, Object)
- AdsClient.TryWriteValue Method (ISymbol, Object)
- AdsClient.TryWriteValue.T. Method (String, T)
- AdsClient.TryWriteValue.T. Method (ISymbol, T)
- AdsClient.UnregisterAdsStateChangedAsync Method
- AdsClient.UnregisterSymbolVersionChangedAsync Method
- AdsClient.Write Method (UInt32, UInt32)
- AdsClient.WriteAny Method (UInt32, Object)
- AdsClient.WriteAny Method (UInt32, Object, .Int32.)
- AdsClient.WriteAny Method (UInt32, UInt32, Object)
- AdsClient.WriteAny Method (UInt32, UInt32, Object, .Int32.)
- AdsClient.WriteAnyAsync Method (UInt32, Object, CancellationToken)
- AdsClient.WriteAnyAsync Method (UInt32, Object, .Int32., CancellationToken)
- AdsClient.WriteAnyAsync Method (UInt32, UInt32, Object, CancellationToken)
- AdsClient.WriteAnyAsync Method (UInt32, UInt32, Object, .Int32., CancellationToken)
- AdsClient.WriteAnyStringAsync Method (String, String, Int32, Encoding, CancellationToken)
- AdsClient.WriteAnyStringAsync Method (UInt32, String, Int32, Encoding, CancellationToken)
- AdsClient.WriteAsync Method (UInt32, UInt32, CancellationToken)
- AdsClient.WriteControl Method (StateInfo)
- AdsClient.WriteControlAsync Method (AdsState, UInt16, CancellationToken)
- AdsClient.WriteSymbolAsync Method
- AdsClient.WriteValue Method (String, Object)
- AdsClient.WriteValue Method (ISymbol, Object)
- AdsClient.WriteValue.T. Method (String, T)
- AdsClient.WriteValue.T. Method (ISymbol, T)
- AdsClient.WriteValueAsync Method (ISymbol, Object, CancellationToken)
- AdsClient.WriteValueAsync.T. Method (String, T, CancellationToken)
- AdsClient.WriteValueAsync.T. Method (ISymbol, T, CancellationToken)
- AdsClient.InvokeRpcMethodAsync Method (IRpcCallableInstance, IRpcMethod, .Object., CancellationToken)
- AdsClient.Read Method (UInt32, Memory.Byte.)
- AdsClient.Read Method (UInt32, UInt32, Memory.Byte.)
- AdsClient.ReadAsync Method (UInt32, Memory.Byte., CancellationToken)
- AdsClient.ReadAsync Method (UInt32, UInt32, Memory.Byte., CancellationToken)
- AdsClient.ReadWrite Method (UInt32, Memory.Byte., ReadOnlyMemory.Byte.)
- AdsClient.ReadWrite Method (UInt32, UInt32, Memory.Byte., ReadOnlyMemory.Byte.)
- AdsClient.ReadWriteAsync Method (UInt32, Memory.Byte., ReadOnlyMemory.Byte., CancellationToken)
- AdsClient.ReadWriteAsync Method (UInt32, UInt32, Memory.Byte., ReadOnlyMemory.Byte., CancellationToken)
- AdsClient.TryRead Method (UInt32, Memory.Byte., Int32.)
- AdsClient.TryRead Method (UInt32, UInt32, Memory.Byte., Int32.)
- AdsClient.TryReadWrite Method (UInt32, Memory.Byte., ReadOnlyMemory.Byte., Int32.)
- AdsClient.TryReadWrite Method (UInt32, UInt32, Memory.Byte., ReadOnlyMemory.Byte., Int32.)
- AdsClient.TryWrite Method (UInt32, ReadOnlyMemory.Byte.)
- AdsClient.TryWrite Method (UInt32, UInt32, ReadOnlyMemory.Byte.)
- AdsClient.TryWriteControl Method (StateInfo, ReadOnlyMemory.Byte.)
- AdsClient.Write Method (UInt32, ReadOnlyMemory.Byte.)
- AdsClient.Write Method (UInt32, UInt32, ReadOnlyMemory.Byte.)
- AdsClient.WriteAsync Method (UInt32, ReadOnlyMemory.Byte., CancellationToken)
- AdsClient.WriteAsync Method (UInt32, UInt32, ReadOnlyMemory.Byte., CancellationToken)
- AdsClient.WriteControl Method (StateInfo, ReadOnlyMemory.Byte.)
- AdsClient.WriteControlAsync Method (AdsState, UInt16, ReadOnlyMemory.Byte., CancellationToken)
- AdsClient.RegisterSymbolVersionChanged Method
- AdsClient.UnregisterSymbolVersionChanged Method
- AdsClient.AdsNotification Event
- AdsClient.AdsNotificationError Event
- AdsClient.AdsNotificationEx Event
- AdsClient.AdsStateChanged Event
- AdsClient.AdsSumNotification Event
- AdsClient.AdsSymbolVersionChanged Event
- AdsClient.ConnectionStateChanged Event
- AdsClient.RouterStateChanged Event
- AdsClient.AdsNotificationsInvalidated Event
- AdsClient Constructor (AdsClientSettings)
- AdsClient Constructor (ILogger)
- AdsClient Constructor (ISession, ILogger)
- AdsClient Constructor (ISession, AdsClientSettings, ILogger)