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

📄 cntintdlg.cpp

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

#include "stdafx.h"
#include "CntInt.h"
#include "CntIntDlg.h"


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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
TCHAR *szCascadeMode[] = { _T("24 bit"),  _T("48 bit") };
TCHAR *szInputMode[] = { _T("Disable"), _T("X1"), _T("X2"), _T("X4"),
_T("2 pulse"), _T("1 pulse"), _T("N/A"), _T("Cascade") };
TCHAR *szLatchSource[] =
{
	_T("S/W read"),   _T("Index-in"),   _T("DI0"),   _T("DI1"),   _T("Timer") 
};
TCHAR *szSystemClock[] = { _T("8MHz"), _T("4MHz"), _T("2MHz") };
TCHAR *szColumnTitle[] = { _T("Counter"),_T("Value"),_T("Input Mode"),_T("Latch Source"),_T("Overflow"),_T("Index In")};
TCHAR *szContent[COUNT_NUMBER][COL_NUMER] = {
	{_T("CH1"),_T("0"),_T("Disable"),_T("S/W read"),_T("0"),_T("0")},
	{_T("CH2"),_T("0"),_T("Disable"),_T("S/W read"),_T("0"),_T("0")},
	{_T("CH3"),_T("0"),_T("Disable"),_T("S/W read"),_T("0"),_T("0")}
};

static USHORT gwEventTypeAry[]=
{
	    ADS_EVT_OVERFLOW_CNT0,
		ADS_EVT_OVERFLOW_CNT1,
		ADS_EVT_OVERFLOW_CNT2,
		ADS_EVT_INDEX_CNT0,
		ADS_EVT_INDEX_CNT1,
		ADS_EVT_INDEX_CNT2,
		ADS_EVT_INTERRUPT_IDI0,
		ADS_EVT_INTERRUPT_IDI1,
		ADS_EVT_TC_TIMER
};

CString strConfigContent[COUNT_NUMBER][COL_NUMER];

static CONFIG_833  gConfig833;
static THREAD_PAR  pThread_Par;  // passed to Userthread
BOOL  gbThreadRun = FALSE, gbRunning = FALSE;
HANDLE   hThread = NULL;
DWORD UserThread(LPVOID lpPara);
DWORD dwThreadID = 0;


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

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

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

	for(int i = 0; i < COUNT_NUMBER ; i++)
	{
        m_baResetCounterAfterLatch[i] = FALSE;
		m_baResetValueTo0x8[i] = FALSE;
		m_baOverflowLock[i] = FALSE;
	}
}

void CCntIntDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCntIntDlg)
	DDX_Control(pDX, IDC_STATIC_DI1, m_staticDI1);
	DDX_Control(pDX, IDC_STATIC_DI0, m_staticDI0);
	DDX_Control(pDX, IDC_LIST_CNT, m_listCnt);
	DDX_Control(pDX, IDC_DEVICENAME, m_DeviceName);
	DDX_Control(pDX, IDC_CMBSYMCLOCK, m_comboSystemLock);
	DDX_Control(pDX, IDC_CMBLATCHSOURCE, m_comboLatchSource);
	DDX_Control(pDX, IDC_CMBINPUTMODE, m_comboInputMode);
	DDX_Control(pDX, IDC_CMBCNT, m_comboCnt);
	DDX_Control(pDX, IDC_CMBCASCADE, m_comboCascade);
	//}}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_BTN_SEL, OnSelectDevice)//
	ON_BN_CLICKED(IDOK, OnClose)//
	ON_BN_CLICKED(IDC_BTN_CONFIG, OnConfigure833Parameter)
	ON_BN_CLICKED(IDC_CHKDITIMER, OnChkditimer)//
	ON_BN_CLICKED(IDC_CHKINTINDEX, OnChkintindex)
	ON_BN_CLICKED(IDC_CHKINTOVERFLOW, OnChkintoverflow)
	ON_BN_CLICKED(IDC_CHKLOCK, OnChklock)
	ON_BN_CLICKED(IDC_CHKRESETLATCH, OnChkresetlatch)
	ON_BN_CLICKED(IDC_CHKRESETVALUE, OnChkresetvalue)
	ON_CBN_SELCHANGE(IDC_CMBCNT, OnSelchangeCounter)
	ON_CBN_SELCHANGE(IDC_CMBINPUTMODE, OnSelchangeInputMode)
	ON_CBN_SELCHANGE(IDC_CMBLATCHSOURCE, OnSelchangeLatchSource)
	ON_BN_CLICKED(IDC_RDDI1, OnRddi1)
	ON_BN_CLICKED(IDC_RDTIMER, OnRdtimer)
	ON_CBN_SELCHANGE(IDC_CMBSYMCLOCK, OnSelchangeCmbsymclock)
	ON_CBN_SELCHANGE(IDC_CMBCASCADE, OnSelchangeCmbcascade)
	ON_BN_CLICKED(IDC_CHKDI0, OnChkdi0)
	ON_BN_CLICKED(IDC_BTN_ENABLE, OnBtnEnable)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_DISABLE, OnBtnDisable)
	ON_BN_CLICKED(IDC_BTNRESETCOUNTER, OnBtnresetcounter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

BOOL CCntIntDlg::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;
	}

    InitControl();
	InitConfigParameter();
   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::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 CCntIntDlg::OnClose() 
{
	// TODO: Add your control notification handler code here
	if (m_DriverHandle != NULL)
		DRV_DeviceClose(&m_DriverHandle);
	CDialog::OnOK();
}

