⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchdlg.cpp

📁 F340+CP2200的汉化版NETFINDER源代码,增加了串口设置
💻 CPP
字号:
// SearchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "netfinder.h"
#include "SearchDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CNetfinderApp theApp;

#define SEARCH_TIMER 1

/////////////////////////////////////////////////////////////////////////////
// CSearchDlg dialog


CSearchDlg::CSearchDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSearchDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSearchDlg)
	m_progress_text = _T("");
	m_devicesfound_text = _T("");
	//}}AFX_DATA_INIT
}


void CSearchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSearchDlg)
	DDX_Control(pDX, IDCANCEL, m_stop_button);
	DDX_Control(pDX, IDOK, m_ok_button);
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	DDX_Text(pDX, IDC_EDIT2, m_progress_text);
	DDX_Text(pDX, IDC_EDIT3, m_devicesfound_text);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSearchDlg, CDialog)
	//{{AFX_MSG_MAP(CSearchDlg)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDOK, OnOK)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSearchDlg message handlers
//建立搜索对话框
BOOL CSearchDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	
	//-----------------------------------------------------------------
	// Add pointer to this dialog to <theApp> to provide global access
	//-----------------------------------------------------------------
	theApp.pSearchDlg = this;//添加指向此对话框的指针为全局访问
	
	
	//-----------------------------------------------------------------
	// Register the Operation to allow packets to be received
	//-----------------------------------------------------------------
	theApp.m_operation = SEARCH;//登记允许包接收的操作
	
	//-----------------------------------------------------------------
	// Initialize the network error flag
	//-----------------------------------------------------------------
	m_networkerror = 0;


	//-----------------------------------------------------------------
	// Initialize the Progress Bar
	//-----------------------------------------------------------------
	
	#define RANGE 10*1000					// 10 seconds
	#define STEP 250 						// 250 ms
	
	m_progress.SetRange( 0, RANGE );		// Set Range设置整个范围
	m_progress.SetStep( -STEP );			// Same as Timer Interval设置步长
	m_progress.SetPos(RANGE);				//设置起始位置
	
	m_interval = 0;					// Clear timer counter

	m_progress_text.Format("%i 秒剩余时间", RANGE/1000);
	
	m_devicesfound = 0;
	m_devicesfound_text.Format ("%i 个装置被发现!", m_devicesfound);
	UpdateData(FALSE);
	
	m_stop_button.EnableWindow();
	m_ok_button.EnableWindow(FALSE);
	
	CheckDlgButton(IDC_CLOSE, TRUE);

	//-----------------------------------------------------------------
	// Start the Timer
	//-----------------------------------------------------------------
	SetTimer(SEARCH_TIMER, STEP, NULL);//设置SEARCH_TIMER定时器
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}



void CSearchDlg::OnTimer(UINT nIDEvent) 
{
	
	//-----------------------------------------------------
	// Perform Tasks for first time 第一次
	//-----------------------------------------------------
	if(m_interval == 0){
		
		SendBroadcast();			// Send Identity Request Packets 发送广播请求包
	}

	//-----------------------------------------------------
	// Perform Tasks for each interval 每一个时间间隔
	//-----------------------------------------------------
	
	// Adjust Progress Bar
	m_progress.StepIt();
	
	// Set Progress Bar Text
	m_progress_text.Format("%i",  m_progress.GetPos()/1000); 
	m_progress_text += " 秒剩余时间";
	UpdateData(FALSE);

	// Check for timeout
	if( (m_progress.GetPos() == 0) && !IsDlgButtonChecked(IDC_RELOAD) ){
		KillTimer(SEARCH_TIMER);
		
		m_stop_button.EnableWindow(FALSE);
		m_ok_button.EnableWindow();

		if(IsDlgButtonChecked(IDC_CLOSE)){
			OnOK();
		}
	}
	
	//-----------------------------------------------------
	// Perform Tasks for specific intervals 指定时间间隔
	//-----------------------------------------------------
	
	// Resend Breoadcast (after 2seconds)
	if(m_progress.GetPos() == (RANGE-(2*4*STEP)) ){
		
		ResendBroadcast();	

	} else
	
	
	// Resend Breoadcast (after 4seconds)
	if(m_progress.GetPos() == (RANGE-(4*4*STEP)) ){
		
		ResendBroadcast();	

	} else
	
	// Resend Breoadcast (after 5 seconds if no response)
	if(m_progress.GetPos() == (RANGE-(5*4*STEP))   && (m_devicesfound == 0)){
		
		ResendBroadcast();	

	} else

	// Resend Breoadcast (after 6 seconds if no response)
	if(m_progress.GetPos() == (RANGE-(6*4*STEP))  && (m_devicesfound == 0)){
		
		ResendBroadcast();	

	} else

	// Resend Breoadcast (after 7 seconds if no response)
	if(m_progress.GetPos() == (RANGE-(7*4*STEP))  && (m_devicesfound == 0)){
		
		ResendBroadcast();	

	} else

	// Resend Breoadcast (after 8 seconds if no response)
	if(m_progress.GetPos() == (RANGE-(8*4*STEP))  && (m_devicesfound == 0)){
		
		ResendBroadcast();	

	} else
	

	// Resend Breoadcast (after 9 seconds if no response)
	if(m_progress.GetPos() == (RANGE-(9*4*STEP))  && (m_devicesfound == 0)){
		
		ResendBroadcast();	

	} else

	//-----------------------------------------------------
	// Increment Interval 间隔加一
	//-----------------------------------------------------
	m_interval++;

	CDialog::OnTimer(nIDEvent);
}




