// IOView.cpp : implementation file // #include "stdafx.h" #include "Sys.h" #include "DIOView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif int OldIn=-1; HANDLE gl_hDevice; HANDLE hEventInt; ///////////////////////////////////////////////////////////////////////////// // CDIOView IMPLEMENT_DYNCREATE(CDIOView, CFormView) CDIOView::CDIOView() : CFormView(CDIOView::IDD) { //{{AFX_DATA_INIT(CDIOView) m_bDO0 = FALSE; m_bDO1 = FALSE; m_bDO10 = FALSE; m_bDO11 = FALSE; m_bDO12 = FALSE; m_bDO13 = FALSE; m_bDO14 = FALSE; m_bDO15 = FALSE; m_bDO2 = FALSE; m_bDO3 = FALSE; m_bDO4 = FALSE; m_bDO5 = FALSE; m_bDO6 = FALSE; m_bDO7 = FALSE; m_bDO8 = FALSE; m_bDO9 = FALSE; //}}AFX_DATA_INIT bCreateDevice = FALSE; } CDIOView::~CDIOView() { } void CDIOView::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDIOView) DDX_Control(pDX, IDC_CHECK_DI9, m_Button_In9); DDX_Control(pDX, IDC_CHECK_DI8, m_Button_In8); DDX_Control(pDX, IDC_CHECK_DI7, m_Button_In7); DDX_Control(pDX, IDC_CHECK_DI6, m_Button_In6); DDX_Control(pDX, IDC_STATIC_OutIntCount, m_Static_OutIntCount); DDX_Control(pDX, IDC_STATIC_InIntCount, m_Static_InIntCount); DDX_Control(pDX, IDC_BUTTON_ReleaseDeviceInt, m_Button_ReleaseDeviceInt); DDX_Control(pDX, IDC_BUTTON_InitDeviceInt, m_Button_InitDeviceInt); DDX_Control(pDX, IDC_CHECK_DI5, m_Button_In5); DDX_Control(pDX, IDC_CHECK_DI4, m_Button_In4); DDX_Control(pDX, IDC_CHECK_DI3, m_Button_In3); DDX_Control(pDX, IDC_CHECK_DI2, m_Button_In2); DDX_Control(pDX, IDC_CHECK_DI15, m_Button_In15); DDX_Control(pDX, IDC_CHECK_DI14, m_Button_In14); DDX_Control(pDX, IDC_CHECK_DI13, m_Button_In13); DDX_Control(pDX, IDC_CHECK_DI12, m_Button_In12); DDX_Control(pDX, IDC_CHECK_DI11, m_Button_In11); DDX_Control(pDX, IDC_CHECK_DI10, m_Button_In10); DDX_Control(pDX, IDC_CHECK_DI1, m_Button_In1); DDX_Control(pDX, IDC_CHECK_DI0, m_Button_In0); DDX_Control(pDX, IDC_CHECK_DO9, m_Check_DO9); DDX_Control(pDX, IDC_CHECK_DO8, m_Check_DO8); DDX_Control(pDX, IDC_CHECK_DO7, m_Check_DO7); DDX_Control(pDX, IDC_CHECK_DO6, m_Check_DO6); DDX_Control(pDX, IDC_CHECK_DO5, m_Check_DO5); DDX_Control(pDX, IDC_CHECK_DO4, m_Check_DO4); DDX_Control(pDX, IDC_CHECK_DO3, m_Check_DO3); DDX_Control(pDX, IDC_CHECK_DO2, m_Check_DO2); DDX_Control(pDX, IDC_CHECK_DO15, m_Check_DO15); DDX_Control(pDX, IDC_CHECK_DO14, m_Check_DO14); DDX_Control(pDX, IDC_CHECK_DO13, m_Check_DO13); DDX_Control(pDX, IDC_CHECK_DO12, m_Check_DO12); DDX_Control(pDX, IDC_CHECK_DO11, m_Check_DO11); DDX_Control(pDX, IDC_CHECK_DO10, m_Check_DO10); DDX_Control(pDX, IDC_CHECK_DO1, m_Check_DO1); DDX_Control(pDX, IDC_CHECK_DO0, m_Check_DO0); DDX_Check(pDX, IDC_CHECK_DO0, m_bDO0); DDX_Check(pDX, IDC_CHECK_DO1, m_bDO1); DDX_Check(pDX, IDC_CHECK_DO10, m_bDO10); DDX_Check(pDX, IDC_CHECK_DO11, m_bDO11); DDX_Check(pDX, IDC_CHECK_DO12, m_bDO12); DDX_Check(pDX, IDC_CHECK_DO13, m_bDO13); DDX_Check(pDX, IDC_CHECK_DO14, m_bDO14); DDX_Check(pDX, IDC_CHECK_DO15, m_bDO15); DDX_Check(pDX, IDC_CHECK_DO2, m_bDO2); DDX_Check(pDX, IDC_CHECK_DO3, m_bDO3); DDX_Check(pDX, IDC_CHECK_DO4, m_bDO4); DDX_Check(pDX, IDC_CHECK_DO5, m_bDO5); DDX_Check(pDX, IDC_CHECK_DO6, m_bDO6); DDX_Check(pDX, IDC_CHECK_DO7, m_bDO7); DDX_Check(pDX, IDC_CHECK_DO8, m_bDO8); DDX_Check(pDX, IDC_CHECK_DO9, m_bDO9); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDIOView, CFormView) //{{AFX_MSG_MAP(CDIOView) ON_BN_CLICKED(IDC_CHECK_DO0, OnCheckDo0) ON_BN_CLICKED(IDC_CHECK_DO1, OnCheckDo1) ON_BN_CLICKED(IDC_CHECK_DO2, OnCheckDo2) ON_BN_CLICKED(IDC_CHECK_DO3, OnCheckDo3) ON_BN_CLICKED(IDC_CHECK_DO4, OnCheckDo4) ON_BN_CLICKED(IDC_CHECK_DO5, OnCheckDo5) ON_BN_CLICKED(IDC_CHECK_DO6, OnCheckDo6) ON_BN_CLICKED(IDC_CHECK_DO7, OnCheckDo7) ON_BN_CLICKED(IDC_CHECK_DO8, OnCheckDo8) ON_BN_CLICKED(IDC_CHECK_DO9, OnCheckDo9) ON_BN_CLICKED(IDC_CHECK_DO10, OnCheckDo10) ON_BN_CLICKED(IDC_CHECK_DO11, OnCheckDo11) ON_BN_CLICKED(IDC_CHECK_DO12, OnCheckDo12) ON_BN_CLICKED(IDC_CHECK_DO13, OnCheckDo13) ON_BN_CLICKED(IDC_CHECK_DO14, OnCheckDo14) ON_BN_CLICKED(IDC_CHECK_DO15, OnCheckDo15) ON_WM_TIMER() ON_WM_DESTROY() ON_COMMAND(IDM_ListDevice, OnListDevice) ON_BN_CLICKED(IDC_BUTTON_InitDeviceInt, OnBUTTONInitDeviceInt) ON_BN_CLICKED(IDC_BUTTON_ReleaseDeviceInt, OnBUTTONReleaseDeviceInt) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDIOView diagnostics #ifdef _DEBUG void CDIOView::AssertValid() const { CFormView::AssertValid(); } void CDIOView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CDIOView message handlers void CDIOView::SetSwitchStatus(void) { if(!bCreateDevice) return; BYTE bDISts[32]; // 读入各路开关量状态 if(!PCI2312_GetDeviceDI(gl_hDevice,bDISts)) { MessageBox(L"读开关量数据失败..."); return; } if(bDISts[0]) // 开 { m_Button_In0.SetState(TRUE); m_Button_In0.SetWindowText(L"开关0 (开)"); m_Button_In0.SetCheck(1); } else { // 关 m_Button_In0.SetState(FALSE); m_Button_In0.SetWindowText(L"开关0 (关)"); m_Button_In0.SetCheck(0); } if(bDISts[1]) // 开 { m_Button_In1.SetState(TRUE); m_Button_In1.SetWindowText(L"开关1 (开)"); m_Button_In1.SetCheck(1); } else { // 关 m_Button_In1.SetState(FALSE); m_Button_In1.SetWindowText(L"开关1 (关)"); m_Button_In1.SetCheck(0); } if(bDISts[2]) // 开 { m_Button_In2.SetState(TRUE); m_Button_In2.SetWindowText(L"开关2 (开)"); m_Button_In2.SetCheck(1); } else { // 关 m_Button_In2.SetState(FALSE); m_Button_In2.SetWindowText(L"开关2 (关)"); m_Button_In2.SetCheck(0); } if(bDISts[3]) // 开 { m_Button_In3.SetState(TRUE); m_Button_In3.SetWindowText(L"开关3 (开)"); m_Button_In3.SetCheck(1); } else { // 关 m_Button_In3.SetState(FALSE); m_Button_In3.SetWindowText(L"开关3 (关)"); m_Button_In3.SetCheck(0); } if(bDISts[4]) // 开 { m_Button_In4.SetState(TRUE); m_Button_In4.SetWindowText(L"开关4 (开)"); m_Button_In4.SetCheck(1); } else { // 关 m_Button_In4.SetState(FALSE); m_Button_In4.SetWindowText(L"开关4 (关)"); m_Button_In4.SetCheck(0); } if(bDISts[5]) // 开 { m_Button_In5.SetState(TRUE); m_Button_In5.SetWindowText(L"开关5 (开)"); m_Button_In5.SetCheck(1); } else { // 关 m_Button_In5.SetState(FALSE); m_Button_In5.SetWindowText(L"开关5 (关)"); m_Button_In5.SetCheck(0); } if(bDISts[6]) // 开 { m_Button_In6.SetState(TRUE); m_Button_In6.SetWindowText(L"开关6 (开)"); m_Button_In6.SetCheck(1); } else { // 关 m_Button_In6.SetState(FALSE); m_Button_In6.SetWindowText(L"开关6 (关)"); m_Button_In6.SetCheck(0); } if(bDISts[7]) // 开 { m_Button_In7.SetState(TRUE); m_Button_In7.SetWindowText(L"开关7 (开)"); m_Button_In7.SetCheck(1); } else { // 关 m_Button_In7.SetState(FALSE); m_Button_In7.SetWindowText(L"开关7 (关)"); m_Button_In7.SetCheck(0); } if(bDISts[8]) // 开 { m_Button_In8.SetState(TRUE); m_Button_In8.SetWindowText(L"开关8 (开)"); m_Button_In8.SetCheck(1); } else { // 关 m_Button_In8.SetState(FALSE); m_Button_In8.SetWindowText(L"开关8 (关)"); m_Button_In8.SetCheck(0); } if(bDISts[9]) // 开 { m_Button_In9.SetState(TRUE); m_Button_In9.SetWindowText(L"开关9 (开)"); m_Button_In9.SetCheck(1); } else { // 关 m_Button_In9.SetState(FALSE); m_Button_In9.SetWindowText(L"开关9 (关)"); m_Button_In9.SetCheck(0); } if(bDISts[10]) // 开 { m_Button_In10.SetState(TRUE); m_Button_In10.SetWindowText(L"开关10 (开)"); m_Button_In10.SetCheck(1); } else { // 关 m_Button_In10.SetState(FALSE); m_Button_In10.SetWindowText(L"开关10 (关)"); m_Button_In10.SetCheck(0); } if(bDISts[11]) // 开 { m_Button_In11.SetState(TRUE); m_Button_In11.SetWindowText(L"开关11 (开)"); m_Button_In11.SetCheck(1); } else { // 关 m_Button_In11.SetState(FALSE); m_Button_In11.SetWindowText(L"开关11 (关)"); m_Button_In11.SetCheck(0); } if(bDISts[12]) // 开 { m_Button_In12.SetState(TRUE); m_Button_In12.SetWindowText(L"开关12 (开)"); m_Button_In12.SetCheck(1); } else { // 关 m_Button_In12.SetState(FALSE); m_Button_In12.SetWindowText(L"开关12 (关)"); m_Button_In12.SetCheck(0); } if(bDISts[13]) // 开 { m_Button_In13.SetState(TRUE); m_Button_In13.SetWindowText(L"开关13 (开)"); m_Button_In13.SetCheck(1); } else { // 关 m_Button_In13.SetState(FALSE); m_Button_In13.SetWindowText(L"开关13 (关)"); m_Button_In13.SetCheck(0); } if(bDISts[14]) // 开 { m_Button_In14.SetState(TRUE); m_Button_In14.SetWindowText(L"开关14 (开)"); m_Button_In14.SetCheck(1); } else { // 关 m_Button_In14.SetState(FALSE); m_Button_In14.SetWindowText(L"开关14 (关)"); m_Button_In14.SetCheck(0); } if(bDISts[15]) // 开 { m_Button_In15.SetState(TRUE); m_Button_In15.SetWindowText(L"开关15 (开)"); m_Button_In15.SetCheck(1); } else { // 关 m_Button_In15.SetState(FALSE); m_Button_In15.SetWindowText(L"开关15 (关)"); m_Button_In15.SetCheck(0); } } void CDIOView::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default SetSwitchStatus(); CFormView::OnTimer(nIDEvent); } void CDIOView::SetSwitchOut(void) { if(!bCreateDevice) return; BYTE bDOSts[32]; bDOSts[0]=m_bDO0; bDOSts[8]=m_bDO8; bDOSts[1]=m_bDO1; bDOSts[9]=m_bDO9; bDOSts[2]=m_bDO2; bDOSts[10]=m_bDO10; bDOSts[3]=m_bDO3; bDOSts[11]=m_bDO11; bDOSts[4]=m_bDO4; bDOSts[12]=m_bDO12; bDOSts[5]=m_bDO5; bDOSts[13]=m_bDO13; bDOSts[6]=m_bDO6; bDOSts[14]=m_bDO14; bDOSts[7]=m_bDO7; bDOSts[15]=m_bDO15; Flag =true; // 输出各路开关量状态 PCI2312_SetDeviceDO( gl_hDevice, bDOSts); } void CDIOView::OnCheckDo0() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO0) { m_Check_DO0.SetWindowText(L"开关0 (开)"); } else { m_Check_DO0.SetWindowText(L"开关0 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo1() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO1) { m_Check_DO1.SetWindowText(L"开关1 (开)"); } else { m_Check_DO1.SetWindowText(L"开关1 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo2() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO2) { m_Check_DO2.SetWindowText(L"开关2 (开)"); } else { m_Check_DO2.SetWindowText(L"开关2 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo3() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO3) { m_Check_DO3.SetWindowText(L"开关3 (开)"); } else { m_Check_DO3.SetWindowText(L"开关3 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo4() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO4) { m_Check_DO4.SetWindowText(L"开关4 (开)"); } else { m_Check_DO4.SetWindowText(L"开关4 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo5() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO5) { m_Check_DO5.SetWindowText(L"开关5 (开)"); } else { m_Check_DO5.SetWindowText(L"开关5 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo6() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO6) { m_Check_DO6.SetWindowText(L"开关6 (开)"); } else { m_Check_DO6.SetWindowText(L"开关6 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo7() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO7) { m_Check_DO7.SetWindowText(L"开关7 (开)"); } else { m_Check_DO7.SetWindowText(L"开关7 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo8() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO8) { m_Check_DO8.SetWindowText(L"开关8 (开)"); } else { m_Check_DO8.SetWindowText(L"开关8 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo9() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO9) { m_Check_DO9.SetWindowText(L"开关9 (开)"); } else { m_Check_DO9.SetWindowText(L"开关9 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo10() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO10) { m_Check_DO10.SetWindowText(L"开关10 (开)"); } else { m_Check_DO10.SetWindowText(L"开关10 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo11() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO11) { m_Check_DO11.SetWindowText(L"开关11 (开)"); } else { m_Check_DO11.SetWindowText(L"开关11 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo12() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO12) { m_Check_DO12.SetWindowText(L"开关12 (开)"); } else { m_Check_DO12.SetWindowText(L"开关12 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo13() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO13) { m_Check_DO13.SetWindowText(L"开关13 (开)"); } else { m_Check_DO13.SetWindowText(L"开关13 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo14() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO14) { m_Check_DO14.SetWindowText(L"开关14 (开)"); } else { m_Check_DO14.SetWindowText(L"开关14 (关)"); } SetSwitchOut(); } void CDIOView::OnCheckDo15() { // TODO: Add your control notification handler code here UpdateData(TRUE); if(m_bDO15) { m_Check_DO15.SetWindowText(L"开关15 (开)"); } else { m_Check_DO15.SetWindowText(L"开关15 (关)"); } SetSwitchOut(); } void CDIOView::OnInitialUpdate() { CFormView::OnInitialUpdate(); // TODO: Add your specialized code here and/or call the base class CSysApp *pApp=(CSysApp *)AfxGetApp(); DeviceID = pApp->m_CurrentDeviceID; // 取得当前应用程序使用的设备ID号 m_Button_InitDeviceInt.EnableWindow(TRUE); m_Button_ReleaseDeviceInt.EnableWindow(FALSE); gl_hDevice = pApp->m_hDevice; // 创建设备对象 if(gl_hDevice==INVALID_HANDLE_VALUE) { bCreateDevice = FALSE; return; } bCreateDevice = TRUE; Flag = true; SetTimer(1, 250, NULL); // 启动定时器,查询开关量输入状态 } void CDIOView::OnDestroy() { CFormView::OnDestroy(); // TODO: Add your message handler code here if(bCreateDevice) PCI2312_ReleaseDevice(gl_hDevice); // 释放设备对象 } void CDIOView::OnListDevice() { // TODO: Add your command handler code here PCI2312_ListDeviceDlg(gl_hDevice); } BOOL bWaitInt=FALSE; LONG InIntCount=0, OutIntCount=0; UINT ProccessDataThread(PVOID pPara) // 绘制数据线程 { CDIOView* pView = (CDIOView*)pPara; CString str; InIntCount=0; OutIntCount=0; while(bWaitInt) { do{ if(!bWaitInt) goto ExitThread; }while(WaitForSingleObject (hEventInt, 100)!=WAIT_OBJECT_0); if(!bWaitInt) goto ExitThread; InIntCount = PCI2312_GetDeviceIntCount(gl_hDevice); // 取得中断程序内部计数 str.Format(L"%d", InIntCount); pView->m_Static_InIntCount.SetWindowText(str); OutIntCount++; str.Format(L"%d", OutIntCount); pView->m_Static_OutIntCount.SetWindowText(str); } ExitThread: return TRUE; } void CDIOView::OnBUTTONInitDeviceInt() { // TODO: Add your control notification handler code here // 创建窗口绘制子线程 m_pThreadInt=AfxBeginThread(ProccessDataThread,this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); m_pThreadInt->m_bAutoDelete=false; hEventInt = PCI2312_CreateSystemEvent(); if(!PCI2312_InitDeviceInt(gl_hDevice, hEventInt)) return; m_Button_InitDeviceInt.EnableWindow(FALSE); m_Button_ReleaseDeviceInt.EnableWindow(TRUE); m_Static_InIntCount.SetWindowText(L"0"); m_Static_OutIntCount.SetWindowText(L"0"); bWaitInt = TRUE; m_pThreadInt->ResumeThread(); } void CDIOView::OnBUTTONReleaseDeviceInt() { // TODO: Add your control notification handler code here bWaitInt = FALSE; SetEvent(hEventInt); if(!PCI2312_ReleaseDeviceInt(gl_hDevice)) return; PCI2312_ReleaseSystemEvent(hEventInt); m_Button_InitDeviceInt.EnableWindow(TRUE); m_Button_ReleaseDeviceInt.EnableWindow(FALSE); }