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

📄 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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
typedef struct _PT_ThreadArg
{
	LONG	lDriverHandle;
	HWND	hWnd;
} PT_ThreadArg, * LPT_ThreadArg;

static PT_ThreadArg		      ptThreadArg;
HANDLE   hThread = NULL;
UINT UserThread(LPVOID lpPara);
DWORD dwThreadID = 0;

PT_QCounterConfigSys          gCounterConfigSys; 
PT_CheckEvent                 gCheckEvent;
PT_EnableEvent                gEnableEvent;
BOOL                          gbCountine = FALSE;

void uShowDriverErrMsg(HWND hWnd, UINT ErrCde)
{
	TCHAR szText[80];
	CString strTitle;
	
	strTitle.LoadString(IDS_TITLE);
	DRV_GetErrorMessage(ErrCde, szText);
	MessageBox(hWnd, szText, strTitle, MB_OK);
}

void uShowMsg(HWND hWnd, UINT uText, UINT uTitle)
{
	CString strText, strTitle;
	
	strTitle.LoadString(uTitle);
	strText.LoadString(uText);
	MessageBox(hWnd, strText, strTitle, MB_OK);
}

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_nTimeDivider = 1;
	m_DriverHandle = NULL;
	//}}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_COMBO_TIMEBASE, m_comboTimeBase);
	DDX_Control(pDX, IDC_DEVICENAME, m_DeviceName);
	DDX_Text(pDX, IDC_EDIT_TD, m_nTimeDivider);
	DDV_MinMaxUInt(pDX, m_nTimeDivider, 1, 255);
	//}}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_BTN_SEL, OnSelectDevice)
	ON_BN_CLICKED(IDC_BTN_SET, OnBtnConfigSys)
	ON_BN_CLICKED(IDC_BTN_ENABLE, OnBtnEnableTimer)
	ON_BN_CLICKED(IDC_BTN_DISABLE, OnBtnDisableTimer)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

BOOL CTmCntSetDlg::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
	DWORD dwRet;
	UCHAR DeviceName[50];
	
    m_ulDevNum = 0;
	DRV_SelectDevice(m_hWnd, FALSE, &m_ulDevNum, DeviceName);
	m_DeviceName.SetWindowText((LPCSTR)DeviceName);
	dwRet = DRV_DeviceOpen(m_ulDevNum, &m_DriverHandle);
	if (dwRet != 0) 
	{
		uShowDriverErrMsg(GetSafeHwnd(), dwRet);
		DRV_DeviceClose(&m_DriverHandle);
        OnCancel();
		return FALSE;
	}
    
	m_comboTimeBase.AddString("0.1ms");
	m_comboTimeBase.AddString("1ms");
	m_comboTimeBase.AddString("10ms");
	m_comboTimeBase.AddString("100ms");
	m_comboTimeBase.AddString("1000ms");
	m_comboTimeBase.SetCurSel(0);
    SetDlgItemText( IDC_EVENTCOUNT, "0" );

	GetDlgItem(IDC_BTN_ENABLE)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DISABLE)->EnableWindow(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() 
{
	// TODO: Add your control notification handler code here
	UCHAR DeviceName[50];
	DWORD dwRet;
	
	GetDlgItem(IDC_BTN_ENABLE)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DISABLE)->EnableWindow(FALSE);

	if (m_DriverHandle != NULL)
		DRV_DeviceClose(&m_DriverHandle);
	
	DRV_SelectDevice(m_hWnd, FALSE, &m_ulDevNum, DeviceName);
	m_DeviceName.SetWindowText((LPCSTR)DeviceName);
	dwRet = DRV_DeviceOpen(m_ulDevNum, &m_DriverHandle);
   if (dwRet != 0) 
   {
	   uShowDriverErrMsg(GetSafeHwnd(), dwRet);
	   DRV_DeviceClose(&m_DriverHandle);
	   return;
   }
}

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

	GetDlgItem(IDC_BTN_ENABLE)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_DISABLE)->EnableWindow(FALSE);

   gCounterConfigSys.CascadeMode = 0;
   gCounterConfigSys.SysClock = 0;
   gCounterConfigSys.TimeBase = m_comboTimeBase.GetCurSel();
   gCounterConfigSys.TimeDivider = m_nTimeDivider;

   dwRet = DRV_QCounterConfigSys(m_DriverHandle, &gCounterConfigSys);
   if (dwRet != 0) 
   {
	   uShowDriverErrMsg(GetSafeHwnd(), dwRet);
	   DRV_DeviceClose(&m_DriverHandle);
	   return;
   }
}

