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

📄 eventtriggerdlg.cpp

📁 关于台湾研华远程以太网模块上位机控制源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// EventTriggerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EventTrigger.h"
#include "EventTriggerDlg.h"
#include "adamtcp.h"
#include <winsock2.h>

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

struct THREAD_INFO  
{
	int SleepTime;
	BOOL Continue;
	int radioCtrlID;
	int countCtrlID;
	int ipCtrlID;
	int listCtrlID;
	HWND hWnd;
} Thread_Info;

/////////////////////////////////////////////////////////////////////////////
// CEventTriggerDlg dialog

CEventTriggerDlg::CEventTriggerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEventTriggerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEventTriggerDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEventTriggerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEventTriggerDlg)
	DDX_Control(pDX, IDC_EDIT_VER, m_ver);
	DDX_Control(pDX, IDC_RADIO3, m_rdo3);
	DDX_Control(pDX, IDC_RADIO2, m_rdo2);
	DDX_Control(pDX, IDC_RADIO1, m_rdo1);
	DDX_Control(pDX, IDC_EDIT_M8, m_module8);
	DDX_Control(pDX, IDC_EDIT_M7, m_module7);
	DDX_Control(pDX, IDC_EDIT_M6, m_module6);
	DDX_Control(pDX, IDC_EDIT_M5, m_module5);
	DDX_Control(pDX, IDC_EDIT_M4, m_module4);
	DDX_Control(pDX, IDC_EDIT_M3, m_module3);
	DDX_Control(pDX, IDC_EDIT_M2, m_module2);
	DDX_Control(pDX, IDC_EDIT_M1, m_module1);
	DDX_Control(pDX, IDC_EDIT_C7, m_count7);
	DDX_Control(pDX, IDC_EDIT_C6, m_count6);
	DDX_Control(pDX, IDC_EDIT_C5, m_count5);
	DDX_Control(pDX, IDC_EDIT_C4, m_count4);
	DDX_Control(pDX, IDC_EDIT_C3, m_count3);
	DDX_Control(pDX, IDC_EDIT_C2, m_count2);
	DDX_Control(pDX, IDC_EDIT_C1, m_count1);
	DDX_Control(pDX, IDC_EDIT_C8, m_count8);
	DDX_Control(pDX, IDC_LIST1, m_ctrllist);
	DDX_Control(pDX, IDBtnExit, m_btnexit);
	DDX_Control(pDX, IDBtnStop, m_btnstop);
	DDX_Control(pDX, IDBtnStart, m_btnstart);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEventTriggerDlg, CDialog)
	//{{AFX_MSG_MAP(CEventTriggerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDBtnStart, OnBtnStart)
	ON_BN_CLICKED(IDBtnStop, OnBtnStop)
	ON_BN_CLICKED(IDBtnExit, OnBtnExit)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WMU_THREAD_EXIT, OnThreadExit)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEventTriggerDlg message handlers

