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

📄 event2dlg.cpp

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

#include "stdafx.h"
#include "Event2.h"
#include "Event2Dlg.h"

#include <winsock2.h>
#include "..\..\..\include\adamtcp.h"

UINT ThreadProc( LPVOID lpParam );

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

char	  	  szIPAddress[20];   // store the IP address of 1st 5000/TCP
char		  szIPAddress2[20];  // store the IP address of 2nd 5000/TCP
static int    iTotalReceive;
static int    iTotalTimeout;
static int    iThreadIndex;

//----------------------------------------------
struct _StreamData   *pStreamData,StreamData;
static BYTE byLoopFlag=1;
HANDLE    hEvent;

int  iMatchIndex,iReceiveCount,iThreadRun,iTotalStream=99;
char szFromIP[20];
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEvent2Dlg dialog

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

void CEvent2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEvent2Dlg)
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_IPADDRESS2, m_strIPAddress2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEvent2Dlg, CDialog)
	//{{AFX_MSG_MAP(CEvent2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_EXIT, OnExit)
	ON_BN_CLICKED(ID_START, OnStart)
	ON_BN_CLICKED(ID_STOP, OnStop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEvent2Dlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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_strIPAddress="172.18.1.41";
	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	m_strIPAddress2="172.18.1.211";
	SetDlgItemText(IDC_EDIT_IPADDRESS2,m_strIPAddress2);

	// get the DLL version 
	char   szCh[20];
    int    iVersion;    
 
	iVersion=ADAM5KTCP_GetDLLVersion();
	_ltoa( iVersion, szCh, 16 );
	SetDlgItemText(IDC_EDIT_DLLVER, szCh);

    //--- To initialize DLL to work ---
    int   iRetVal;

    iRetVal=ADAM5KTCP_Open();
    if( iRetVal!=0 )
    {
        MessageBox("ADAM5KTCP_Open() Failure !!!");
    }

    (CButton *)GetDlgItem(ID_STOP)->EnableWindow( FALSE );
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEvent2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CEvent2Dlg::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 CEvent2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEvent2Dlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();

    //--- Finally, calling ADAM5KTCP_Close() to release all allocated resource  ---
    ADAM5KTCP_Close();
}

void CEvent2Dlg::OnStart() 
{
	// TODO: Add your control notification handler code here
    //char      szDump[80];
	int       iRetVal; 
    CString   strText,strTemp;

   (CButton *)GetDlgItem(ID_START)->EnableWindow( FALSE );
   (CButton *)GetDlgItem(ID_STOP)->EnableWindow( TRUE );
   (CButton *)GetDlgItem(ID_EXIT)->EnableWindow( FALSE );

	m_bStopThread = FALSE;
	m_pThread = NULL;

    m_iTotalReceive=0;
    m_iTotalTimeout=0;
    m_iThreadIndex=0;

    // get the 1st 5000/TCP's IP Address
	GetDlgItemText(IDC_EDIT_IPADDRESS,szIPAddress,20);
    // get the 2nd 5000/TCP's IP Address
	GetDlgItemText(IDC_EDIT_IPADDRESS2,szIPAddress2,20);

    //-----  create a event for synchronization ---------------
    hEvent = CreateEvent(NULL,
			     FALSE,   // auto reset
			     FALSE,  // init state as Non-Signal
			     NULL);

    //--- adding the first 5000/TCP for streaming ----
    iRetVal=ADAM5KTCP_Add5KTCPForStream(szIPAddress);
    if( iRetVal<0 )
    {
		MessageBox("ADAMTCP_Add5KTCPForStream() Failure !!!");
    }

    //--- adding the second 5000/TCP for streaming ----
    iRetVal=ADAM5KTCP_Add5KTCPForStream(szIPAddress2);
    if( iRetVal<0 )
    {
		MessageBox("ADAMTCP_Add5KTCPForStream() Failure !!!");
    }

	//sprintf(szDump,"Total Receive:%d   Timeout=%d  index=%d",
	//				m_iTotalReceive,m_iTotalTimeout,m_iThreadIndex);
	//MessageBox( TEXT(szDump) );

	((CListBox*)GetDlgItem(IDC_LIST_STREAM))->ResetContent();
    m_pListBox=(CListBox*)GetDlgItem(IDC_LIST_STREAM);
	((CListBox*)GetDlgItem(IDC_LIST_STREAM2))->ResetContent();
    m_pListBox2=(CListBox*)GetDlgItem(IDC_LIST_STREAM2);

	if( m_pThread == NULL )
	{
		m_ThreadData.hEvent = &hEvent;
		m_ThreadData.lpStopThread = &m_bStopThread;
		m_ThreadData.TotalReceive=&m_iTotalReceive;
        m_ThreadData.TotalTimeout=&m_iTotalTimeout;
        m_ThreadData.ThreadIndex=&m_iThreadIndex;
		m_ThreadData.pListBox=m_pListBox;
		m_ThreadData.pListBox2=m_pListBox2;
  	    // Start the thread.
		m_pThread =
			AfxBeginThread(ThreadProc, (LPVOID) &m_ThreadData);
	}

    //--- Starting to receive the streaming data come from 5000/TCP ---
    ADAM5KTCP_StartStream( &hEvent);
}

void CEvent2Dlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	HANDLE hThread = m_pThread->m_hThread;

	(CButton *)GetDlgItem(ID_START)->EnableWindow( TRUE );
    (CButton *)GetDlgItem(ID_STOP)->EnableWindow( FALSE );
	(CButton *)GetDlgItem(ID_EXIT)->EnableWindow( TRUE );

	// Set the flag to kill the thread to TRUE;
    m_bStopThread = TRUE;

	// Wait for the thread to end.
	::WaitForSingleObject( hThread, 1000 );

    //--- Stop receiving the streaming data come from 5000/TCP ---	
	ADAM5KTCP_StopStream( );
    //--- Close the event handle ---
    CloseHandle(hEvent);

    m_iTotalReceive=0;
    m_iTotalTimeout=0;
    m_iThreadIndex=0;
}


