Accessing an array in the PLC with Delphi
Task
The PLC contains an array that is to be read by the .Net application using a read command.
Description
The PLC contains an array of 100 elements of type integer (2 bytes). The array in the PLC is to be filled with the values from 3500 to 3599.
Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET) program
namespace Sample01;
interface
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Windows.Forms,
System.ComponentModel, System.IO, TwinCAT.Ads;
type
/// <summary>
/// Summary description for MainForm.
/// </summary>
MainForm = partial class(System.Windows.Forms.Form)
private
method MainForm_Load(sender: System.Object; e: System.EventArgs);
method MainForm_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
method btnRead_Click(sender: System.Object; e: System.EventArgs);
tcClient : TcAdsClient;
hVar : Int32;
protected
method Dispose(disposing: Boolean); override;
public
// PLCVar : ARRAY [0..99] OF INT; array of 2 byte signed integer
const MAX_ARRAY_ELEMENTS = 100;// Max. number of PLC array elements
const MAX_ADSSTREAM_SIZE = MAX_ARRAY_ELEMENTS * 2;// Max. byte length of array data
constructor;
end;
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
if assigned(components) then
components.Dispose();
//
// TODO: Add custom disposition code here
//
end;
inherited Dispose(disposing);
end;
{$ENDREGION}
method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
begin
try
// Create instance of class TcAdsClient
tcClient := new TcAdsClient();
// Create connection with Port 801 on local PC
tcClient.Connect(801);
// Create PLC variable handle
hVar := tcClient.CreateVariableHandle('MAIN.PLCVar')
except
on err: Exception do
MessageBox.Show(err.Message, err.Source)
end;
end;
method MainForm.MainForm_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
try
// Release PLC variable handle
tcClient.DeleteVariableHandle(hVar)
except
on err: Exception do
MessageBox.Show(err.Message, err.Source)
end;
// Close connection
tcClient.Dispose();
end;
method MainForm.btnRead_Click(sender: System.Object; e: System.EventArgs);
begin
try
// Create stream which gets the data
var dataStream := new AdsStream(MAX_ADSSTREAM_SIZE);
// Create BinaryReader
var binRead := new BinaryReader(dataStream);
// Read complete array from PLC to stream
tcClient.Read(hVar,dataStream);
// Clear list box
lbArray.Items.Clear();
// Reset stream position
dataStream.Position := 0;
// Read all data from stream, convert to string and add list box items
for I:Integer := 1 to MAX_ARRAY_ELEMENTS do
lbArray.Items.Add(binRead.ReadInt16().ToString());
except
on err: Exception do
MessageBox.Show(err.Message, err.Source)
end;
end;
PLC program
PROGRAM MAIN
VAR
PLCVar : ARRAY [0..99] OF INT;
Index: BYTE;
END_VAR
FOR Index := 0 TO 99 DO
PLCVar[Index] := 3500 + INDEX;
END_FOR
Download
Language / IDE | Unpack the sample program |
---|---|
Delphi Prism (Embarcadero Prism XE2, Oxygene for .NET) | |
Delphi for .NET (Borland Developer Studio 2006) |