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

📄 tmcntsetdlg.cpp

📁 PCI 例程
💻 CPP
字号:
// TmCntSetDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TmCntSet.h"
#include "TmCntSetDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CTmCntSetDlg dialog

CTmCntSetDlg::CTmCntSetDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTmCntSetDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTmCntSetDlg)
	m_nCounter0Val = 10;
	m_nCounter1Val = 10;
	m_nCounter2Val = 10;
	m_uCount = 1;
	m_bEventGroup0 = FALSE;
	m_bEventGroup1 = FALSE;
	m_bEventInt = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_DriverHandle = (LONG)NULL;          // driver handle
	m_bContinue = FALSE;
	m_bHiPriority = FALSE;
}

void CTmCntSetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTmCntSetDlg)
	DDX_Control(pDX, IDC_DEVICENAME, m_DeviceName);
	DDX_Control(pDX, IDS_SELECTDEVICE, m_SelectDeviceBtn);
	DDX_Control(pDX, IDOK, m_ExitBtn);
	DDX_Control(pDX, IDC_DISABLEBTN, m_DisableBtn);
	DDX_Control(pDX, IDC_ENABLEBTN, m_EnableBtn);
	DDX_Text(pDX, IDC_CounterVal0, m_nCounter0Val);
	DDV_MinMaxUInt(pDX, m_nCounter0Val, 2, 65535);
	DDX_Text(pDX, IDC_CounterVal1, m_nCounter1Val);
	DDV_MinMaxUInt(pDX, m_nCounter1Val, 2, 65535);
	DDX_Text(pDX, IDC_CounterVal2, m_nCounter2Val);
	DDV_MinMaxUInt(pDX, m_nCounter2Val, 2, 65535);
	DDX_Text(pDX, IDC_COUNT, m_uCount);
	DDV_MinMaxUInt(pDX, m_uCount, 1, 65535);
	DDX_Check(pDX, IDC_EventGroup0, m_bEventGroup0);
	DDX_Check(pDX, IDC_EventGroup1, m_bEventGroup1);
	DDX_Check(pDX, IDC_EventInt, m_bEventInt);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTmCntSetDlg, CDialog)
	//{{AFX_MSG_MAP(CTmCntSetDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDS_SELECTDEVICE, OnSelectdevice)
	ON_BN_CLICKED(IDS_ABOUTBOX, OnAboutbox)
	ON_BN_CLICKED(IDC_ENABLEBTN, OnEnablebtn)
	ON_BN_CLICKED(IDC_DISABLEBTN, OnDisablebtn)
	ON_BN_CLICKED(IDOK, OnClose)
	ON_BN_CLICKED(IDS_EVENTPRIORITY1, OnEventpriority1)
	ON_BN_CLICKED(IDS_EVENTPRIORITY2, OnEventpriority2)
	ON_EN_CHANGE(IDC_CounterVal0, OnChangeCounterVal0)
	ON_EN_CHANGE(IDC_CounterVal1, OnChangeCounterVal1)
	ON_EN_CHANGE(IDC_CounterVal2, OnChangeCounterVal2)
	ON_BN_CLICKED(IDC_EventInt, OnEventInt)
	ON_BN_CLICKED(IDC_EventGroup0, OnEventGroup0)
	ON_BN_CLICKED(IDC_EventGroup1, OnEventGroup1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTmCntSetDlg message handlers

BOOL CTmCntSetDlg::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
	
	// TODO: Add extra initialization here
	m_ulDevNum = 0;
	DRV_SelectDevice( m_hWnd, FALSE, &m_ulDevNum, DeviceName );
	m_DeviceName.SetWindowText( (LPCTSTR)DeviceName );
	SendDlgItemMessage( IDS_EVENTPRIORITY1, BM_SETCHECK, 1, 0 );
	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 CTmCntSetDlg::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 CTmCntSetDlg::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 CTmCntSetDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTmCntSetDlg::OnSelectdevice() 
{
	UCHAR	DeviceName[50];

	// TODO: Add your control notification handler code here
	if ( m_DriverHandle != NULL )
		DRV_DeviceClose( &m_DriverHandle );
	DRV_SelectDevice( m_hWnd, FALSE, &m_ulDevNum, DeviceName );
	m_DeviceName.SetWindowText( (LPCTSTR)DeviceName );
	m_ErrCde = DRV_DeviceOpen( m_ulDevNum, &m_DriverHandle);
}

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

	aboutDlg.DoModal();	
}

