Integration in Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET)
Embarcadero Prism XE2 = Oxygene for .NET
Delphi Prism uses Microsoft Visual Studio 2010 as IDE and integrates itself in an existing VS installation from there. The Visual Studio IDE is supplied with the Delphi Prism installation if it is not yet available.
Create new project
The TwinCAT.ADS component can only be integrated in an existing project. Start VS and select the following command from the menu: File->New->Project...
In the New Project dialog please select as project type: Delphi Prism->Windows Forms Application.
Adding a 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 should select the Browse tab and navigate to file TwinCAT.ADS.dll. Confirm the selected dll with Add and OK. By default the TwinCAT.ADS component is located in folder ..TwinCAT\Ads Api\.NET\ .
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:
namespace AdsNETSample;'
interface
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Windows.Forms,
System.ComponentModel,
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:
type
//...
MainForm = partial class(System.Windows.Forms.Form)
private
tcAds : TcAdsClient;
method MainForm_Load(sender: System.Object; e: System.EventArgs);
method MainForm_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
method ReadVar_Click(sender: System.Object; e: System.EventArgs);
method WriteVar_Click(sender: System.Object; e: System.EventArgs);
protected
method Dispose(disposing: Boolean); override;
public
constructor;
end;
implementation
//...
ethod MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
begin
tcAds := new TcAdsClient();
tcAds.Connect('1.2.3.4.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.
method MainForm.ReadVar_Click(sender: System.Object; e: System.EventArgs);
begin
// creates a stream with a length of 4 byte
var ds := new AdsStream(4);
var br := new BinaryReader(ds);
// reads a DINT from PLC
tcAds.Read($4020,0,ds);
ds.Position := 0;
textBox1.Text := br.ReadInt32().ToString();
end;
Writing a PLC variable:
method MainForm.WriteVar_Click(sender: System.Object; e: System.EventArgs);
begin
// creates a stream with a length of 4 byte
var ds := new AdsStream(4);
var bw := new BinaryWriter(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:
method MainForm.MainForm_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
tcAds.Dispose();
end;
PLC program:
PROGRAM MAIN
VAR
test AT%MD0 : DINT;
END_VAR
test := test + 1;