void CCntIntDlg::InitControl()
{
	int i,j;
	CString str;
	m_comboCascade.AddString(szCascadeMode[NOCASCADE]);
	m_comboCascade.AddString(szCascadeMode[CASCADE]);
	m_comboCascade.SetCurSel(0);
	for (i = 0; i < 8; i++)
	{
		m_comboInputMode.AddString(szInputMode[i]);
	}
	m_comboInputMode.SetCurSel(0);
	for (i = 0; i < 5; i++)
	{
		m_comboLatchSource.AddString(szLatchSource[i]);
	}
	m_comboLatchSource.SetCurSel(0);
	for (i = 0; i < 3; i++)
	{
		m_comboSystemLock.AddString(szSystemClock[i]);
	}
	m_comboSystemLock.SetCurSel(0);
	for(i = 0; i < COUNT_NUMBER ; i++)
	{
		str.Format("CH%d",i+1);
		m_comboCnt.AddString(str);
	}
	m_comboCnt.SetCurSel(0);
	
	m_listCnt.SetExtendedStyle( m_listCnt.GetExtendedStyle()
		|LVS_EX_GRIDLINES );
	for(i = 0; i< COL_NUMER ;i++)
	{
		m_listCnt.InsertColumn(i,szColumnTitle[i],LVCFMT_CENTER,80);
	}  
	str = "";
	for(i = 0; i< COUNT_NUMBER ; i++)
	{
		m_listCnt.InsertItem(i,str);
	}
	for(i = 0; i< COUNT_NUMBER ; i++)
	{
		for(j = 0; j< COL_NUMER ;j++)
		{
			strConfigContent[i][j].Format("%s",szContent[i][j]);
			m_listCnt.SetItemText(i,j,szContent[i][j]);
		}  
	}
	((CButton*)GetDlgItem(IDC_BTNRESETCOUNTER))->SetWindowText("Reset Counter 1");
	((CButton*)GetDlgItem(IDC_RDDI1))->SetCheck(1);
	((CButton*)GetDlgItem(IDC_RDDI1))->EnableWindow(FALSE);
	((CButton*)GetDlgItem(IDC_RDTIMER))->EnableWindow(FALSE);
	m_staticDI0.SetWindowText("0");
	m_staticDI1.SetWindowText("0");

    ((CButton*)GetDlgItem(IDC_BTN_ENABLE))->EnableWindow(FALSE);
    ((CButton*)GetDlgItem(IDC_BTN_DISABLE))->EnableWindow(FALSE);
	((CButton*)GetDlgItem(IDOK))->EnableWindow(TRUE);
	
}

