Start/stop PLC

The following program starts or stops runtime system 1 to 4 in the PLC.

Requirements:

Start/stop PLC 1:

Delphi 5 program:

unit AdsDLLWriteControlForm;
interface
uses
  TcAdsDEF, TcAdsAPI, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, ComCtrls;
type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    GroupBox1: TGroupBox;
    Start: TButton;
    Stop: TButton;
    RadioGroup1: TRadioGroup;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure StartClick(Sender: TObject);
    procedure StopClick(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  private
    { Private declarations }
    LocalAddr     :TAmsAddr;
    ServerAddr    :TAmsAddr;
    AdsResult     :Longint;
    ClientPort    :Word;
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.DFM}
//////////////////////////////////////////////////////////////////////////////
procedure TForm1.FormCreate(Sender: TObject);
begin
  GroupBox1.Caption := 'PLC';
  GroupBox1.Align := alClient;
  RadioGroup1.Caption := 'RTS';
  RadioGroup1.Items.Add('801');
  RadioGroup1.Items.Add('811');
  RadioGroup1.Items.Add('821');
  RadioGroup1.Items.Add('831');
  RadioGroup1.ItemIndex:=0;
  StatusBar1.SimplePanel := true;
  Label1.Caption:='Ads result:';
  ClientPort:= AdsPortOpen();
  if ClientPort = 0 then {error!}
    ShowMessage('AdsPortOpen() error!')
  else  {OK}
  begin
    AdsResult:=AdsGetLocalAddress( @LocalAddr );
    if AdsResult = 0 then {OK}
    begin
      ServerAddr.netId:=LocalAddr.netId;{connect to the PLC on the local PC}
      ServerAddr.port:=801; {select first RTS}
      StatusBar1.SimpleText:=Format('Client NetId:%d.%d.%d.%d.%d.%d  Client Port:%d',[
          LocalAddr.netId.b[0], LocalAddr.netId.b[1], LocalAddr.netId.b[2],
          LocalAddr.netId.b[3], LocalAddr.netId.b[4], LocalAddr.netId.b[5],
          ClientPort]);
    end
    else
      ShowMessageFmt('AdsGetLocalAddress() error:%d', [AdsResult]);
  end;
end;
//////////////////////////////////////////////////////////////////////////////
procedure TForm1.FormDestroy(Sender: TObject);
begin
  AdsResult := AdsPortClose();
  if AdsResult > 0 then
    ShowMessageFmt('AdsPortClose() error:%d', [AdsResult]);
end;
//////////////////////////////////////////////////////////////////////////////
procedure TForm1.StartClick(Sender: TObject);
begin
  AdsResult:=AdsSyncWriteControlReq( @ServerAddr, ADSSTATE_RUN, 0, 0, Nil );
  Label1.Caption := Format('AdsSyncWriteControlReq result:%d', [AdsResult]);
end;
//////////////////////////////////////////////////////////////////////////////
procedure TForm1.StopClick(Sender: TObject);
begin
  AdsResult:=AdsSyncWriteControlReq( @ServerAddr, ADSSTATE_STOP, 0, 0, Nil );
  Label1.Caption := Format('AdsSyncWriteControlReq result:%d', [AdsResult]);
end;
//////////////////////////////////////////////////////////////////////////////
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
  ServerAddr.port := StrToInt(RadioGroup1.Items[RadioGroup1.ItemIndex]);
end;
end.

Language / IDE

Unpack sample program

Delphi XE2

delphixe2_api_ADS-DLL Sample05.exe

Delphi 5 or higher (classic)

Sample05.exe