void CTmCntSetDlg::OnBtnEnableTimer() 
{
	// TODO: Add your control notification handler code here
	DWORD dwRet;

	GetDlgItem(IDC_BTN_ENABLE)->EnableWindow(FALSE);
    GetDlgItem(IDC_BTN_DISABLE)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_SEL)->EnableWindow(FALSE);
    GetDlgItem(IDOK)->EnableWindow(FALSE);

    gEnableEvent.Enabled = TRUE;
	gEnableEvent.EventType = ADS_EVT_TC_TIMER;
	dwRet = DRV_EnableEvent(m_DriverHandle, &gEnableEvent);
	if (dwRet != 0) 
	{
		uShowDriverErrMsg(GetSafeHwnd(), dwRet);
		DRV_DeviceClose(&m_DriverHandle);
		return;
    }
    ptThreadArg.hWnd = GetSafeHwnd();
    ptThreadArg.lDriverHandle = m_DriverHandle;

	
	hThread = CreateThread(0, 0,
		            (LPTHREAD_START_ROUTINE)UserThread,
		            &ptThreadArg,
		            CREATE_SUSPENDED,
                    &dwThreadID);
	if (hThread == (HANDLE)NULL)
	{
		uShowDriverErrMsg(GetSafeHwnd(), dwRet);
		DRV_DeviceClose(&m_DriverHandle);
		return;
	}
	// resume the thread to wait for event
	gbCountine = TRUE;
    ResumeThread(hThread);	
}

void CTmCntSetDlg::OnBtnDisableTimer() 
{
	// TODO: Add your control notification handler code here
	   // disable event
	DWORD dwRet;
	GetDlgItem(IDC_BTN_ENABLE)->EnableWindow(TRUE);
    GetDlgItem(IDC_BTN_DISABLE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_SEL)->EnableWindow(TRUE);
    GetDlgItem(IDOK)->EnableWindow(TRUE);

	gEnableEvent.Enabled = FALSE;
	dwRet = DRV_EnableEvent(m_DriverHandle, &gEnableEvent);
	if (dwRet != 0) 
	{
		uShowDriverErrMsg(GetSafeHwnd(), dwRet);
	}

	gbCountine = FALSE;
	Sleep(500);    // wait for thread exit
	DWORD dwExitcode = 0;
	if(hThread)
	{
		GetExitCodeThread(hThread, &dwExitcode);
		if (dwExitcode == STILL_ACTIVE) 
		{
			TerminateThread(hThread, 0);
		}
		CloseHandle(hThread);
        hThread = (HANDLE)NULL;
	}

}

UINT UserThread(LPVOID ThreadArg)
{
	PT_ThreadArg *ptTArg;
	USHORT usEventType;
	DWORD dwEventCount;
	char szBuf[10];
	
	ptTArg = (PT_ThreadArg*)ThreadArg;
	gCheckEvent.EventType = &usEventType;
	gCheckEvent.Milliseconds = 1000;
	
	dwEventCount = 0;
	while (gbCountine)
	{
		if (DRV_CheckEvent(ptTArg->lDriverHandle, &gCheckEvent) == SUCCESS)
		{
			if (usEventType == ADS_EVT_TC_TIMER)
			{
				dwEventCount++;
				sprintf(szBuf, "%ld", dwEventCount);
				::SetDlgItemText( ptTArg->hWnd, IDC_EVENTCOUNT, szBuf );
			}
		}
	}
	
	return 0;
}

void CTmCntSetDlg::OnOK() 
{
	// TODO: Add extra validation here
	gbCountine = FALSE;
    DWORD dwExitcode = 0;
	if(hThread)
	{
		GetExitCodeThread(hThread, &dwExitcode);
		if (dwExitcode == STILL_ACTIVE) 
		{
			TerminateThread(hThread, 0);
		}
		CloseHandle(hThread);
        hThread = (HANDLE)NULL;
	}
	
	if (m_DriverHandle != NULL)
		DRV_DeviceClose(&m_DriverHandle);

	CDialog::OnOK();
}

⌨️ 快捷键说明

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