Connect to a target device / ADS Server

In the TwinCAT world, every software/hardware instance participating at the ADS communication network is an ADS Server. To access these servers various implementations of ADS Clients for different programming languages are available.

To establish an ADS connection in Powershell, the ‘New-TcSession’ Cmdlet is used. Internally it uses the .NET ‘TwinCAT.Ads.AdsClient’ object for communicating.

If using the -NetId, -Port, -Route or -Address Arguments of the ‘TcXaeMgmt’ Module Cmdlets, the connection (and therefore the AdsClient) is established on the fly. The connection is shut down after the single Cmdlet is executed.

The advantage of the ‘Session’ is, that leaves the connection established until ‘Close-TcSession’ is called, or the Powershell host process (the Powershell-Session) ends.

Existing ADS Sessions can be stored in variables or can be accessed by ‘Get-TcSession’.

These Cmdlets use ArgumentCompleters proposing registered Routes.

Open an ADS Session

PS> New-TcSession -NetId '172.17.62.105.1.1' -port 851

ID Address                Connected State     Cycles Losses LastError Established         LastSucceed
-- -------                --------- -----     ------ ------ --------- -----------         -----------
5  172.17.62.105.1.1:851 True       Succeeded 0      0                2024-01-08T12:50:58

Establishes a new Ads Session/Connection to the specified NetId/Port address.

Opens an ADS Session, use it and closes it afterwards

PS> $session = New-TcSession -NetId '1.2.3.4.1.1' -port 10000
PS> $session | Get-AdsState

Target               NetId             Port   State      Latency
                                                          (ms)
------               -----             ----   -----      -------
CX_1234              1.2.3.4.1.1       10000  Config     3

PS> $session | Close-TcSession

Opens a session to the registered route with AmsNetId: 1.2.3.4.1.1 and closes the ADS Session again.

Open an ADS Session with wildcard name pattern.

PS> $route = Get-AdsRoute -Name "Tc3*"
PS> $session = New-TcSession -Route $route -Port 851
PS> $session

ID Address                Connected State     Cycles Losses LastError Established         LastSucceed
-- -------                --------- -----     ------ ------ --------- -----------         -----------
5  172.17.62.105.1.1:851  True      Succeeded 0      0                2024-01-08T12:50:58

Establishes a new ADS Session/Connection to the specified route destination that has the name pattern "tc3*" via port 851 (PLC1).

List established ADS Sessions

PS> Get-TcSession

ID Address                Connected State     Cycles Losses LastError Established         LastSucceed
-- -------                --------- -----     ------ ------ --------- -----------         -----------
5  172.17.62.105.1.1:851  True      Succeeded 0      0                2024-01-08T12:50:58

Lists all actual initiated sessions.

Close ADS Sessions

PS> Close-TcSession -Id 5

Closes the session with id 5.