AdsSession Class

AdsSession class

Inheritance Hierarchy

System.Object
  TwinCAT.Session
    TwinCAT.Ads.AdsSession
Namespace:  TwinCAT.Ads
Assembly:  TwinCAT.Ads (in TwinCAT.Ads.dll) Version: 4.3.0.0

Syntax

C#

public class AdsSession : Session, IAdsSession, 
    ISession, IConnectionStateProvider

VB

Public Class AdsSession
    Inherits Session
    Implements IAdsSession, ISession, IConnectionStateProvider

The AdsSession type exposes the following members.

Constructors

 

Name

Description

AdsSession(AmsAddress)

Initializes a new instance of the AdsSession class.

AdsSession(AmsAddress, SessionSettings)

Initializes a new instance of the AdsSession class.

AdsSession(AmsNetId, Int32)

Initializes a new instance of the AdsSession class.

AdsSession(AmsAddress, SessionSettings, Object)

Initializes a new instance of the AdsSession class.

AdsSession(AmsNetId, Int32, SessionSettings)

Initializes a new instance of the AdsSession class.

Properties

 

Name

Description

Address

Gets the target address of the AdsSession

AddressSpecifier

Gets the communication endpoint address string representation. (Inherited from Session.)

Connection

Gets the connection.

ConnectionState

Gets the current Connection state of the Session (Inherited from Session.)

Disposed

Gets a value indicating whether this Session is disposed. (Inherited from Session.)

EstablishedAt

Gets the UTC time when the session was established. (Inherited from Session.)

Id

Gets the Session Identifier (Inherited from Session.)

IsConnected

Gets a value indicating whether this instance is connected. (Inherited from Session.)

Name

Gets the name of the session (Inherited from Session.)

NetId

Gets the NetId of the Session

Owner

Gets the Session owner.

Port

Gets the Ams Port of the Session

Settings

Gets the settings of the connection.

Statistics

Gets the Communication / Session statistics.

SymbolServer

Gets the symbol server. (Inherited from Session.)

Methods

 

Name

Description

Close

Closes this ISession (Inherited from Session.)

Connect

Connects the session. (Inherited from Session.)

Disconnect

Disconnects the session from the target. (Inherited from Session.)

Dispose.

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. (Inherited from Session.)

Dispose(Boolean)

Releases unmanaged and - optionally - managed resources. (Overrides Session.Dispose(Boolean).)

Equals

Determines whether the specified object is equal to the current object. (Inherited from Object.)

Finalize

Finalizes an instance of the AdsSession class. (Overrides Object.Finalize..)

GetHashCode

Serves as the default hash function. (Inherited from Object.)

GetSessionName

Gets the name/string identifier of the session. (Overrides Session.GetSessionName..)

GetType

Gets the Type of the current instance. (Inherited from Object.)

MemberwiseClone

Creates a shallow copy of the current Object. (Inherited from Object.)

OnConnect

Handler function connecting the Session. (Overrides Session.OnConnect(Boolean).)

OnCreateSymbolServer

Handler function creating the symbol server object. (Overrides Session.OnCreateSymbolServer..)

OnDisconnect

Called when [disconnect]. (Overrides Session.OnDisconnect..)

OnGetAddress

Handler function getting the address of the session. (Overrides Session.OnGetAddress..)

ToString

Returns a string that represents the current object. (Inherited from Object.)

Events

 

Name

Description

ConnectionStateChanged

Occurs when connection status of the IConnectionStateProvider has been changed. (Inherited from Session.)

Fields

 

Name

Description

connection

The (established) connection (Inherited from Session.)

Remarks

On top of the well known TcAdsClient class that is used traditionally for ADS communication, the AdsSession class provides the following additionally abilities out of the box: These are used to provide more stable connections to ADS Servers than the TcAdsClient can provide. The main issues are Resurrection / Self-Healing after communication timeouts, faster and less error prone reaction to communication errors (not necessarily waiting for communication timeouts) und enhanced communication diagnosis. These enhanced features are provided by the following additions to the TwinCAT.Ads API:

The AdsConnection is established by calling the Connect. method. The returned AdsConnection can be used as long the AdsSession exists.

Examples

The following sample shows a simple use of the AdsSession object. The AdsSession object (and the dynamic SymbolLoader features) are only available from .NET 4 and upwards.

Use of the AdsSession object

using System;
using System.Diagnostics;
using System.Threading;

using TwinCAT;
using TwinCAT.Ads;
using TwinCAT.Ads.TypeSystem;
using TwinCAT.TypeSystem;
using TwinCAT.TypeSystem.Generic;

namespace Sample
{
    class Session
    {
    /// <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);
        SessionSettings settings = SessionSettings.Default; // Default settings are Async access with Timeout 5 sec

        // Async access is necessary for Console applications!

        using (AdsSession session = new AdsSession(address, settings))
        {
        AdsConnection connection = (AdsConnection) session.Connect(); // Establish the connection
        connection.ConnectionStateChanged += Connection_ConnectionStateChanged;

        ConnectionState connectionState = connection.ConnectionState; // The actual connection state

        // Read the identification and version number of the device
        DeviceInfo deviceInfo = connection.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 = connection.ReadState();
        AdsState adsState = stateInfo.AdsState;

        short deviceState = stateInfo.DeviceState;
        Console.WriteLine(string.Format("DeviceState: {0}", deviceState));
        Console.WriteLine(string.Format("AdsState   : {0}", adsState));

        // Other ADS methods (as formerly used on TcAdsClient) can be used also on connection object:

        // connection.Read(...)
        // connection.Write(...)
        // connection.AddDeviceNotificationEx += ...


        // Session communication Diagnostic:

        int resurrectionTries = connection.ResurrectingTries;
        int succeededResurrections = connection.Resurrections;

        AdsCommunicationStatistics statistics = session.Statistics; // The communication statistics

        // Symbol access:
        // The Session holds and Caches the Symbolic data information
        ReadOnlyDataTypeCollection types = session.SymbolServer.DataTypes;
        ReadOnlySymbolCollection symbols = session.SymbolServer.Symbols;

        dynamic projectNameSymbol = symbols["TwinCAT_SystemInfoVarList._AppInfo.ProjectName"];
        string projectName = (string) projectNameSymbol.ReadValue();

        // Or use dynamic objects
        dynamic appInfo = symbols["TwinCAT_SystemInfoVarList._AppInfo"];
        string projectName2 = appInfo.ProjectName.ReadValue();

        Console.WriteLine("");
        Console.WriteLine("Press [Enter] for leave:");
        Console.ReadLine();
        }
    }

    private static void Connection_ConnectionStateChanged(object sender, ConnectionStateChangedEventArgs e)
    {
        Console.WriteLine("Connection State changed (NewState: {0}, OldState: {1}",e.NewState,e.OldState);
    }

Reference

TwinCAT.Ads Namespace

TwinCAT.Session

TwinCAT.Ads.IAdsSession

IInterceptionFactory