Cookbook "How to implement an ADS client"

 

• 0. Overview
• 1. Get started and open/connect to ADS router
• 2. Which variables available
• 3. Get handles of variables
• 4. Cyclic collection of life data
• 5. Release handles
• 6. Close ADS channel
• 7. In case of ADS error

0. Overview

Always good to start with overview

Following samples are based on C++, however they work identically for C#

See device concept

1. Get started and open/connect to ADS router

Overview C++ samples

The easy (but not recommended way) to read data: Read flag synchronously from the PLC by absolute adress information
See sample

Disadvantage: If PLC code or PLC variable declaration is changing the ADS-client will not notice this in any case.
WE HIGHLY RECOMMEND to work on handles of variables (see Agenda 3)

2. Which variables available

By compiling PLC code the PLC-Editor "PLC-Control"  creates an XML-based export file <projectname.tpy> containing all information about symbols (variables) and their properties (datatype etc).
At HMI design time this file can be imported to Scada editor to configure mapping between variable and HMI-object.

We are preparing sample code to parse type on demand, will be available here

3. Get handles of variables

ADS-List-command: Get/Release multiple handles with one ADS call

Instead looping through a routine to get handles we recommend, to save protocol transport time by requesting multiple handles with one single ADS call

See sample C++

NOTE:

Devices with previous TwinCAT-versions might not support this call (ADS return code “Service not supported”). In this case collect handles one by one.

4. Cyclic collection of life data

ADS Sum Command : Read

Works also for write direction

The Sub-IndexGroup should always be 0xF005 in combination with the Sub-IndexOffset filled with the handle of variable.

See sample C++

5. Release handles

ADS-List-command: Get/Release multiple handles with one ADS call
Instead looping through a routine to release handles we recommend, to save protocol transport time by releasing multiple handles with one single ADS call

See sample C++

Cookbook "How to implement an ADS client" 1:

Previous TwinCAT-versions

Devices with previous TwinCAT-versions might not support this call (ADS return code “Service not supported”) In this case release handles one by one.

6. Close ADS channel

Always close ADS channel PortClose() – be aware to release handles before !

7. In case of ADS error

Always good to take care on return codes

See ADS return codes
In case of return code “Invalid handle” somebody downloaded a new PLC code or restarted the TwinCAT machine.

Release existing handles and request new handles, after this continue collection data.