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

📄 diintdlg.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
// MDiIntDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DiInt.h"
#include "DiIntDlg.h"
#include "..\..\..\..\include\Driver.h"

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

typedef struct _PT_ThreadArg
{
	LONG	m_lDriverHandle;
	HWND	m_hWnd;
//	PT_CheckEvent	ptCheckEvent;
} PT_ThreadArg, * LPT_ThreadArg;

static PT_CheckEvent	ptCheckEvent;
static PT_ThreadArg		ptThreadArg;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMDiIntDlg dialog

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



}

void CMDiIntDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMDiIntDlg)
	DDX_Control(pDX, IDC_DISABLEBTN, m_DisableBtn);
	DDX_Control(pDX, IDC_ENABLEBTN, m_EnableBtn);
	DDX_Control(pDX, IDC_DEVICENAME, m_DeviceName);
	DDX_Text(pDX, IDC_COUNT, m_uCount);
	DDV_MinMaxInt(pDX, m_uCount, 1, 10000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMDiIntDlg, CDialog)
	//{{AFX_MSG_MAP(CMDiIntDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ENABLEBTN, OnEnablebtn)
	ON_BN_CLICKED(IDS_EVENTPRIORITY1, OnEventpriority1)
	ON_BN_CLICKED(IDS_EVENTPRIORITY2, OnEventpriority2)
	ON_BN_CLICKED(IDC_DISABLEBTN, OnDisablebtn)
	ON_BN_CLICKED(IDCLOSE, OnClose)
   ON_BN_CLICKED(IDD_ABOUTBOX, OnAboutbox)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMDiIntDlg message handlers

BOOL CMDiIntDlg::OnInitDialog()
{
	
	UCHAR	DeviceName[50];


	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
	

	// initial some value
	m_DriverHandle = (LONG)NULL;          // driver handle
	m_bContinue = FALSE;
	m_bHiPriority = FALSE;
	SendDlgItemMessage( IDS_EVENTPRIORITY1, BM_SETCHECK, 1, 0 );

	//Set the interrupt channel
	for (int i = 0; i < 8 ; i++)
	{
		SendDlgItemMessage(IDC_DI0+i,BM_SETCHECK,1,0);
	}

	// 1. select device
	m_ulDevNum = 0;		// initail device number
	DRV_SelectDevice( m_hWnd, FALSE, &m_ulDevNum, DeviceName );
	m_DeviceName.SetWindowText( (LPCTSTR)DeviceName );			// show device name

	// device open
	m_ErrCde = DRV_DeviceOpen( m_ulDevNum, &m_DriverHandle );
	if ( m_ErrCde != SUCCESS )
		return FALSE;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMDiIntDlg::OnEnablebtn() 
{
	// TODO: Add your control notification handler code here

	PT_EnableEvent	 EventSetting;
	CWinThread*     pThreadObject;
    USHORT          usEvent;
	CWnd*           pWnd;

	UpdateData(TRUE);
    usEvent = ADS_EVT_INTERRUPT_DI0;
	EventSetting.Enabled = TRUE;
	EventSetting.Count = m_uCount;
	for ( int i = 0; i < 8; i++ )
	{
		if (BST_CHECKED ==SendDlgItemMessage(IDC_DI0+i,BM_GETSTATE,0,0))
		{
			EventSetting.EventType = usEvent + i;
		    m_ErrCde = DRV_EnableEvent( m_DriverHandle, &EventSetting );
		}
		pWnd = GetDlgItem(IDC_DI0+i);
		pWnd->EnableWindow(FALSE);
	}
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}
	m_bContinue = TRUE;
	m_DisableBtn.EnableWindow( TRUE );
	m_EnableBtn.EnableWindow( FALSE );
	ptThreadArg.m_lDriverHandle = m_DriverHandle;
	ptThreadArg.m_hWnd = GetSafeHwnd();
	if ( m_bHiPriority ) 
   {
		//
		// Using THREAD_PRIORITY_TIME_CRITICAL for interrupt event handling routine,
		// you can get about 20 KHz response ratio by using PENTIUM 200 MMX PC.
		pThreadObject = AfxBeginThread( CMDiIntDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_TIME_CRITICAL, 0, 0, NULL );
	   m_pThreadHandle = pThreadObject->m_hThread;
	} 
   else 
   {
		//
		// Using THREAD_PRIORITY_NORMAL for interrupt event handling routine,
		// you can get about 1.2 ~ 1.5 KHz response ratio by using PENTIUM 200 MMX PC.
		pThreadObject = AfxBeginThread( CMDiIntDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_NORMAL, 0, 0, NULL );
	   m_pThreadHandle = pThreadObject->m_hThread;
	}	
}

