📄 ibhnetsample.pas
字号:
unit IBHNetSample;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, IBHNETLib_TLB,
StdCtrls, ActiveX; // Add required units here
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
PLCNames: TComboBox;
Connect: TButton;
Disconnect: TButton;
Order: TEdit;
Label2: TLabel;
State: TEdit;
Label3: TLabel;
MPI: TEdit;
Label4: TLabel;
GroupBox2: TGroupBox;
Label5: TLabel;
Flag10: TEdit;
Label6: TLabel;
Flag83: TEdit;
R83: TButton;
W83: TButton;
R10: TButton;
W10: TButton;
GroupBox3: TGroupBox;
ListBox1: TListBox;
Label7: TLabel;
ReadBulk: TButton;
Label8: TLabel;
procedure FormCreate(Sender: TObject);
procedure ConnectClick(Sender: TObject);
procedure DisconnectClick(Sender: TObject);
procedure R83Click(Sender: TObject);
procedure W83Click(Sender: TObject);
procedure R10Click(Sender: TObject);
procedure W10Click(Sender: TObject);
procedure ReadBulkClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
IBHNet_Obj: IIIBHnet; // Declare the object
Value: OleVariant;
Connected: Integer;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
Names: WideString;
begin
PLCNames.Items.Clear;
Connected := 0;
IBHNet_Obj := CoIIBHnet.Create; // Create the object
try
IBHNet_Obj.FirstStation(Names); // Get First Station
PLCNames.Items.Add(Names);
while names <> '' do
begin
try
IBHNet_Obj.NextStation(Names); // Get all other stations
if Names <> '' then PLCNames.Items.Add(Names);
except
Names := '';
end;
PLCNames.Text := PLCNames.Items.Strings[0];
end;
except
Application.MessageBox('Error accessing the component', 'Sample', IDOK);
end;
end;
procedure TForm1.ConnectClick(Sender: TObject);
var
Names: WideString;
runstop : Integer;
_mpi: Integer;
Code: Integer;
begin
Names := PLCNames.Text;
Val(MPI.Text, _mpi, Code);
try
IBHNet_Obj.Connect(Names, _mpi); // Connect to CPU with MPI#
Names := IBHNet_Obj.PLC_Mlfb; // Get CPU Order number
runstop := IBHNet_Obj.PLC_Run; // Get Run/Stop
if runstop = 0 then
begin
State.Text := 'Stop';
State.Color := clRed;
end;
if runstop = 1 then
begin
State.Text := 'Run';
State.Color := clGreen;
end;
Order.Text := Names;
Connected := 1;
except
Application.MessageBox('Error connecting to the PLC', 'Sample', IDOK);
end;
end;
procedure TForm1.DisconnectClick(Sender: TObject);
begin
Order.Text := '';
try
Connected := 0;
IBHNet_Obj.Disconnect; // Disconnect from PLC
except
Application.MessageBox('Error disconnecting from the PLC', 'Sample', IDOK);
end;
end;
procedure TForm1.R83Click(Sender: TObject);
var
Value: Integer;
S: string[11];
begin
if Connected = 1 then
begin
try
Value := IBHNet_Obj.M[8,3]; // Read Flag 8.3
Str(Value, S);
Flag83.Text := S;
except
Application.MessageBox('Error Read/Write from/to PLC', 'Sample', IDOK);
end;
end;
end;
procedure TForm1.W83Click(Sender: TObject);
var
Value: Integer;
Code: Integer;
begin
if Connected = 1 then
begin
try
Val(Flag83.Text, Value, Code);
IBHNet_Obj.M[8,3] := Value; // Write Flag 8.3
except
Application.MessageBox('Error Read/Write from/to PLC', 'Sample', IDOK);
end;
end;
end;
procedure TForm1.R10Click(Sender: TObject);
var
Value: Integer;
S: string[11];
begin
if Connected = 1 then
begin
try
Value := IBHNet_Obj.MW[10]; // Read Flagword 10
Str(Value, S);
Flag10.Text := S;
except
Application.MessageBox('Error Read/Write from/to PLC', 'Sample', IDOK);
end;
end;
end;
procedure TForm1.W10Click(Sender: TObject);
var
Value: Integer;
Code: Integer;
begin
if Connected = 1 then
begin
try
Val(Flag10.Text, Value, Code);
IBHNet_Obj.MW[10] := Value; // Write Flagword 10
except
Application.MessageBox('Error Read/Write from/to PLC', 'Sample', IDOK);
end;
end;
end;
procedure TForm1.ReadBulkClick(Sender: TObject);
var
// We need a Safearray to read/write blocks of Data
ArrayBounds : TSafeArrayBound;
ByteField: PSafeArray;
ArrayData : pointer;
I: Integer;
// For Read/WriteVals (Bulk) Functions
// typ: 65d = 41h = 'A' = Output
// 68d = 44h = 'D' = Data
// 69d = 45h = 'E' = Input
// 77d = 4Dh = 'M' = Flag
typ: Integer; // Variable Type (input, output, flag, data)
nr: Integer; // Starting Byte of block
DBNr: Integer; // Datablock number (else 0)
size: Integer; // Size (in bytes) of block
// Data conversion purposes only
Value: Integer;
S: string[11];
// Used to access data from the safearray
type
ByteArray = Array of Byte;
begin
if Connected = 1 then
begin
ListBox1.Clear;
try
typ := 77; // Flag
nr := 4; // starting at 4
DBNr := 0; // No Datablock
size := 20; // Read 20 bytes from starting position
// Initialize safearray first
ArrayBounds.lLbound:= 0;
ArrayBounds.cElements:= size; // The number we want to read
ByteField:= SafeArrayCreate( varByte, 1, ArrayBounds );
// Read Block of data
IBHNet_Obj.ReadVals(typ, nr, DBNr, size, ByteField);
// Lock the safearray for data access
SafeArrayAccessData( ByteField, ArrayData );
for I := 0 to size - 1 do
begin
Value := ByteArray(ArrayData)[I];
Str(Value, S);
ListBox1.Items.Add(S); // Display Values
end;
// Unlock and free the safearray
SafeArrayUnAccessData(ByteField);
SafeArrayDestroy(ByteField);
except
Application.MessageBox('Error Read/Write from/to PLC', 'Sample', IDOK);
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -