//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "zne100dll.h"
#include <stdio.h>
#include <stdlib.h>



//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        ZNET_Init();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
	int count=5;
	DEVINFO devinfo[50];
	int len=50;
	int getlen;
	String str="搜索到设备:\n",strtmp;

	ZNET_SearchBroadcast();  //广播搜索
	while(count--) //搜索超时时间设为5秒
	{
		while((getlen=ZNET_RecDevice(devinfo,&len))>0) //此时表示搜索到设备
		{
			for(int i=0;i<getlen;i++)
			{
				strtmp.sprintf("IP:%s MAC:%s\n",devinfo[i].szip,devinfo[i].szmacaddr);
				str+=strtmp;
			}
		}
		Sleep(1000);
	}
	ZNET_Stop(); //广播搜索，指定IP搜索操作完后都必须调用此函数来停止
	ShowMessage(str);
}
//---------------------------------------------------------------------------


__fastcall TForm1::~TForm1()
{
        //TODO: Add your source code here
        ZNET_Uninit();
        
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
	String strip;
        strip=Edit1->Text;
	int count=5;
	DEVINFO devinfo[50];
	int len=50;
	int getlen;
        String str="搜索到设备:\n",strtmp;

	ZNET_SearchIP(strip.c_str());  //指定IP搜索
	while(count--) //搜索超时时间设为5秒
	{
		while((getlen=ZNET_RecDevice(devinfo,&len))>0) //此时表示搜索到设备
		{
			for(int i=0;i<getlen;i++)
			{
				if(strcmp(strip.c_str(),devinfo[i].szip)==0)
				{
        				strtmp.sprintf("IP:%s MAC:%s\n",devinfo[i].szip,devinfo[i].szmacaddr);
					str+=strtmp;
					goto ext;
				}
			}
		}
		Sleep(1000);
	}
ext:
	ZNET_Stop(); //广播搜索，指定IP搜索操作完后都必须调用此函数来停止
	ShowMessage(str);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
	String strip;
        strip=Edit1->Text;
	DEVINFO devinfo;
	String str="设备信息:\n",strtmp;

	if(ZNET_GetDeviceInformation(strip.c_str(),&devinfo)==1)
	{
                strtmp.sprintf("设备名称:%s\n",devinfo.szname);
		str+=strtmp;
		strtmp.sprintf("设备IP:%s\n",devinfo.szip);
		str+=strtmp;
		strtmp.sprintf("网关:%s\n",devinfo.szgateip);
		str+=strtmp;
		strtmp.sprintf("子网掩码:%s\n",devinfo.szmask);
		str+=strtmp;
		strtmp.sprintf("MAC地址:%s\n",devinfo.szmacaddr);
		str+=strtmp;
		strtmp.sprintf("工作端口:%d\n",devinfo.workport);
		str+=strtmp;
	}
	ShowMessage(str);
        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
	String strip,strpwd,strnewip,strtmp;
        int newport;
        strip=Edit2->Text;
        strpwd=Edit3->Text;
        strnewip=Edit4->Text;
        newport=atoi(Edit5->Text.c_str());
	DEVINFO devinfo;
	int res;

	if(ZNET_GetDeviceInformation(strip.c_str(),&devinfo)==1)
	{
		strcpy(devinfo.szip,strnewip.c_str());
		devinfo.workport=newport;
		if((res=ZNET_ModifyDevice(strip.c_str(),strpwd.c_str(),
			devinfo))==1)
		{
			ShowMessage("更改成功");
		}
	}        
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
	String strip,strpwd;
        strip=Edit6->Text;
        strpwd=Edit7->Text;
	DEVINFO devinfo;
	int res;

	if(ZNET_GetDeviceInformation(strip.c_str(),&devinfo)==1)
	{
		devinfo.zne100info.baud=ComboBox1->ItemIndex;
		devinfo.zne100info.checkbits=ComboBox4->ItemIndex;
		devinfo.zne100info.databits=5+ComboBox2->ItemIndex;
		devinfo.zne100info.stopbits=1+ComboBox3->ItemIndex;
		if((res=ZNET_ModifyDevice(strip.c_str(),strpwd.c_str(),
			devinfo))==1)
		{
			ShowMessage("更改成功");
		}
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
	String strip,strpwd;
        strip=Edit8->Text;
        strpwd=Edit12->Text;
	if(ZNET_ResetDevice(strip.c_str(),strpwd.c_str())!=1)
		ShowMessage("复位设备失败");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
	String strip,strport,strtmp;
	BYTE dir,elevel;
        int port;
        strip=Edit8->Text;
        port=atoi(Edit9->Text.c_str());
	if(ZNET_GetIOStat(strip.c_str(),port,&dir,&elevel)==0)
	{
		ShowMessage("获取IO口状态失败");
		return;
	}
	strtmp.sprintf("%02X",dir);
        Edit10->Text=strtmp;
	strtmp.sprintf("%02X",elevel);
        Edit11->Text=strtmp;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
	String strip,strport,strtmp;
	BYTE dir,elevel;
	int itmp;
        strip=Edit8->Text;
        int port=atoi(Edit9->Text.c_str());
	sscanf(Edit10->Text.c_str(),"%x",&itmp);
	dir=itmp;
	sscanf(Edit11->Text.c_str(),"%x",&itmp);
	sscanf(strtmp.c_str(),"%x",&itmp);
	elevel=itmp;
	if(ZNET_SetIOStat(strip.c_str(),port,2,&dir,&elevel)==0)
	{
		ShowMessage("更改IO口状态失败");
		return;
	}
	strtmp.sprintf("%02X",dir);
        Edit10->Text=strtmp;
	strtmp.sprintf("%02X",elevel);
        Edit11->Text=strtmp;
}
//---------------------------------------------------------------------------
