Establishing ADS communication
This chapter shows you how to connect a CX7080 to another CX70x0 or any TwinCAT controller. The ADS protocol provides the simplest way to connect two TwinCAT systems to each other. With the ADS protocol, data can be both read and written. ADS function blocks are normally used for communication; these are included in the Tc2_System library. In the following example, data are to be written to and read from a memory area.
In order to set up an ADS connection, an ADS route is created first. Communication then takes place via Ethernet and data exchange via the TCP/IP protocol. The ADS route is then the interface between the ADS and TCP/IP connection. The ADS route indicates which AmsNetId is assigned to which TCP/IP address. As a result, the ADS function blocks no longer use the TCP/IP address, but the AmsNetId.
Requirements:
- Two CX70x0 Embedded PCs.
- Both CX70x0s are in the same network and accessible via ADS.
Proceed as follows:
- 1. Start TwinCAT and connect to the first CX70x0 (see: Connect to the CX70x0).
- 2. On the left in the tree view, click Routes, select the Static Routes tab, and click the Add button.
- 3. Under Remote Route, select the Static option so that the ADS route remains in the project, and then click the Broadcast Search button.
- 4. Select the second CX70x0 as the destination of the ADS route. The ADS route is entered for both Embedded PCs. The AmsNetId of the second CX70x0 is displayed and can be used in the program for ADS function blocks.
- 5. Now connect to the second CX70x0, which has been set as the destination of the ADS route, and write a small program. Define an array and increment a value of the array.
VAR
MarksTest AT %MB0 : ARRAY[0..9] of INT;
END_VAR
Program:
MarksTest[0]:=MarksTest[0]+1;
- 6. Activate the configuration and switch the CX70x0 to Run mode.
- 7. For the first CX70x0, write a program that reads the incremented value of the array.
VAR
ADSREAD : ADSREAD;
NetID : STRING:='5.81.38.23.1.1'; (* AMSNetId of the target*)
Value : INT; (* value of target MarksTest[0]*)
Error : INT;
NoError : INT;
END_VAR
Program:
ADSREAD(
NETID:=NetID ,
PORT:=851 , (* plc port of the target*)
IDXGRP:=16#4020 , (* Marks %MB*)
IDXOFFS:=0 , (* Marks offset in byte*)
LEN:=2 , (* length of data in byte*)
DESTADDR:=ADR(Value) , (* pointer to the data in which the value is to be stored *)
READ:=TRUE ,
TMOUT:= ,
BUSY=> ,
ERR=> ,
ERRID=> );
IF NOT ADSREAD.BUSY THEN
IF NOT ADSREAD.ERR THEN
NoError:=NoError+1;
ELSE
Error:=Error+1;
END_IF
ADSREAD(Read:=FALSE);
END_IF
- 8. The incremented value is read out and transmitted to the first CX70x0.
- You should see on the first CX70x0 how the value of the
Value
variable is incremented. The writing of the data works in the same way. Data can be written with theADSWRITE
function block. Make sure that you set the offset (IDXOFFSET
) to 10 in this sample setup so that the array [4... 9] is written. Limit the length to 10 bytes, as an array of 0... 9 of type INT was created and the memory thus uses %MB0... MB19 (10 * 2 bytes) (The elements 0...4 for reading the array and the elements 5...9 for writing it).
Use one ADS command at a time. Wait until the ADS service is finished, i.e. theBUSY
output of the function block is switched toFALSE
, and only then use the next ADS function block. To optimize the access timing, you can also use anADSREADWRITE
function block that reads and writes the data at the same time.