UINT ThreadProc( LPVOID lpParam )
{
	THREAD_DATA *lpThreadData =
		(THREAD_DATA *) lpParam;
    BOOL   *bStopThread=(BOOL*)lpParam ; 
	int    iRetVal;
    CString   strText; 
	int    iReceive=0,iReceive2=0;



	while( !(*lpThreadData->lpStopThread) )
	{
		*lpThreadData->ThreadIndex=101;
	    if(WaitForSingleObject(*lpThreadData->hEvent,2000)==WAIT_OBJECT_0)  // waiting for 5 seconds
	    { 
              ADAM5KTCP_Debug(&iMatchIndex, &iReceiveCount, &iThreadRun, &iTotalStream, szFromIP);
              switch( iMatchIndex )
			  {
			    case 0: 
			     if( iReceive%3==2 )
				 {
				   (lpThreadData->pListBox)->ResetContent();
				 }
			     iReceive++;
	 		     // reading stream data from 1st 5000/TCP
                 iRetVal=ADAM5KTCP_ReadStreamData(szIPAddress,&StreamData);
                 pStreamData=(struct _StreamData *)&StreamData;

		         strText.Format( "Receive:%d", iReceive);
		         (lpThreadData->pListBox)->InsertString( -1, strText );
			  
	             strText.Format ("DI/DO data of Slot0 to Slot 7:\n");
			     (lpThreadData->pListBox)->InsertString( -1, strText );
			     strText.Format("%04x:%04x:%04x:%04x-%04x:%04x:%04x:%04x\n",StreamData.DIO[0],StreamData.DIO[1],
							       StreamData.DIO[2],StreamData.DIO[3],
							       StreamData.DIO[4],StreamData.DIO[5],	
							       StreamData.DIO[6],StreamData.DIO[7]);
			     (lpThreadData->pListBox)->InsertString( -1, strText );
			     strText.Format("AI/Ao data of Slot0:\n");
			     (lpThreadData->pListBox)->InsertString( -1, strText );
                 strText.Format("%04x:%04x:%04x:%04x-%04x:%04x:%04x:%04x\n",StreamData.Slot0[0],StreamData.Slot0[1],
							       StreamData.Slot0[2],StreamData.Slot0[3],
							       StreamData.Slot0[4],StreamData.Slot0[5],
							       StreamData.Slot0[6],StreamData.Slot0[7]);	
			     (lpThreadData->pListBox)->InsertString( -1, strText );
			     strText.Format("AI/AO data of Slot1:\n");
			     (lpThreadData->pListBox)->InsertString( -1, strText );
                 strText.Format("%04x:%04x:%04x:%04x-%04x:%04x:%04x:%04x\n",StreamData.Slot1[0],StreamData.Slot1[1],
							       StreamData.Slot1[2],StreamData.Slot1[3],
							       StreamData.Slot1[4],StreamData.Slot1[5],
							       StreamData.Slot1[6],StreamData.Slot1[7]);			
			     (lpThreadData->pListBox)->InsertString( -1, strText );

                 break;
				case 1:  
			     if( iReceive2%3==2 )
				 {
				   (lpThreadData->pListBox2)->ResetContent();
				 }
			     iReceive2++;
			     // reading stream data from 2nd 5000/TCP
                 iRetVal=ADAM5KTCP_ReadStreamData(szIPAddress2,&StreamData);
		         strText.Format( "Receive:%d",iReceive2);
		         (lpThreadData->pListBox2)->InsertString( -1, strText );
			  
	             strText.Format ("DI/DO data of Slot0 to Slot 7:\n");
			     (lpThreadData->pListBox2)->InsertString( -1, strText );
			     strText.Format("%04x:%04x:%04x:%04x-%04x:%04x:%04x:%04x\n",StreamData.DIO[0],StreamData.DIO[1],
							       StreamData.DIO[2],StreamData.DIO[3],
							       StreamData.DIO[4],StreamData.DIO[5],	
							       StreamData.DIO[6],StreamData.DIO[7]);
			     (lpThreadData->pListBox2)->InsertString( -1, strText );
			     strText.Format("AI/Ao data of Slot0:\n");
			     (lpThreadData->pListBox2)->InsertString( -1, strText );
                 strText.Format("%04x:%04x:%04x:%04x-%04x:%04x:%04x:%04x\n",StreamData.Slot0[0],StreamData.Slot0[1],
							       StreamData.Slot0[2],StreamData.Slot0[3],
							       StreamData.Slot0[4],StreamData.Slot0[5],
							       StreamData.Slot0[6],StreamData.Slot0[7]);	
			     (lpThreadData->pListBox2)->InsertString( -1, strText );
			     strText.Format("AI/AO data of Slot1:\n");
			     (lpThreadData->pListBox2)->InsertString( -1, strText );
                 strText.Format("%04x:%04x:%04x:%04x-%04x:%04x:%04x:%04x\n",StreamData.Slot1[0],StreamData.Slot1[1],
							       StreamData.Slot1[2],StreamData.Slot1[3],
							       StreamData.Slot1[4],StreamData.Slot1[5],
							       StreamData.Slot1[6],StreamData.Slot1[7]);			
			     (lpThreadData->pListBox2)->InsertString( -1, strText );
				 break;
			  }

		}
		else
		{
			(*lpThreadData->TotalTimeout)++;
		}

	}
	(*lpThreadData->ThreadIndex)=102;
	return 0;
}

⌨️ 快捷键说明

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