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

📄 tmcntsetdlg.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 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_ThreadArg, * LPT_ThreadArg;

static PT_CheckEvent	ptCheckEvent;
static PT_ThreadArg		ptThreadArg;

static BOOL m_bCountine;
/////////////////////////////////////////////////////////////////////////////
// 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_TimerInterval = 20;
	m_bCountine = FALSE;
	pThread = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTmCntSetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTmCntSetDlg)
	DDX_Control(pDX, IDC_SELECTDEVICE, m_btnSelectDev);
	DDX_Control(pDX, IDOK, m_btnClose);
	DDX_Control(pDX, IDC_EVENTEN, m_btnEnable);
	DDX_Control(pDX, IDC_EVENTDIS, m_btnDisable);
	DDX_Control(pDX, IDC_TIMERINTERREAL, m_TimerIntervalReal);
	DDX_Control(pDX, IDC_UNIT, m_UnitReal);
	DDX_Control(pDX, IDC_EVENTCOUNT, m_EventCount);
	DDX_Control(pDX, IDC_DEVICENAME, m_DeviceName);
	DDX_Text(pDX, IDC_TIMERINTER, m_TimerInterval);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTmCntSetDlg, CDialog)
	//{{AFX_MSG_MAP(CTmCntSetDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELECTDEVICE, OnSelectdevice)
	ON_BN_CLICKED(IDC_SETTIMER, OnSettimer)
	ON_BN_CLICKED(IDC_RADMS, OnRadms)
	ON_BN_CLICKED(IDC_RADUS, OnRadus)
	ON_BN_CLICKED(IDC_RADS, OnRads)
	ON_EN_CHANGE(IDC_TIMERINTER, OnChangeTimerinter)
	ON_BN_CLICKED(IDC_EVENTEN, OnEventen)
	ON_BN_CLICKED(IDC_EVENTDIS, OnEventdis)
	//}}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((LPCSTR)DeviceName);
	m_ErrCode = DRV_DeviceOpen(m_ulDevNum, &m_DriverHandle);
	if (m_ErrCode != SUCCESS)
	{
		m_DriverHandle = 0;
		return FALSE;
	}
	
	SendDlgItemMessage(IDC_RADUS, BM_SETCHECK, 1, 0);
	m_usUnit = 1;

	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() 
{
	// TODO: Add your control notification handler code here
	UCHAR DeviceName[50];

	if (m_DriverHandle != NULL)
		DRV_DeviceClose(&m_DriverHandle);

	DRV_SelectDevice(m_hWnd, FALSE, &m_ulDevNum, DeviceName);
	m_DeviceName.SetWindowText((LPCSTR)DeviceName);
	m_ErrCode = DRV_DeviceOpen(m_ulDevNum, &m_DriverHandle);

}

