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

📄 cntintdlg.cpp

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

#include "stdafx.h"
#include "CntInt.h"
#include "CntIntDlg.h"
#include "..\..\..\..\..\Include\Driver.h"

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


static PT_CheckEvent	ptCheckEvent;
static BOOL m_bCountine;
static char *szInterrupt[] = {
		"Overflow",
		"Underflow",
		"Index",
		"OverCompare",
		"UnderCompare",
		"DI0",
		"DI1",
		"DI2",
		"DI3",
		"Timer"
};
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCntIntDlg dialog

CCntIntDlg::CCntIntDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCntIntDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCntIntDlg)
	m_StartPort = 0;
	m_StartCounter = 0;
	m_PortCount = 1;
	m_CntCount = 1;
	m_CurCounter = 0;
	m_InterruptSrc = 0;
	m_bCountine = FALSE;
	pThread = NULL;
	m_CompData = 500;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCntIntDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCntIntDlg)
	DDX_Control(pDX, IDC_SELDEV, m_btnSelectDev);
	DDX_Control(pDX, IDC_LSTDI, m_lstDI);
	DDX_Control(pDX, IDC_LSTCOUNTER, m_lstCounter);
	DDX_Control(pDX, IDOK, m_btnOk);
	DDX_Control(pDX, IDC_BTNSTOP, m_btnStop);
	DDX_Control(pDX, IDC_BTNSTART, m_btnStart);
	DDX_Control(pDX, IDC_COMCOUNTER, m_Counter);
	DDX_Control(pDX, IDC_CMBINT, m_CmbInt);
	DDX_Control(pDX, IDC_DEVICE, m_DeviceName);
	DDX_Text(pDX, IDC_STARTPORT, m_StartPort);
	DDX_Text(pDX, IDC_STARTCOUNTER, m_StartCounter);
	DDX_Text(pDX, IDC_PORTCOUNT, m_PortCount);
	DDX_Text(pDX, IDC_CNTCOUNT, m_CntCount);
	DDX_Text(pDX, IDC_COMPDATA, m_CompData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCntIntDlg, CDialog)
	//{{AFX_MSG_MAP(CCntIntDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELDEV, OnSeldev)
	ON_BN_CLICKED(IDC_BTNSTART, OnBtnstart)
	ON_EN_CHANGE(IDC_STARTPORT, OnChangeStartport)
	ON_EN_CHANGE(IDC_PORTCOUNT, OnChangePortcount)
	ON_EN_CHANGE(IDC_STARTCOUNTER, OnChangeStartcounter)
	ON_EN_CHANGE(IDC_CNTCOUNT, OnChangeCntcount)
	ON_BN_CLICKED(IDC_BTNSTOP, OnBtnstop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCntIntDlg message handlers

BOOL CCntIntDlg::OnInitDialog()
{
	UCHAR DeviceName[50];
	char szBuf[10];
	int i;

	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);

	// Add counter
	for (i = 0; i < 4; i++)
	{
		sprintf(szBuf, "Counter%d", i);
		m_Counter.AddString(szBuf);
	}
	m_Counter.SetCurSel(0);
	for (i = 0; i < 10; i++)
	{
		m_CmbInt.AddString (szInterrupt[i]);
	}
	m_CmbInt.SetCurSel(0);

	// Initialize list
	m_lstCounter.InsertColumn(0, "Counter", LVCFMT_CENTER, 60);
	m_lstCounter.InsertColumn(1, "Value", LVCFMT_CENTER, 120);

	m_lstDI.InsertColumn(0, "Port", LVCFMT_CENTER, 60);
	m_lstDI.InsertColumn(1, "Value", LVCFMT_CENTER, 120);

	m_ErrCode = DRV_DeviceOpen(m_ulDevNum, &m_DriverHandle);
	if (m_ErrCode != SUCCESS)
	{
		m_DriverHandle = 0;
		return FALSE;
	}
	m_btnStop.EnableWindow(FALSE);

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

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

void CCntIntDlg::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();
}

