unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ZNE100DLL, StdCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Button1: TButton;
    Edit1: TEdit;
    Button2: TButton;
    Button3: TButton;
    GroupBox2: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button4: TButton;
    GroupBox3: TGroupBox;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Edit6: TEdit;
    Edit7: TEdit;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Button5: TButton;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    GroupBox4: TGroupBox;
    Label12: TLabel;
    Label13: TLabel;
    Edit8: TEdit;
    Edit9: TEdit;
    GroupBox5: TGroupBox;
    Label14: TLabel;
    Label15: TLabel;
    Edit10: TEdit;
    Edit11: TEdit;
    Button6: TButton;
    Button7: TButton;
    GroupBox6: TGroupBox;
    Button8: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
count:integer;
ddevinfo:array[0..49] of DEVINFO;
len:integer;
getlen:integer;
str:AnsiString;
i:integer;
begin
	count:=5;
	len:=50;
	str:='搜索到设备:'+#10;

	ZNET_SearchBroadcast();  //广播搜索
	while count>0 do //搜索超时时间设为5秒
  begin
    getlen:=ZNET_RecDevice(@ddevinfo[0],@len);
		while getlen>0 do //此时表示搜索到设备
    begin
      for i:=0 to getlen-1 do
      begin
        str:=str+'IP:'+ddevinfo[i].szip+' MAC:'+ddevinfo[i].szmacaddr+#10;
      end;
      getlen:=ZNET_RecDevice(@ddevinfo[0],@len);
		end;
		Sleep(1000);
    count:=count-1;
  end;

	ZNET_Stop(); //广播搜索，指定IP搜索操作完后都必须调用此函数来停止
	ShowMessage(str);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    ZNET_Init();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    ZNET_Uninit();
end;

procedure TForm1.Button2Click(Sender: TObject);
var
strip:AnsiString;
count:integer;
ddevinfo:array[0..49] of DEVINFO;
len:integer;
getlen:integer;
str:AnsiString;
strtmp:AnsiString;
i:integer;
label ext;
begin
  strip:=Edit1.Text;
	count:=5;
	len:=50;
	str:='搜索到设备:'+#10;

	ZNET_SearchIP(PChar(strip));  //指定IP搜索
	while count>0 do //搜索超时时间设为5秒
  begin
    getlen:=ZNET_RecDevice(@ddevinfo,@len);
		while getlen>0 do //此时表示搜索到设备
    begin
			for i:=0 to getlen-1 do
      begin
        strtmp:=ddevinfo[i].szip;
				if strip=strtmp then
        begin
          str:=str+'IP:'+ddevinfo[i].szip+' MAC:'+ddevinfo[i].szmacaddr+#10;
					goto ext;
				end;
			end;
      getlen:=ZNET_RecDevice(@ddevinfo,@len);
		end;
		Sleep(1000);
    count:=count-1;
	end;
ext:
	ZNET_Stop(); //广播搜索，指定IP搜索操作完后都必须调用此函数来停止
	ShowMessage(str);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
strip:AnsiString;
ddevinfo:DEVINFO;
str:AnsiString;
begin
  strip:=Edit1.Text;
	str:='设备信息:'+#10;

	if ZNET_GetDeviceInformation(PChar(strip),@ddevinfo)=1 then
  begin
    str:=str+'设备名称:'+ddevinfo.szname+#10+'设备IP:'+ddevinfo.szip+#10+'网关:'+ddevinfo.szgateip+#10+
      '子网掩码:'+ddevinfo.szmask+#10+'MAC地址:'+ddevinfo.szmacaddr+#10+'工作端口:'+IntToStr(ddevinfo.workport)+#10;
	end;
	ShowMessage(str);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
strip:AnsiString;
strpwd:AnsiString;
strnewip:AnsiString;
strtmp:AnsiString;
newport:integer;
ddevinfo:DEVINFO;
begin
  strip:=Edit2.Text;
  strpwd:=Edit3.Text;
  strnewip:=Edit4.Text;
  newport:=StrToInt(Edit5.Text);
	if ZNET_GetDeviceInformation(PChar(strip),@ddevinfo)=1 then
	begin
		StrLCopy(PChar(@ddevinfo.szip[0]),PChar(strnewip),StrLen(PChar(strnewip))+1);
		ddevinfo.workport:=newport;
		if ZNET_ModifyDevice(PChar(strip),PChar(strpwd),@ddevinfo)=1 then
		begin
			ShowMessage('更改成功');
		end;
	end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
strip:AnsiString;
strpwd:AnsiString;
ddevinfo:DEVINFO;
begin
  strip:=Edit6.Text;
  strpwd:=Edit7.Text;

	if ZNET_GetDeviceInformation(PChar(strip),@ddevinfo)=1 then
	begin
		ddevinfo.zne100info.baud:=ComboBox1.ItemIndex;
		ddevinfo.zne100info.checkbits:=ComboBox4.ItemIndex;
		ddevinfo.zne100info.databits:=5+ComboBox2.ItemIndex;
		ddevinfo.zne100info.stopbits:=1+ComboBox3.ItemIndex;
		if ZNET_ModifyDevice(PChar(strip),PChar(strpwd),@ddevinfo)=1 then
		begin
			ShowMessage('更改成功');
		end;
	end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var
strip:AnsiString;
begin
  strip:=Edit8.Text;
	if ZNET_ResetDevice(PChar(strip))<>1 then
  begin
		ShowMessage('复位设备失败');
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
strip:AnsiString;
strport:AnsiString;
strtmp:AnsiString;
dir:BYTE;
elevel:BYTE;
port:integer;
begin
  strip:=Edit8.Text;
  port:=StrToInt(Edit9.Text);
	if ZNET_GetIOStat(PChar(strip),port,@dir,@elevel)=0 then
	begin
		ShowMessage('获取IO口状态失败');
		Exit;
	end;

  Edit10.Text:=IntToHex(dir,2);
  Edit11.Text:=IntToHex(elevel,2);
end;

procedure TForm1.Button7Click(Sender: TObject);
var
strip:AnsiString;
strport:AnsiString;
strtmp:AnsiString;
dir:BYTE;
elevel:BYTE;
port:integer;
begin
  strip:=Edit8.Text;
  port:=StrToInt(Edit9.Text);
	dir:=StrToInt('0x'+Edit10.Text);
	elevel:=StrToInt('0x'+Edit11.Text);
	if ZNET_SetIOStat(PChar(strip),port,2,@dir,@elevel)=0 then
	begin
		ShowMessage('更改IO口状态失败');
		Exit;
	end;
  Edit10.Text:=IntToHex(dir,2);
  Edit11.Text:=IntToHex(elevel,2);
end;

end.