void CTmCntSetDlg::OnEnablebtn() 
{
	// TODO: Add your control notification handler code here
	PT_EnableEvent	EventSetting;
	PT_TimerCountSetting	CounterSetting;
	ULONG	CounterVal=0x00;
	UpdateData(TRUE);

	// Test if Supportted Device!!!
	CounterSetting.counter = 2;
	CounterSetting.Count = 0x0002;
	m_ErrCde = DRV_TimerCountSetting(m_DriverHandle, &CounterSetting);
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	// Real Start
	EventSetting.EventType = NULL;
	if( m_bEventInt )
		EventSetting.EventType |= ADS_EVT_INTERRUPT;
	if( m_bEventGroup0 )
		EventSetting.EventType |= ADS_EVT_PORT0;
	if( m_bEventGroup1 )
		EventSetting.EventType |= ADS_EVT_PORT1;

	// Counter Value Set
	CounterSetting.counter = 2;
	CounterSetting.Count = m_nCounter2Val;
	m_ErrCde = DRV_TimerCountSetting(m_DriverHandle, &CounterSetting);
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	CounterVal = (m_nCounter0Val << 16) + m_nCounter1Val;
	CounterSetting.counter = 1;
	CounterSetting.Count = CounterVal;
	m_ErrCde = DRV_TimerCountSetting(m_DriverHandle, &CounterSetting);
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	CounterSetting.counter = 0;
	m_ErrCde = DRV_TimerCountSetting(m_DriverHandle, &CounterSetting);
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	// Enable Event
	EventSetting.Enabled = TRUE;
	EventSetting.Count = m_uCount;
	m_ErrCde = DRV_EnableEvent( m_DriverHandle, &EventSetting );
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}
  
	m_DisableBtn.EnableWindow( TRUE );
	m_EnableBtn.EnableWindow( FALSE );
	m_ExitBtn.EnableWindow (FALSE );
	m_SelectDeviceBtn.EnableWindow( FALSE );
	m_bContinue = TRUE;
	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.
		pThread=AfxBeginThread( CTmCntSetDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_TIME_CRITICAL, 0, 0, NULL );
	} 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.
		pThread=AfxBeginThread( CTmCntSetDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_NORMAL, 0, 0, NULL );
	}
	
}

void CTmCntSetDlg::OnDisablebtn() 
{
	// TODO: Add your control notification handler code here
	PT_EnableEvent	EventSetting;
	DWORD dwExitCode;

	m_DisableBtn.EnableWindow( FALSE );
	m_EnableBtn.EnableWindow( TRUE );
	m_ExitBtn.EnableWindow (TRUE );
	m_SelectDeviceBtn.EnableWindow( TRUE );


	EventSetting.EventType = ADS_EVT_INTERRUPT;
	EventSetting.Enabled = FALSE;
	EventSetting.Count = 1;
	m_ErrCde = DRV_EnableEvent( m_DriverHandle, &EventSetting );
	Sleep(100L);
	m_bContinue = FALSE;
	if(pThread->m_hThread)
	   GetExitCodeThread(pThread->m_hThread,&dwExitCode);
	if(dwExitCode==STILL_ACTIVE)
		TerminateThread(pThread->m_hThread,dwExitCode);
	if ( m_ErrCde != 0) {
		DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}
	
}

void CTmCntSetDlg::OnClose() 
{
	// TODO: Add your control notification handler code here
	if ( m_DriverHandle	!= NULL )
		DRV_DeviceClose( &m_DriverHandle );

	CDialog::OnClose();
	CDialog::OnOK();
	
}

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

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

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

	ptTArg = (PT_ThreadArg *)ThreadArg;
	ptCheckEvent.EventType = &usEventType;
	ptCheckEvent.Milliseconds = 1000;
	dwStartTime = GetTickCount();
	while ( m_bContinue ) {
		if (DRV_CheckEvent( ptTArg->m_lDriverHandle, &ptCheckEvent ) == SUCCESS) {
			dwEventCount++;
		}
		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;
		}
	}
	return 0;
}

void CTmCntSetDlg::OnChangeCounterVal0() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_CHANGE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
}

void CTmCntSetDlg::OnChangeCounterVal1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_CHANGE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
}

void CTmCntSetDlg::OnChangeCounterVal2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_CHANGE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
}

void CTmCntSetDlg::OnEventInt() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
}

void CTmCntSetDlg::OnEventGroup0() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

}

void CTmCntSetDlg::OnEventGroup1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	
}

⌨️ 快捷键说明

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