void CTmCntSetDlg::OnSettimer() 
{
	ULONG ulTimeInterval;
	ULONG ulSize;
	USHORT usDividor;
	float fFreq, fFreqBase;
	char szBuf[20], szUnit[5];
	float* pTimerFreq;
	PT_TimerCountSetting ptTimerCountSetting;

	// TODO: Add your control notification handler code here

	if (m_usUnit == 3) // s
		ulTimeInterval = m_TimerInterval * 1000000;
	else if (m_usUnit == 2) // ms
		ulTimeInterval = m_TimerInterval * 1000;
	else // us
		ulTimeInterval = m_TimerInterval;

	fFreq = (float)(1000000.0 / ulTimeInterval); // HZ

	if (fFreq > TIMER_BASE_5KHZ)
		fFreqBase = TIMER_BASE_50KHZ;
	else if ((fFreq <= TIMER_BASE_5KHZ) && (fFreq > TIMER_BASE_500HZ))
		fFreqBase = TIMER_BASE_5KHZ;
	else if ((fFreq <= TIMER_BASE_500HZ) && (fFreq > TIMER_BASE_50HZ))
		fFreqBase = TIMER_BASE_500HZ;
	else if ((fFreq <= TIMER_BASE_50HZ) && (fFreq > TIMER_BASE_5HZ))
		fFreqBase = TIMER_BASE_50HZ;
	else
		fFreqBase = TIMER_BASE_5HZ;

	usDividor = (USHORT)(ulTimeInterval * fFreqBase / 1000000 + 0.5);
	if (usDividor == 0)
		usDividor = 1;

	// Set Timer Base
	m_ErrCode = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrClockFrequency, NULL, &ulSize);
	if (m_ErrCode == SUCCESS)
	{
		pTimerFreq = new float[ulSize / sizeof(float)];
		m_ErrCode = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrClockFrequency, pTimerFreq, &ulSize);
		pTimerFreq[4] = fFreq; // counter 4 is a timer
		m_ErrCode = DRV_DeviceSetProperty(m_DriverHandle, CFG_CntrClockFrequency, pTimerFreq, ulSize);
		delete[] pTimerFreq;
	}
	
	if (m_ErrCode != SUCCESS)
	{
		DRV_GetErrorMessage( m_ErrCode, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	// PCI-1784 Counter 4 is a timer
	ptTimerCountSetting.Count = usDividor; 
	ptTimerCountSetting.counter = 4;
	m_ErrCode = DRV_TimerCountSetting(m_DriverHandle, &ptTimerCountSetting);
	if (m_ErrCode != SUCCESS)
	{
		DRV_GetErrorMessage( m_ErrCode, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	// Real timer interval
	ulTimeInterval = 1000000 / fFreqBase * usDividor;
	if (ulTimeInterval >= 1000000)
	{
		ulTimeInterval = ulTimeInterval / 1000000;
		sprintf(szUnit, "s");
	}
	else if ((ulTimeInterval >= 1000) && (ulTimeInterval < 1000000))
	{
		ulTimeInterval = ulTimeInterval / 1000;
		sprintf(szUnit, "ms");
	}
	else
	{
		sprintf(szUnit, "us");
	}
	sprintf(szBuf, "%d", ulTimeInterval);
	
	m_TimerIntervalReal.SetWindowText((LPCSTR)szBuf);
	m_UnitReal.SetWindowText((LPCSTR)szUnit);

	UpdateData(FALSE);
}

void CTmCntSetDlg::OnRadms() 
{
	// TODO: Add your control notification handler code here
	m_usUnit = 2;
}

void CTmCntSetDlg::OnRadus() 
{
	// TODO: Add your control notification handler code here
	m_usUnit = 1;
}

void CTmCntSetDlg::OnRads() 
{
	// TODO: Add your control notification handler code here
	m_usUnit = 3;
}

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

void CTmCntSetDlg::OnEventen() 
{
	PT_EnableEvent ptEventSetting;
	// TODO: Add your control notification handler code here
	
	// Enable Event
	ptEventSetting.Count = 1;
	ptEventSetting.Enabled = TRUE;
	ptEventSetting.EventType = ADS_EVT_INTERRUPT_TIMER4;
	m_ErrCode = DRV_EnableEvent(m_DriverHandle, &ptEventSetting);
	if (m_ErrCode != SUCCESS)
	{
		DRV_GetErrorMessage( m_ErrCode, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	m_btnEnable.EnableWindow( FALSE );
	m_btnDisable.EnableWindow(TRUE);
	m_btnClose.EnableWindow(FALSE);
	m_btnSelectDev.EnableWindow(FALSE);

	m_bCountine = TRUE;
	ptThreadArg.m_hWnd = GetSafeHwnd();
	ptThreadArg.m_lDriverHandle = m_DriverHandle;
	pThread = AfxBeginThread( CTmCntSetDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_NORMAL, 0, 0, NULL );

}

UINT CTmCntSetDlg::EventThread(LPVOID ThreadArg)
{
	PT_ThreadArg *ptTArg;
	USHORT usEventType;
	DWORD dwEventCount;
	char szBuf[10];

	ptTArg = (PT_ThreadArg*)ThreadArg;
	ptCheckEvent.EventType = &usEventType;
	ptCheckEvent.Milliseconds = 1000;
	
	dwEventCount = 0;
	while (m_bCountine)
	{
		if (DRV_CheckEvent(ptTArg->m_lDriverHandle, &ptCheckEvent) == SUCCESS)
		{
			if (usEventType == ADS_EVT_INTERRUPT_TIMER4)
			{
				dwEventCount++;
				sprintf(szBuf, "%ld", dwEventCount);
				::SetDlgItemText( ptTArg->m_hWnd, IDC_EVENTCOUNT, szBuf );
			}
		}
	}

	return 0;
}

void CTmCntSetDlg::OnEventdis() 
{
	PT_EnableEvent ptEnableEvent;
	DWORD dwExitCode;
	// TODO: Add your control notification handler code here
	
	m_btnEnable.EnableWindow(TRUE);
	m_btnDisable.EnableWindow(FALSE);
	m_btnClose.EnableWindow(TRUE);
	m_btnSelectDev.EnableWindow(TRUE);

	ptEnableEvent.Count = 1;
	ptEnableEvent.Enabled = FALSE;
	ptEnableEvent.EventType = ADS_EVT_INTERRUPT_TIMER4;
	m_ErrCode = DRV_EnableEvent(m_DriverHandle, &ptEnableEvent);
	m_bCountine = FALSE;
	
	if (pThread)
		GetExitCodeThread(pThread->m_hThread, &dwExitCode);
	if (dwExitCode == STILL_ACTIVE)
		TerminateThread(pThread->m_hThread, dwExitCode);
	
	if ( m_ErrCode != 0) {
		DRV_GetErrorMessage( m_ErrCode, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}
	
}

void CTmCntSetDlg::OnOK() 
{
	// TODO: Add extra validation here
	ULONG dwExitCode;

	m_bCountine = FALSE;

	if (pThread)
		GetExitCodeThread(pThread->m_hThread, &dwExitCode);
	if (dwExitCode == STILL_ACTIVE)
		TerminateThread(pThread->m_hThread, dwExitCode);
	
	if (m_DriverHandle != NULL)
		DRV_DeviceClose(&m_DriverHandle);
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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