void CCntIntDlg::OnSeldev() 
{
	// 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 CCntIntDlg::OnBtnstart() 
{
	ULONG dwEventID;
	UINT i;
	ULONG* pCompData;
	ULONG ulSize;

	// TODO: Add your control notification handler code here
	m_CurCounter = m_Counter.GetCurSel();
	switch(m_CmbInt.GetCurSel()) 
	{
	case 0:
		dwEventID = ADS_EVT_OVERFLOW_CNT0 + m_CurCounter;
		break;
	case 1:
		dwEventID = ADS_EVT_UNDERFLOW_CNT0 + m_CurCounter;
		break;
	case 2:
		dwEventID = ADS_EVT_INDEX_CNT0 + m_CurCounter;
		break;
	case 3:
		dwEventID = ADS_EVT_OVERCOMPARE_CNT0 + m_CurCounter;
		break;
	case 4:
		dwEventID = ADS_EVT_UNDERCOMPARE_CNT0 + m_CurCounter;
		break;
	case 5:
		dwEventID = ADS_EVT_DI_INTERRUPT0;
		break;
	case 6:
		dwEventID = ADS_EVT_DI_INTERRUPT1;
		break;
	case 7:
		dwEventID = ADS_EVT_DI_INTERRUPT2;
		break;
	case 8:
		dwEventID = ADS_EVT_DI_INTERRUPT3;
		break;
	case 9:
		dwEventID = ADS_EVT_INTERRUPT_TIMER4;
		break;
	}
	m_InterruptSrc = dwEventID;

	if ((dwEventID >= ADS_EVT_DI_INTERRUPT0) && (dwEventID <= ADS_EVT_DI_INTERRUPT3))
	{
		m_ErrCode = AdxDioEnableEventAndSpecifyDiPorts(m_DriverHandle, dwEventID, m_StartPort, m_PortCount);
		m_ErrCode = AdxDioEnableEventAndSpecifyEventCounter(m_DriverHandle, dwEventID, m_StartCounter, m_CntCount);
	}
	else
		m_ErrCode = AdxCntrEnableEventAndSpecifyEventCounter(m_DriverHandle, dwEventID, m_StartCounter, m_CntCount);

	if (m_ErrCode != SUCCESS)
	{
		DRV_GetErrorMessage( m_ErrCode, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}

	if ((dwEventID == ADS_EVT_OVERCOMPARE_CNT0 + m_CurCounter) || (dwEventID == ADS_EVT_UNDERCOMPARE_CNT0 + m_CurCounter))
	{
		// set compare data
		m_ErrCode = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrCompareData, NULL, &ulSize);
		if (m_ErrCode == SUCCESS)
		{
			pCompData = new ULONG[ulSize / sizeof(ULONG)];
			m_ErrCode = DRV_DeviceGetProperty(m_DriverHandle, CFG_CntrCompareData, pCompData, &ulSize);
			pCompData[m_CurCounter] = m_CompData;
			m_ErrCode = DRV_DeviceSetProperty(m_DriverHandle, CFG_CntrCompareData, pCompData, ulSize);
		}
		delete[] pCompData;
	}

	// reset list
	m_lstCounter.DeleteAllItems();
	m_lstDI.DeleteAllItems();
	for (i = 0; i < m_CntCount; i++)
		m_lstCounter.InsertItem(i, "");
	for (i = 0; i < m_PortCount; i++)
		m_lstDI.InsertItem(i, "");

	m_btnStart.EnableWindow(FALSE);
	m_btnOk.EnableWindow(FALSE);
	m_btnStop.EnableWindow(TRUE);
	m_btnSelectDev.EnableWindow(FALSE);

	m_bCountine = TRUE;
	pThread = AfxBeginThread( CCntIntDlg::EventThread, this, THREAD_PRIORITY_ABOVE_NORMAL, 0, 0, NULL );
	
}

void CCntIntDlg::OnChangeStartport() 
{
	// 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 CCntIntDlg::OnChangePortcount() 
{
	// 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 CCntIntDlg::OnChangeStartcounter() 
{
	// 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 CCntIntDlg::OnChangeCntcount() 
{
	// 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 CCntIntDlg::OnBtnstop() 
{
	// TODO: Add your control notification handler code here
	ULONG dwEventID;
	ULONG dwExitCode;
	
	// TODO: Add your control notification handler code here
	m_btnStart.EnableWindow(TRUE);
	m_btnOk.EnableWindow(TRUE);
	m_btnStop.EnableWindow(FALSE);
	m_btnSelectDev.EnableWindow(TRUE);
	
	m_bCountine = FALSE;
	dwEventID = m_InterruptSrc;
	if ((m_InterruptSrc >= ADS_EVT_DI_INTERRUPT0) && (m_InterruptSrc <= ADS_EVT_DI_INTERRUPT3))
		m_ErrCode = AdxDioDisableEvent(m_DriverHandle, dwEventID);
	else
		m_ErrCode = AdxCntrDisableEvent(m_DriverHandle, dwEventID);
	if (m_ErrCode != SUCCESS)
	{
		DRV_GetErrorMessage( m_ErrCode, (LPSTR)m_szErrMsg );
		MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		return;
	}
	
	if (pThread)
	{
		GetExitCodeThread(pThread->m_hThread, &dwExitCode);
		if (dwExitCode == STILL_ACTIVE)
			TerminateThread(pThread->m_hThread, dwExitCode);
	}
}

UINT CCntIntDlg::EventThread(LPVOID ThreadArg)
{
	USHORT usEventType;
	char szBuf[10];
	CCntIntDlg* ptArg;
	UINT i;
	USHORT usTmp;
	BYTE pData[1];
	ULONG pCntrData[4];
	
	ptArg = (CCntIntDlg*)ThreadArg;
	ptCheckEvent.EventType = &usEventType;
	ptCheckEvent.Milliseconds = 1000;
	
	while (m_bCountine)
	{
		if (DRV_CheckEvent(ptArg->m_DriverHandle, &ptCheckEvent) == SUCCESS)
		{
			if ((usEventType >= ADS_EVT_DI_INTERRUPT0) && (usEventType <= ADS_EVT_DI_INTERRUPT3))
			{
				AdxDioGetLatestEventDiPortsState(ptArg->m_DriverHandle, usEventType, pData, ptArg->m_PortCount);
				for (i = 0; i < ptArg->m_PortCount; i++)
				{
					usTmp = i + ptArg->m_StartPort;
					sprintf(szBuf, "Port%d", usTmp);
					ptArg->m_lstDI.SetItemText(i, 0, szBuf);
					sprintf(szBuf, "%d", pData[i]);
					ptArg->m_lstDI.SetItemText(i, 1, szBuf);
				}
			}
		    if ((usEventType != ADS_EVT_NO_EVENT) && (usEventType != ADS_EVT_TIME_OUT))
			{
				AdxCntrGetLatestEventCounterValue(ptArg->m_DriverHandle, usEventType, pCntrData, ptArg->m_CntCount);
				for (i = 0; i < ptArg->m_CntCount; i++)
				{
					usTmp = i + ptArg->m_StartCounter;
					sprintf(szBuf, "Counter%d", usTmp);
					ptArg->m_lstCounter.SetItemText(i, 0, szBuf);
					sprintf(szBuf, "%lu", pCntrData[i]);
					ptArg->m_lstCounter.SetItemText(i, 1, szBuf);
				}
			}
		}
	}

	return 0;
}

⌨️ 快捷键说明

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