Integration in Borland Developer Studio 2006
Creating a new project
Create a new project in Borland Developer Studio (Windows Forms Application).
Add reference
To select the class library TwinCAT.ADS select the command Add Reference... from the Project menu. The Add Reference dialog opens.
In this dialog press the Browse... button and select the file TwinCAT.Ads.dll.
Add namespace
The project manager offers an option for checking whether the component was added to the reference list.
All class library types belong to the TwinCAT.ADS namespace. This namespace must be added in the uses section:
uses
System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data, System.IO, TwinCAT.Ads;
This enables access to the data types defined in TwinCAT.ADS without having to specify the namespace repeatedly. The TcAdsClient class is the core of the TwinCAT.ADS class library and enables the user to communicate with an Ads device. The first step is to create an instance of the TcAdsClient class. Then make use of the Connect method to establish a connection with the ADS device.
Establishing a connection to an ADS device:
var tcAds : TcAdsClient;
procedure TWinForm.Connect();
begin
tcAds := TcAdsClient.Create();
tcAds.Connect('172.16.3.217.1.1', 801);
end;
Reading a PLC variable:
The data (4 bytes integer in the flag area) are transferred with the aid of the AdsStream class, which is derived from System.IO.MemoryStream. The class System.IO.BinaryWriter can be used to write data to the stream, the class System.IO.BinaryReader can be used to read data from the stream.
procedure TWinForm.ReadVar();
var ds : AdsStream;
br : BinaryReader;
begin
// creates a stream with a length of 4 byte
ds := AdsStream.Create(4);
br := BinaryReader.Create(ds);
// reads a DINT from PLC
tcAds.Read($4020,0,ds);
ds.Position := 0;
textBox1.Text := br.ReadInt32().ToString();
end;
Writing a PLC variable:
procedure TWinForm.WriteVar();
var ds : AdsStream;
bw : BinaryWriter;
begin
// creates a stream with a length of 4 byte
ds := AdsStream.Create(4);
bw := BinaryWriter.Create(ds);
ds.Position := 0;
bw.Write( Int32(100));
// writes a DINT to PLC
tcAds.Write($4020,0,ds);
end;
Separating a connection to an ADS device:
procedure TWinForm.Disconnect();
begin
tcAds.Dispose();
end;
PLC program:
PROGRAM MAIN
VAR
test AT%MD0 : DINT;
END_VAR
test := test + 1;