AdsSyncReadWriteReqEx
Delphi 5 Programm
unit frmAdsSyncReadWriteReqExUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, TcAdsDef, TcAdsApi, Buttons, ExtCtrls ;
type
TfrmAdsSyncReadWriteReqEx = class(TForm)
editValue: TEdit;
Label1: TLabel;
ComboBox1: TComboBox;
Label4: TLabel;
BitBtn1: TBitBtn;
Button1: TButton;
Bevel1: TBevel;
editName: TEdit;
Label2: TLabel;
Label3: TLabel;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(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 TfrmAdsSyncReadWriteReqEx.InitData( destAddr : TAmsAddr);
begin
serverAddr := destAddr;
ComboBox1.itemindex := 0;
end;
/////////////////////////////////////////////////////////////////
procedure TfrmAdsSyncReadWriteReqEx.Button1Click( Sender: TObject);
var result : longword;
dataType : TPlcDataTypes;
hVar : Longword;
cbReturn : 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;
sName : AnsiString;
begin
Memo1.Lines.clear;
dataType := TPlcDataTypes(ComboBox1.ItemIndex);
sName := AnsiString(editName.Text);
result := AdsSyncReadWriteReqEx(@serverAddr, ADSIGRP_SYM_HNDBYNAME, $0000, sizeof(hVar), @hVar, Length(sName)+1, @sName[1], @cbReturn );
Memo1.Lines.add( Format( 'AdsSyncReadWriteReqEx(ADSIGRP_SYM_HNDBYNAME..) result: %d [0x%x], Handle: %d [0x%x], Returned bytes: %d', [result, result, hVar, hVar, cbReturn]));
if result = ADSERR_NOERR then
begin
case dataType of
plcBOOL:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nBYTE), @nBYTE );
editValue.Text := IntToStr(nByte And $01);
end;
plcBYTE:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nBYTE), @nBYTE );
editValue.Text := IntToStr(nByte);
end;
plcWORD:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nWORD), @nWORD );
editValue.Text := IntToStr(nWORD);
end;
plcDWORD:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nDWORD), @nDWORD );
editValue.Text := IntToStr(nDWORD);
end;
plcSINT:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(iSINT), @iSINT );
editValue.Text := IntToStr(iSINT);
end;
plcINT:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(iINT), @iINT );
editValue.Text := IntToStr(iINT);
end;
plcDINT:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(iDINT), @iDINT );
editValue.Text := IntToStr(iDINT);
end;
plcREAL:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(fREAL), @fREAL );
editValue.Text := FloatToStr(fREAL);
end;
plcLREAL:
begin
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(fLREAL), @fLREAL );
editValue.Text := FloatToStr(fLREAL);
end;
plcSTRING:
begin
SetLength(sSTRING,256);//make sure the delphi string size is >= SIZEOF(plc string)
result := AdsSyncReadReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, Length(sSTRING), @sSTRING[1] );
editValue.Text := String(sSTRING);
end;
end;
Memo1.Lines.add( Format( 'AdsSyncReadReq(ADSIGRP_SYM_VALBYHND..) result: %d [0x%x]', [result, result]));
result := AdsSyncWriteReq( @serverAddr, ADSIGRP_RELEASE_SYMHND, $0000, sizeof( hVar), @hVar );
Memo1.Lines.add( Format( 'AdsSyncWriteReq(ADSIGRP_RELEASE_SYMHND..) result: %d [0x%x]', [result, result]));
end;
end;
/////////////////////////////////////////////////////////////////
procedure TfrmAdsSyncReadWriteReqEx.Button2Click(Sender: TObject);
var result : longword;
dataType : TPlcDataTypes;
hVar : Longword;
cbReturn : 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;
sName : AnsiString;
begin
Memo1.Lines.clear;
dataType := TPlcDataTypes(ComboBox1.ItemIndex);
sName := AnsiString(editName.Text);
result := AdsSyncReadWriteReqEx(@serverAddr, ADSIGRP_SYM_HNDBYNAME, $0000, sizeof(hVar), @hVar, Length(sName)+1, @sName[1], @cbReturn );
Memo1.Lines.add( Format( 'AdsSyncReadWriteReqEx(ADSIGRP_SYM_HNDBYNAME..) result: %d [0x%x], Handle: %d [0x%x], Returned bytes: %d', [result, result, hVar, hVar, cbReturn]));
if result = ADSERR_NOERR then
begin
case dataType of
plcBOOL:
begin
nByte := Byte(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nBYTE), @nBYTE );
end;
plcBYTE:
begin
nByte := Byte(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nBYTE), @nBYTE );
end;
plcWORD:
begin
nWORD := Word(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nWORD), @nWORD );
end;
plcDWORD:
begin
nDWORD := Longword(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(nDWORD), @nDWORD );
end;
plcSINT:
begin
iSINT := Shortint(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(iSINT), @iSINT );
end;
plcINT:
begin
iINT := Smallint(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(iINT), @iINT );
end;
plcDINT:
begin
iDINT := Longint(StrToInt(editValue.Text));
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(iDINT), @iDINT );
end;
plcREAL:
begin
fREAL := StrToFloat(editValue.Text);
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(fREAL), @fREAL );
end;
plcLREAL:
begin
fLREAL := StrToFloat(editValue.Text);
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, sizeof(fLREAL), @fLREAL );
end;
plcSTRING:
begin
sSTRING := AnsiString(editValue.Text);
if sSTRING = '' then
sSTRING := ''#00;
result := AdsSyncWriteReq(@serverAddr, ADSIGRP_SYM_VALBYHND, hVar, Length(sSTRING)+1, @sSTRING[1] );
end;
end;
Memo1.Lines.add( Format( 'AdsSyncWriteReq(ADSIGRP_SYM_VALBYHND..) result: %d [0x%x]', [result, result]));
result := AdsSyncWriteReq( @serverAddr, ADSIGRP_RELEASE_SYMHND, $0000, sizeof( hVar), @hVar );
Memo1.Lines.add( Format( 'AdsSyncWriteReq(ADSIGRP_RELEASE_SYMHND..) result: %d [0x%x]', [result, result]));
end;
end;
end.
Dokumente hierzu