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

📄 diintdlg.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DiIntDlg.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;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDiIntDlg dialog

CDiIntDlg::CDiIntDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDiIntDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDiIntDlg)
	m_dwScanTime = 1000;
	m_uChannel = 0;
	m_uCount_IDI1 = m_uCount_IDI0 = m_uCount_IDI16 = m_uCount_IDI17 = 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_bDI0Flag  = FALSE;
	m_bDI1Flag  = FALSE;
   m_bDI16Flag = FALSE;
	m_bDI17Flag = FALSE;
   m_StartFlag = 0;
} 

void CDiIntDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDiIntDlg)
	DDX_Control(pDX, IDC_ENABLEBTN1,      m_EnableBtn1);
	DDX_Control(pDX, IDC_ENABLEBTN2,      m_EnableBtn2);
	DDX_Control(pDX, IDC_ENABLEBTN3,      m_EnableBtn3);
	DDX_Control(pDX, IDC_ENABLEBTN4,      m_EnableBtn4);
	DDX_Control(pDX, IDC_DISABLEBTN1,     m_DisableBtn1);
	DDX_Control(pDX, IDC_DISABLEBTN2,     m_DisableBtn2);
	DDX_Control(pDX, IDC_DISABLEBTN3,     m_DisableBtn3);
	DDX_Control(pDX, IDC_DISABLEBTN4,     m_DisableBtn4);
   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_DI0, 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_IDI0,  m_uCount_IDI0);
   DDX_Text(pDX, IDC_COUNT_IDI1,  m_uCount_IDI1);
   DDX_Text(pDX, IDC_COUNT_IDI16, m_uCount_IDI16);
   DDX_Text(pDX, IDC_COUNT_IDI17, m_uCount_IDI17);
   DDV_MinMaxUInt(pDX, m_uCount_IDI0, 1, 10000);
   DDV_MinMaxUInt(pDX, m_uCount_IDI1, 1, 10000);
   DDV_MinMaxUInt(pDX, m_uCount_IDI16, 1, 10000);
   DDV_MinMaxUInt(pDX, m_uCount_IDI17, 1, 10000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDiIntDlg, CDialog)
	//{{AFX_MSG_MAP(CDiIntDlg)
	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_ENABLEBTN1,   OnEnableBtn1)	
	ON_BN_CLICKED(IDC_ENABLEBTN2,   OnEnableBtn2)
	ON_BN_CLICKED(IDC_ENABLEBTN3,   OnEnableBtn3)
	ON_BN_CLICKED(IDC_ENABLEBTN4,   OnEnableBtn4)
	ON_BN_CLICKED(IDC_DISABLEBTN1,  OnDisableBtn1)
	ON_BN_CLICKED(IDC_DISABLEBTN2,  OnDisableBtn2)
	ON_BN_CLICKED(IDC_DISABLEBTN3,  OnDisableBtn3)
	ON_BN_CLICKED(IDC_DISABLEBTN4,  OnDisableBtn4)
  	ON_EN_CHANGE(IDC_COUNT_IDI0,    OnChangeCountIDI0)
   ON_EN_CHANGE(IDC_COUNT_IDI1,    OnChangeCountIDI1)
   ON_EN_CHANGE(IDC_COUNT_IDI16,   OnChangeCountIDI16)
   ON_EN_CHANGE(IDC_COUNT_IDI17,   OnChangeCountIDI17)
	ON_WM_CLOSE()
	ON_EN_CHANGE (IDC_SCANTIME,     OnChangeScanTime)
	ON_BN_CLICKED(IDS_EVENTPRIORITY1, OnEventPriority1)
	ON_BN_CLICKED(IDS_EVENTPRIORITY2, OnEventPriority2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiIntDlg message handlers

BOOL CDiIntDlg::OnInitDialog()
{
   int   i;
   FLOAT ratio;
   char  szBuf_IDI[ 4 ][20];
	UCHAR	DeviceName[ 50 ];

	CDialog::OnInitDialog();

   for ( i = 0; i < 4; i++ )
   {
      ratio = 0.0;
      sprintf( szBuf_IDI[i], "%.3f", ratio );
   }
   ::SetDlgItemText( m_hWnd, IDC_INTERRUPTCOUNT_IDI0,  szBuf_IDI[0] );
   ::SetDlgItemText( m_hWnd, IDC_INTERRUPTCOUNT_IDI1,  szBuf_IDI[1] );
   ::SetDlgItemText( m_hWnd, IDC_INTERRUPTCOUNT_IDI16, szBuf_IDI[2] );
   ::SetDlgItemText( m_hWnd, IDC_INTERRUPTCOUNT_IDI17, szBuf_IDI[3] );

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

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

	aboutDlg.DoModal();
}

void CDiIntDlg::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 CDiIntDlg::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 CDiIntDlg::OnStartBtn()
{
	// TODO: Add your control notification handler code here
   m_StartFlag = 1;
	SetTimer( 1, m_dwScanTime, NULL );
	m_StartBtn.EnableWindow( FALSE );
	m_StopBtn.EnableWindow( TRUE );
   m_DeviceSelect.EnableWindow( FALSE );
}

void CDiIntDlg::OnStopBtn()
{
	// TODO: Add your control notification handler code here
	KillTimer( 1 );
   m_StartFlag = 0;
	m_StartBtn.EnableWindow( TRUE );
	m_StopBtn.EnableWindow( FALSE );
   if ( !(m_bDI0Flag || m_bDI1Flag || m_bDI16Flag || m_bDI17Flag || m_StartFlag ))
   {
      m_DeviceSelect.EnableWindow( TRUE );
   }
}

void CDiIntDlg::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 CDiIntDlg::OnEnableBtn1() 
{
	PT_EnableEvent	EventSetting;
	CWinThread*     pThreadObject;
   
   m_bDI0Flag   = TRUE;
   usStartFlag  = 1;    
   EventSetting.Enabled   = TRUE;
   EventSetting.Count     = m_uCount_IDI0;

   EventSetting.EventType = ADS_EVT_INTERRUPT_IDI0;      
   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_DisableBtn1.EnableWindow( TRUE );
   m_EnableBtn1.EnableWindow( FALSE );
   m_DeviceSelect.EnableWindow(FALSE);
 
   if ( m_bFlag == FALSE )
   {
	  m_bContinue = TRUE;
	  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( CDiIntDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_TIME_CRITICAL, 0, 0, NULL );
      m_pThreadHandle = pThreadObject->m_hThread;
   }
}

void CDiIntDlg::OnEnableBtn2()
{
   PT_EnableEvent  EventSetting;
   CWinThread*     pThreadObject;
   
   m_bDI1Flag   = TRUE;
   usStartFlag  = 1;    
   EventSetting.Enabled   = TRUE;
   EventSetting.Count     = m_uCount_IDI1;
   EventSetting.EventType = ADS_EVT_INTERRUPT_IDI1;   
   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_DisableBtn2.EnableWindow( TRUE );
   m_EnableBtn2.EnableWindow( FALSE );
   m_DeviceSelect.EnableWindow(FALSE);


   if ( m_bFlag == FALSE )
   {
	  m_bContinue = TRUE;
	  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( CDiIntDlg::EventThread, &ptThreadArg, THREAD_PRIORITY_TIME_CRITICAL, 0, 0, NULL );

⌨️ 快捷键说明

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