AdsSyncWriteReq
Delphi 5 Programm
unit frmAdsSyncWriteReqUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, TcAdsDef, TcAdsApi, Buttons, ExtCtrls ;
type
TfrmAdsSyncWriteReq = class(TForm)
editIO: TEdit;
editIG: TEdit;
Label2: TLabel;
Label3: TLabel;
editValue: TEdit;
Label1: TLabel;
Label5: TLabel;
ComboBox1: TComboBox;
Label4: TLabel;
BitBtn1: TBitBtn;
Button1: TButton;
Bevel1: TBevel;
procedure ComboBox1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
serverAddr : TAmsAddr;
public
{ Public-Deklarationen }
procedure InitData( destAddr : TAmsAddr );
end;
type TPlcDataTypes =
(
plcBOOL,
plcBYTE, //and USINT
plcWORD, //and UINT
plcDWORD, //and UDINT
plcSINT,
plcINT,
plcDINT,
plcREAL,
plcLREAL,
plcSTRING
);
implementation
{$R *.DFM}
///////////////////////////////////////////////////////////////////////
procedure TfrmAdsSyncWriteReq.InitData( destAddr : TAmsAddr );
begin
serverAddr := destAddr;
ComboBox1.itemindex := 0;
ComboBox1Change(self);
end;
///////////////////////////////////////////////////////////////////////
procedure TfrmAdsSyncWriteReq.Button1Click(Sender: TObject);
var result : longword;
dataType : TPlcDataTypes;
indexGroup : Longword;
indexOffset : Longword;
nBYTE : Byte; //and USINT
nWORD : Word; //and UINT
nDWORD : Longword; //and UDINT
iSINT : Shortint;
iINT : Smallint;
iDINT : Longint;
fREAL : Single;
fLREAL : Double;
sSTRING : AnsiString;
begin
result := 0;
dataType := TPlcDataTypes(ComboBox1.ItemIndex);
indexGroup := Longword(StrToInt(editIG.Text));
indexOffset := Longword(StrToInt(editIO.Text));
case dataType of
plcBOOL:
begin
nBYTE := Byte(StrToInt(editValue.Text) And $01);
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(nBYTE), @nBYTE );
end;
plcBYTE:
begin
nBYTE := Byte(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(nBYTE), @nBYTE );
end;
plcWORD:
begin
nWORD := Word(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(nWORD), @nWORD );
end;
plcDWORD:
begin
nDWORD := Longword(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(nDWORD), @nDWORD );
end;
plcSINT:
begin
iSINT := Shortint(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(iSINT), @iSINT );
end;
plcINT:
begin
iINT := Smallint(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(iINT), @iINT );
end;
plcDINT:
begin
iDINT := Longint(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(iDINT), @iDINT );
end;
plcREAL:
begin
fREAL := StrToFloat(editValue.Text);
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(fREAL), @fREAL );
end;
plcLREAL:
begin
fLREAL := StrToFloat(editValue.Text);
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, sizeof(fLREAL), @fLREAL );
end;
plcSTRING:
begin
sSTRING := AnsiString(editValue.Text);
if sSTRING = '' then
sSTRING := ''#00;
result := AdsSyncWriteReq(@serverAddr, indexGroup, indexOffset, Length(sSTRING)+1, @sSTRING[1] );
end;
end;
Label5.Caption := Format( 'AdsSyncWriteReq() result: %d [0x%x]', [result, result] );
end;
///////////////////////////////////////////////////////////////////////
procedure TfrmAdsSyncWriteReq.ComboBox1Change(Sender: TObject);
begin
case ComboBox1.ItemIndex of
0:
editValue.Text := '1';
1..6:
editValue.Text := '7';
7..8:
editValue.Text := '12.34';
9:
editValue.Text := 'my string';
end;
end;
end.
Dokumente hierzu