SPS starten/stoppen
Mit dem folgenden Programm wird das Laufzeitsystem 1bis 4 der SPS gestartet, bzw. gestoppt.
Voraussetzungen:
- Delphi 5.0 oder höher;
- TcAdsDLL.DLL;
- TcAdsDEF.pas und TcAdsAPI.pas, enthalten in der Datei delphi_adsdll_api_units.zip, falls Sie den Quelltext selbst übersetzen möchten;
Delphi 5 Programm:
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.
Sprache / IDE | Beispielprogram auspacken |
---|---|
Delphi XE2 | |
Delphi 5 oder höher (classic) |