BOOL CEventTriggerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	int iVersion, iRetVal;
	char ver[8]={0};
	char         szDump[80];
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_rdo1.SetCheck(TRUE);
	m_module1.SetWindowText("127.0.0.1");
	m_count1.SetWindowText("0");
	m_count2.SetWindowText("0");
	m_count3.SetWindowText("0");
	m_count4.SetWindowText("0");
	m_count5.SetWindowText("0");
	m_count6.SetWindowText("0");
	m_count7.SetWindowText("0");
	m_count8.SetWindowText("0");
	//
	iVersion = ADAMTCP_GetDLLVersion();
	sprintf(ver, "%X", iVersion);
	m_ver.SetWindowText(ver);
	iRetVal=ADAMTCP_Open();
	if( iRetVal<0 )
	{
		sprintf(szDump,"ADAMTCP_Open() Failure !!!   code=%d\n",iRetVal);
		MessageBox(szDump, "5000/TCP demo", MB_ICONERROR | MB_OK);
		return FALSE;
	}
	ADAMTCP_InitializeEventTrigger();
	//
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEventTriggerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEventTriggerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEventTriggerDlg::OnBtnStart() 
{
	// TODO: Add your control notification handler code here
    int idx;
	char msg[256]={0};
	char szIP[33]={0};
	CEdit *tmpEdit[8];

	tmpEdit[0] = &m_module1;
	tmpEdit[1] = &m_module2;
	tmpEdit[2] = &m_module3;
	tmpEdit[3] = &m_module4;
	tmpEdit[4] = &m_module5;
	tmpEdit[5] = &m_module6;
	tmpEdit[6] = &m_module7;
	tmpEdit[7] = &m_module8;
    //==================================================
    for (idx=0; idx<8; idx++)
    {
        if (tmpEdit[idx]->IsWindowEnabled() && tmpEdit[idx]->GetWindowTextLength()>0)
        {
            tmpEdit[idx]->GetWindowText(szIP, 32);
            if (ADAMTCP_AddModuleIP((char*)szIP, 3000) == ADAMTCP_NoError)
                tmpEdit[idx]->EnableWindow(FALSE);
            else
            {
				sprintf(msg, "Add module [IP:%s] failed", szIP);
                MessageBox(msg, "Error", MB_ICONERROR | MB_OK);
                return;
            }
        }
    }
	//
	m_rdo1.EnableWindow(FALSE);
	m_rdo2.EnableWindow(FALSE);
	m_rdo3.EnableWindow(FALSE);
	m_btnstart.EnableWindow(FALSE);
	m_btnstop.EnableWindow(TRUE);
	m_btnexit.EnableWindow(FALSE);
	m_count1.SetWindowText("0");
	m_count2.SetWindowText("0");
	m_count3.SetWindowText("0");
	m_count4.SetWindowText("0");
	m_count5.SetWindowText("0");
	m_count6.SetWindowText("0");
	m_count7.SetWindowText("0");
	m_count8.SetWindowText("0");
//	for (int i=0;i < m_ctrllist.GetCount();i++)
//		m_ctrllist.DeleteString(i);
	m_ctrllist.ResetContent();
	Thread_Info.hWnd = m_hWnd;
	Thread_Info.ipCtrlID = IDC_EDIT_M1;
	Thread_Info.countCtrlID = IDC_EDIT_C1;
	Thread_Info.radioCtrlID = IDC_RADIO1;
	Thread_Info.listCtrlID = IDC_LIST1;
	Thread_Info.Continue = TRUE;
	Thread_Info.SleepTime = 1000;
	m_pThread = AfxBeginThread(ThreadFun, (LPVOID)&Thread_Info);
}

void CEventTriggerDlg::OnBtnStop() 
{
	// TODO: Add your control notification handler code here
	Thread_Info.Continue=FALSE;
}

void CEventTriggerDlg::OnBtnExit() 
{
	// TODO: Add your control notification handler code here
    ADAMTCP_Close();
	this->DestroyWindow();
}

void CEventTriggerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
    ADAMTCP_TerminateEventTrigger();
    ADAMTCP_Close();
}

UINT CEventTriggerDlg::ThreadFun(LPVOID lParam)
{
	THREAD_INFO * Thread_Info = (THREAD_INFO *) lParam;
	CEventTriggerDlg * hWnd = (CEventTriggerDlg *)CWnd::FromHandle((HWND) Thread_Info->hWnd);
	int ReturnValue;
	HANDLE hEvent;
	CEdit *tmpIP[8];
	CEdit *tmpCnt[8];
	CButton *tmpRdo[3];
	CListBox *tmpList;
	//
	char szIP[128];
    unsigned char szPattern[512];
    char buf[256]={0};
    char hexchar[256];
    int pLen;
	long lIP, lSlot, lChannel, lType, lStatus, lValue, lDateTime;
    int idx;
    struct in_addr addr;
	int cnt[8]={0,0,0,0,0,0,0,0};
	//
	for (idx=0; idx<8; idx++)
		tmpIP[idx] = (CEdit*)hWnd->GetDlgItem(Thread_Info->ipCtrlID+idx); 
	for (idx=0; idx<8; idx++)
		tmpCnt[idx] = (CEdit*)hWnd->GetDlgItem(Thread_Info->countCtrlID+idx); 
	for (idx=0; idx<3; idx++)
		tmpRdo[idx] = (CButton*)hWnd->GetDlgItem(Thread_Info->radioCtrlID+idx); 
	tmpList = (CListBox*)hWnd->GetDlgItem(Thread_Info->listCtrlID);

⌨️ 快捷键说明

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