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

📄 diintdlg.cpp

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

#include "stdafx.h"
#include <process.h>
#include "..\..\..\..\include\Driver.h"
#include "Diint.h"
#include "DiintDlg.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;
static USHORT           usStartFlag;
static bool  m_StartFlag, m_DI0Flag;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CFDiginDlg dialog

CFDiginDlg::CFDiginDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFDiginDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFDiginDlg)
	m_dwScanTime = 1000;
	m_uChannel = 0;
	m_uCount = 1;
	//}}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;
	m_bFlag        = FALSE;
   m_DI0Flag      = FALSE;
   m_StartFlag    = FALSE;
}

void CFDiginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFDiginDlg)
	DDX_Control(pDX, IDC_ENABLEBTN,      m_EnableBtn);
	DDX_Control(pDX, IDC_DISABLEBTN,     m_DisableBtn);
   DDX_Control(pDX, IDS_SELECTDEVICE,   m_DeviceSelect);
	DDX_Control(pDX, IDC_STOPBTN,        m_StopBtn);
	DDX_Control(pDX, IDC_STARTBTN,       m_StartBtn);
	DDX_Control(pDX, IDC_CHANNELSPIN,    m_ChannelSpin);
	DDX_Control(pDX, IDC_INTERRUPTCOUNT, m_IntEventCount);
	DDX_Control(pDX, IDC_SCANDATA,       m_ScanData);
	DDX_Control(pDX, IDC_DEVICENAME,     m_DeviceName);
	DDX_Text(   pDX, IDC_SCANTIME,       m_dwScanTime);
	DDV_MinMaxDWord(pDX, m_dwScanTime, 1, 100000);
	DDX_Text(   pDX, IDC_CHANNEL,        m_uChannel);
	DDV_MinMaxUInt(pDX, m_uChannel, 0, 10000);
	DDX_Text(pDX, IDC_COUNT, m_uCount);
	DDV_MinMaxUInt(pDX, m_uCount, 1, 10000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFDiginDlg, CDialog)
	//{{AFX_MSG_MAP(CFDiginDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDS_ABOUTBOX,     OnAboutbox)
	ON_BN_CLICKED(IDS_SELECTDEVICE, OnSelectDevice)
	ON_NOTIFY(UDN_DELTAPOS, IDC_CHANNELSPIN, OnDeltaposChannelSpin)
	ON_BN_CLICKED(IDC_STARTBTN,     OnStartBtn)
	ON_BN_CLICKED(IDC_STOPBTN,      OnStopBtn)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_ENABLEBTN,    OnEnableBtn)
	ON_BN_CLICKED(IDC_DISABLEBTN,   OnDisableBtn)
	ON_WM_CLOSE()
	ON_EN_CHANGE (IDC_SCANTIME,     OnChangeScanTime)
	ON_EN_CHANGE(IDC_COUNT, OnChangeCount)
	ON_BN_CLICKED(IDS_EVENTPRIORITY1, OnEventPriority1)
	ON_BN_CLICKED(IDS_EVENTPRIORITY2, OnEventPriority2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFDiginDlg message handlers

BOOL CFDiginDlg::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 );
	m_uChannel = 0;
	m_ChannelSpin.SetPos( 0 );
	m_ChannelSpin.SetRange( 0, 17 );
	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 CFDiginDlg::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 CFDiginDlg::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 CFDiginDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

	aboutDlg.DoModal();
}

void CFDiginDlg::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 CFDiginDlg::OnDeltaposChannelSpin(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	m_uChannel = pNMUpDown->iPos;
	UpdateData(FALSE);
	*pResult = 0;
}

void CFDiginDlg::OnStartBtn()
{
	// TODO: Add your control notification handler code here
	SetTimer( 1, m_dwScanTime, NULL );
	m_StartBtn.EnableWindow( FALSE );
	m_StopBtn.EnableWindow( TRUE );
   m_DeviceSelect.EnableWindow( FALSE );
   m_StartFlag = TRUE;
}

void CFDiginDlg::OnStopBtn()
{
	// TODO: Add your control notification handler code here
   m_StartFlag = FALSE;
	KillTimer( 1 );
	m_StartBtn.EnableWindow( TRUE );
	m_StopBtn.EnableWindow( FALSE );
   if ( ( m_StartFlag == FALSE)&&(m_DI0Flag == FALSE ))
   {
      m_DeviceSelect.EnableWindow( TRUE );
   }
}