UINT CMDiIntDlg::EventThread(LPVOID ThreadArg)
{
	DWORD	dwStartTime;
	DWORD	dwCurrentTime;
	DWORD	dwTime;
	DWORD	dwEventCount = 0;
	float	ratio;
	char	szBuf[20];
	USHORT	usEventType;
	PT_ThreadArg	*ptTArg;
	char	szStatus[10];

	strcpy(szStatus,"00000000");		   // reset interrupt source status
	ptTArg = (PT_ThreadArg *)ThreadArg;	// parameter from AfxBeginThread

	ptCheckEvent.EventType = &usEventType;
	ptCheckEvent.Milliseconds = 1000;	// waiting time 1000 ms

	dwStartTime = GetTickCount();
	while ( m_bContinue ) 
	{
		m_bFlag = TRUE;
		if (DRV_CheckEvent( ptTArg->m_lDriverHandle, &ptCheckEvent ) == SUCCESS) 
		{
			if(usEventType >= ADS_EVT_INTERRUPT_DI0 && 
				usEventType <= ADS_EVT_INTERRUPT_DI7) 
				dwEventCount++;
			
			switch(usEventType)
			{
			case ADS_EVT_INTERRUPT_DI0 :		// interrupt event from DI0
				strcpy(szStatus,"00000001");
				break;
			case ADS_EVT_INTERRUPT_DI1 :		// interrupt event from DI1
				strcpy(szStatus,"00000010");
				break;
			case ADS_EVT_INTERRUPT_DI2 :		// interrupt event from DI2
				strcpy(szStatus,"00000100");
				break;
			case ADS_EVT_INTERRUPT_DI3 :		// interrupt event from DI3
				strcpy(szStatus,"00001000");
				break;
			case ADS_EVT_INTERRUPT_DI4 :		// interrupt event from DI4
				strcpy(szStatus,"00010000");
				break;
			case ADS_EVT_INTERRUPT_DI5 :		// interrupt event from DI5 
				strcpy(szStatus,"00100000");
				break;
			case ADS_EVT_INTERRUPT_DI6 :		// interrupt event from DI6
				strcpy(szStatus,"01000000");
				break;
			case ADS_EVT_INTERRUPT_DI7 :		// interrupt event from DI7
				strcpy(szStatus,"10000000");
				break;
			default :strcpy(szStatus,"00000000");
				break;	

			}
//			::SetDlgItemText( ptTArg->m_hWnd, IDC_EVENTSTATUS, (LPCTSTR)szStatus );
			
		}
		m_bFlag = FALSE;
		
		dwCurrentTime = GetTickCount();
		if ((dwTime = dwCurrentTime - dwStartTime) > 1000) 
		{
			ratio = (float)(((float)dwEventCount / (float)dwTime) * 1000.0);
			sprintf( szBuf, "%.3f", ratio );
			::SetDlgItemText( ptTArg->m_hWnd, IDC_INTERRUPTCOUNT, szBuf );
			dwStartTime = GetTickCount();
			dwEventCount = 0;
		}		
	}
	::SetDlgItemText( ptTArg->m_hWnd, IDC_INTERRUPTCOUNT, " " );
//	::SetDlgItemText( ptTArg->m_hWnd, IDC_EVENTSTATUS, " " );
	
	return 0;
}

void CMDiIntDlg::OnDisablebtn() 
{
	// TODO: Add your control notification handler code here
	PT_EnableEvent	EventSetting;
    USHORT          usEvent;
	CWnd*           pWnd;
	BOOL flag = FALSE;
	m_bContinue = FALSE;
    UpdateData(TRUE);
    while(!flag)
	{
	   m_DisableBtn.EnableWindow( FALSE );
	   m_EnableBtn.EnableWindow( TRUE );	
	   usEvent = ADS_EVT_INTERRUPT_DI0;
	   EventSetting.Enabled = FALSE;
	   EventSetting.Count = 1;
	   for ( int i = 0; i < 8; i++ )
	   {
		   if (BST_CHECKED ==SendDlgItemMessage(IDC_DI0+i,BM_GETSTATE,0,0))
		   {
			   EventSetting.EventType = usEvent + i;
			   m_ErrCde = DRV_EnableEvent( m_DriverHandle, &EventSetting );
		   }
		   pWnd = GetDlgItem(IDC_DI0+i);
		   pWnd->EnableWindow(TRUE);
	   }
	   
	   Sleep(100);
	   m_bContinue = FALSE;
	   flag = TRUE;
	   if ( m_ErrCde != 0) 
	   {
	      DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		   MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		   return;   
	   }	  
	}	
}


void CMDiIntDlg::OnEventpriority1() 
{
	// TODO: Add your control notification handler code here
	m_bHiPriority = FALSE;	
}

void CMDiIntDlg::OnEventpriority2() 
{
	// TODO: Add your control notification handler code here
	m_bHiPriority = TRUE;	
}

void CMDiIntDlg::OnCancel() 
{
	// TODO: Add extra cleanup here

	// call OnDisablebtn()
//	OnDisablebtn(); 
	// device close
	DRV_DeviceClose( &m_DriverHandle );
	
	CDialog::OnCancel();
}

void CMDiIntDlg::OnAboutbox()
{
	// TODO: Add your control notification handler code here
	CAboutDlg aboutDlg;

	aboutDlg.DoModal();
}

⌨️ 快捷键说明

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