void CSearchDlg::SendBroadcast()
{
	//-----------------------------------------------------------------
	// Send Broadcast Identity Request Packets
	//-----------------------------------------------------------------
	// Note: This section may be moved to a function to allow
	// multiple re-tries.
	//
	unsigned char buff[4] = { 0,0,0,0 };
	
	do{
	
		m_rand = (rand()); // rand returns a number between 0 and 0x7FFF
	
	} while(m_rand == 0);    // make sure m_rand is not zero

	buff[2] = (m_rand >> 8);
	buff[3] = (m_rand & 0x00FF);
	
	unsigned int i;
	for(i = 0; i < theApp.m_numports; i++){
	   	
		if(m_networkerror){
			return;
		}
		
		if(SOCKET_ERROR == theApp.m_socket.SendTo(buff, 4, theApp.m_ports[i], NULL)){
			m_networkerror = 1;
			
			CString str;
			str.Format("错误代码: %i.", GetLastError());			
			AfxMessageBox(str + " 不能够发送包. 请检查网络连接.");
			
			OnCancel();
		}
	}
	
}

void CSearchDlg::ResendBroadcast()
{
	//-----------------------------------------------------------------
	// Send Broadcast Identity Request Packets
	//-----------------------------------------------------------------
	// Note: This function uses the same random number calculated
	// by SendBroadcast().
	//
	unsigned char buff[4] = { 0,0,0,0 };
	
	buff[2] = (m_rand >> 8);
	buff[3] = (m_rand & 0x00FF);

	unsigned int i;
	for(i = 0; i < theApp.m_numports; i++){
		
		if(m_networkerror){
			return;
		}

		if(SOCKET_ERROR == theApp.m_socket.SendTo(buff, 4, theApp.m_ports[i], NULL)){
			m_networkerror = 1;
			
			CString str;
			str.Format("错误代码: %i.", GetLastError());			
			AfxMessageBox(str + " 不能够发送包. 请检查网络连接.");
			
			OnCancel();
		}
	}
}