void CFDiginDlg::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	USHORT	gwValue;		// input value
	char	szBuf[10];

	m_ptDioReadPortByte.port = m_uChannel;
	m_ptDioReadPortByte.value = (USHORT far *)&gwValue;

	if ((m_ErrCde = DRV_DioReadPortByte(m_DriverHandle,
                (LPT_DioReadPortByte)&m_ptDioReadPortByte)) != 0)
	{
		DRV_GetErrorMessage(m_ErrCde,(LPSTR)m_szErrMsg);
		KillTimer( 1 );
		MessageBox((LPCSTR)m_szErrMsg, "Driver Message", MB_OK);
	} else {
		sprintf( szBuf, "%02X", gwValue );
		m_ScanData.SetWindowText( (LPCTSTR)szBuf );
	}
	
	CDialog::OnTimer(nIDEvent);
}

void CFDiginDlg::OnEnableBtn() 
{
	// TODO: Add your control notification handler code here
	PT_EnableEvent	 EventSetting;
	CWinThread*     pThreadObject;
   usStartFlag = 1;
   m_DI0Flag = TRUE;

 	EventSetting.EventType = ADS_EVT_INTERRUPT_DI0;
	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_bContinue = TRUE;
	m_DisableBtn.EnableWindow( TRUE );
	m_EnableBtn.EnableWindow( FALSE );
   m_DeviceSelect.EnableWindow( FALSE );
	ptThreadArg.m_lDriverHandle = m_DriverHandle;
	ptThreadArg.m_hWnd = GetSafeHwnd();

   // Using THREAD_PRIORITY_TIME_CRITICAL for interrupt event handling routine,
	// you can get about 20 KHz response ratio by using PENTIUM 200 MMX PC.
	pThreadObject = AfxBeginThread( CFDiginDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_TIME_CRITICAL, 0, 0, NULL );
	m_pThreadHandle = pThreadObject->m_hThread;		
}

void CFDiginDlg::OnDisableBtn() 
{
	// TODO: Add your control notification handler code here
	PT_EnableEvent	EventSetting;
//	DWORD ExitCode ;
	BOOL flag = FALSE;
   m_DI0Flag = FALSE;
	m_bContinue = FALSE;
    usStartFlag = 0;
	while(!flag)
	{
	  if(m_bFlag == FALSE)
	  {
	   m_DisableBtn.EnableWindow( FALSE );
	   m_EnableBtn.EnableWindow( TRUE );      
	   EventSetting.EventType = ADS_EVT_INTERRUPT_DI0;
	   EventSetting.Enabled = FALSE;
	   EventSetting.Count = 1;
	   m_ErrCde = DRV_EnableEvent( m_DriverHandle, &EventSetting );
	   Sleep(50L);
  	  
	   if ( m_ErrCde != 0) 
      {
	      DRV_GetErrorMessage( m_ErrCde, (LPSTR)m_szErrMsg );
		   MessageBox( (LPCSTR)m_szErrMsg, "Driver Message", MB_OK );
		   return;   }
	   
       m_bContinue = FALSE;
	   flag = TRUE;
	  }
    
     if ( ( m_StartFlag == FALSE )&&( m_DI0Flag == FALSE ))
     {
        m_DeviceSelect.EnableWindow( TRUE );
     }
	}
	

}

void CFDiginDlg::OnClose() 
{
	PT_EnableEvent	EventSetting;
	// TODO: Add your message handler code here and/or call default
	if( usStartFlag == 1 )
	{
	   usStartFlag = 0;
	   EventSetting.EventType = ADS_EVT_INTERRUPT_DI0;
	   EventSetting.Enabled = FALSE;
	   EventSetting.Count = 1;
	   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;   }	    

	}
	Sleep(50L);
	if ( m_DriverHandle	!= NULL )
		DRV_DeviceClose( &m_DriverHandle );

	CDialog::OnClose();
}

void CFDiginDlg::OnOK() 
{
	// TODO: Add extra validation here
	PT_EnableEvent	EventSetting;	
	if( usStartFlag == 1 )
	{
	   usStartFlag = 0;
	   EventSetting.EventType = ADS_EVT_INTERRUPT_DI0;
	   EventSetting.Enabled = FALSE;
	   EventSetting.Count = 1;
	   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;   }	    

	}
	if ( m_DriverHandle	!= NULL )
		DRV_DeviceClose( &m_DriverHandle );
	
	CDialog::OnOK();
}

void CFDiginDlg::OnChangeScanTime() 
{
	// 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);
}

UINT CFDiginDlg::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 ) {
		m_bFlag = TRUE;
		if (DRV_CheckEvent( ptTArg->m_lDriverHandle, &ptCheckEvent ) == SUCCESS) {
			dwEventCount++;
		}
		m_bFlag = FALSE;
		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 CFDiginDlg::OnChangeCount() 
{
	// 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 CFDiginDlg::OnEventPriority1() 
{
	// TODO: Add your control notification handler code here
	m_bHiPriority = FALSE;
}

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

⌨️ 快捷键说明

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