void CCntIntDlg::InitConfigParameter()
{
	int i;
	//Configure sys
    gConfig833.CounterConfigSys.CascadeMode = 0;
	gConfig833.CounterConfigSys.SysClock = 0;
	gConfig833.CounterConfigSys.TimeBase = 0;
	gConfig833.CounterConfigSys.TimeDivider = 1;

	//Configure counter
	for(i = 0; i < COUNT_NUMBER ;i++)
	{
	   gConfig833.CounterConfig[i].counter = i;
	   gConfig833.CounterConfig[i].LatchOverflow = 0;
	   gConfig833.CounterConfig[i].LatchSrc = 0;
	   gConfig833.CounterConfig[i].ResetOnLatch = 0;
	   gConfig833.CounterConfig[i].ResetValue = 0;

	   gConfig833.CountStart[i].counter = i;
	   gConfig833.CountStart[i].InputMode = 0;
	}

	//Configure interrupt
	gConfig833.usInterrupt = 0;
}

void CCntIntDlg::OnConfigure833Parameter() 
{
	DWORD dwRet;
	// Configure sys
	dwRet = DRV_QCounterConfigSys(m_DriverHandle, &gConfig833.CounterConfigSys);
	if (dwRet != 0) 
	{
		uShowDriverErrMsg(GetSafeHwnd(), dwRet);
		DRV_DeviceClose(&m_DriverHandle);
		return;
	}

	//Configure counter
	// setup counter
	for (int i = 0; i < COUNT_NUMBER; i++)
	{
		dwRet = DRV_QCounterConfig(m_DriverHandle, &gConfig833.CounterConfig[i]);
		if (dwRet != 0) 
		{
			uShowDriverErrMsg(GetSafeHwnd(), dwRet);
			DRV_DeviceClose(&m_DriverHandle);
			return;
		}
		// setup channel
		dwRet = DRV_QCounterStart(m_DriverHandle, &gConfig833.CountStart[i]);
		if (dwRet != 0) 
		{
			uShowDriverErrMsg(GetSafeHwnd(), dwRet);
			DRV_DeviceClose(&m_DriverHandle);
			return;
		}
	}
    gConfig833.usInterrupt = m_usInterrupt;
	//Update List control content
    UpdateContentConfig();
	//((CButton*)GetDlgItem(IDC_BTN_CONFIG))->EnableWindow(FALSE);
	((CButton*)GetDlgItem(IDC_BTN_ENABLE))->EnableWindow(TRUE);
	((CButton*)GetDlgItem(IDC_BTN_DISABLE))->EnableWindow(FALSE);
}

void CCntIntDlg::OnChkditimer() 
{
	// TODO: Add your control notification handler code here
    int bCheck,bSubCheck;
	bCheck = ((CButton*)GetDlgItem(IDC_CHKDITIMER))->GetCheck();

	m_usInterrupt &= 0x03ff;
	if (bCheck == BST_CHECKED) 
	{	
		((CButton*)GetDlgItem(IDC_RDDI1))->EnableWindow(TRUE);
		((CButton*)GetDlgItem(IDC_RDTIMER))->EnableWindow(TRUE);
		m_usInterrupt |= 0x0004;
		bSubCheck = ((CButton*)GetDlgItem(IDC_RDDI1))->GetCheck();
		if (bSubCheck == BST_CHECKED) 
		{
            m_usInterrupt |= 0x0002;
		}
		if (bSubCheck == BST_UNCHECKED) 
		{
            m_usInterrupt |= 0x0001;
		}
	}
    if (bCheck == BST_UNCHECKED) 
	{
		((CButton*)GetDlgItem(IDC_RDDI1))->EnableWindow(FALSE);
		((CButton*)GetDlgItem(IDC_RDTIMER))->EnableWindow(FALSE);
        m_usInterrupt &= 0x03f8;
    }
	gConfig833.usInterrupt = m_usInterrupt;

}

void CCntIntDlg::OnChkintindex() 
{
	// TODO: Add your control notification handler code here
	int offset;
	offset = 4 + m_nCounter;
	
	SetInterruptValue(IDC_CHKINTINDEX,offset);
	

⌨️ 快捷键说明

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