void CSearchDlg::ReceivePacket()
{
	
	unsigned char buffer[4096] = {0};
	int num_bytes;
	
	//---------------------------------------------------------------
	// Receive Packet from Buffer
	//---------------------------------------------------------------

	// IP and Port of sender.
	CString destIP;
	UINT destPort;

	num_bytes = theApp.m_socket.ReceiveFrom(buffer, 4096, destIP, destPort);

	// Check for errors
	if(num_bytes >= 10000){
		
		// Handle Error
		AfxMessageBox("接受的数据错误");
		return;

	} else {
		
		// Handle Error
		//AfxMessageBox("Received Packet");
	
	}
	
	//---------------------------------------------------------------
	// Verify Packet
	//---------------------------------------------------------------
	//
	//	Check minimum packet size
	//  Check packet type field (must be 0x01)
	//	Verify random number
	//
	//
	if( (num_bytes >= 34) && (buffer[0] == 0x01) && (buffer[2] == (m_rand >> 8))  
												 && (buffer[3] == (m_rand & 0x00FF))
	  )
	{	
		//---------------------------------------------------------------
		// Check if entry already exists
		//---------------------------------------------------------------
		unsigned char  mac[6];
		unsigned char table_mac[6];
		int k;
				
		// Fill <mac> with the mac address from the packet 来自包中的MAC地址
		memcpy(mac, &buffer[14], 6);
			

		// Check the mac address of each cell in the table
		for(k = 0; k < theApp.pMainDlg->m_netdisplay.GetNumCells(); k++){

			// Fill <table_mac> with the mac address from the table 来自表中的MAC地址
			theApp.pMainDlg->m_netdisplay.GetMACAddress(k, table_mac);

			// If the mac address matches a device in the table,
			// ignore this packet	//相同忽略此包!
			if( memcmp(mac, table_mac, 6) == 0){
				return;
			}
		
		}

		//---------------------------------------------------------------
		// Add Entry	增加条目
		//---------------------------------------------------------------

		NETDISPLAY_ENTRY entry;	//定义一个NETDISPLAY_ENTRY对象
		
		entry.alert_level = buffer[1];	//装置状态:00-OK、01-无地址、FF-故障
		entry.port = destPort;

		int i = 4; // Start buffer index at 4
		
		entry.event1_days = ( (int) buffer[i++]<<8 );
		entry.event1_days |= buffer[i++];

		entry.event1_hours = buffer[i++];
		entry.event1_minutes = buffer[i++];
		

		entry.event2_days = ( (int) buffer[i++]<<8 );	
		entry.event2_days |= buffer[i++];

		entry.event2_hours = buffer[i++];
		entry.event2_minutes = buffer[i++];
		
		entry.event1_seconds = buffer[i++];
		entry.event2_seconds = buffer[i++];

		entry.mac[0] = buffer[i++];
		entry.mac[1] = buffer[i++];
		entry.mac[2] = buffer[i++];
		entry.mac[3] = buffer[i++];
		entry.mac[4] = buffer[i++];
		entry.mac[5] = buffer[i++];

		entry.ip[0] = buffer[i++];
		entry.ip[1] = buffer[i++];
		entry.ip[2] = buffer[i++];
		entry.ip[3] = buffer[i++];
		
		entry.subnet[0] = buffer[i++];
		entry.subnet[1] = buffer[i++];
		entry.subnet[2] = buffer[i++];
		entry.subnet[3] = buffer[i++];

		entry.gateway[0] = buffer[i++];
		entry.gateway[1] = buffer[i++];
		entry.gateway[2] = buffer[i++];
		entry.gateway[3] = buffer[i++];

		entry.baudrate      = buffer[i++];
		entry.baudrate_mode = buffer[i++];

		entry.str_type = &buffer[i];
		i += (entry.str_type.GetLength()+1);

		entry.str_description = &buffer[i];
		i += (entry.str_description.GetLength()+1);
		
		// Copy the Event1+2 text descripion without the ": "
		entry.str_ev1 = &buffer[i];
		i += (entry.str_ev1.GetLength()+1);
		
		entry.str_ev2 = &buffer[i];
		i += (entry.str_ev2.GetLength()+1);
	
		// Change EVENT1 and EVENT2 checkbox labels
		theApp.pMainDlg->m_checkbox_ev1.SetWindowText(CString("显示") + entry.str_ev1);
		theApp.pMainDlg->m_check_ev2.SetWindowText(CString("显示") + entry.str_ev2);		
		
		// Change EVENT1 and EVENT2 names in the drop down list
		theApp.pMainDlg->m_sortby_combo.ResetContent();
		theApp.pMainDlg->m_sortby_combo.AddString(entry.str_ev1);
		theApp.pMainDlg->m_sortby_combo.AddString(entry.str_ev2);
		theApp.pMainDlg->m_sortby_combo.AddString("IP地址");
		theApp.pMainDlg->m_sortby_combo.AddString("不分类");
		theApp.pMainDlg->m_sortby_combo.SetCurSel(theApp.pMainDlg->m_sortby_selected);

		// Add ": " to str_ev1 and str_ev2
		entry.str_ev1 += ": ";
		entry.str_ev2 += ": ";

		// Add the entry
		theApp.pMainDlg->m_netdisplay.AddEntry(&entry);

		// Update Devices Found Label
		m_devicesfound++;
		m_devicesfound_text.Format ("%i 个装置被发现!", m_devicesfound);
		UpdateData(FALSE);
	
	} else 
	
	if((buffer[0] == 0x00) && (buffer[2] == (m_rand >> 8))  
						   && (buffer[3] == (m_rand & 0x00FF)))
	{
		// Discard Packet
		// We have received an identity request from another host.
		// This is a very rare case in which the PC randomly chooses port 3040 
		// for the netfinder app and the same random number is chosen.
	} else

	{
		AfxMessageBox("接受无效的搜索包或随机数失配");
	
	}	
}


void CSearchDlg::OnOK() 
{

	//-------------------------------------------------------
	// Close the socket
	//-------------------------------------------------------
	theApp.m_operation = PORT_CLOSED;
	
	CDialog::OnOK();
}

void CSearchDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	//-------------------------------------------------------
	// Close the socket
	//-------------------------------------------------------
	theApp.m_operation = PORT_CLOSED;
	
}


void CSearchDlg::OnCancel() 
{
	//-------------------------------------------------------
	// Close the socket
	//-------------------------------------------------------
	theApp.m_operation = PORT_CLOSED;
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -