// demo1Dlg.cpp : implementation file // #include "stdafx.h" #include "demo1.h" #include "demo1Dlg.h" #include "..\lib\I87123.h" #include #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDemo1Dlg dialog CDemo1Dlg::CDemo1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDemo1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemo1Dlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDemo1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDemo1Dlg) DDX_Control(pDX, IDC_STATIC_DI8, m_di8); DDX_Control(pDX, IDC_STATIC_DI7, m_di7); DDX_Control(pDX, IDC_STATIC_DI6, m_di6); DDX_Control(pDX, IDC_STATIC_DI5, m_di5); DDX_Control(pDX, IDC_STATIC_DI4, m_di4); DDX_Control(pDX, IDC_STATIC_DI3, m_di3); DDX_Control(pDX, IDC_STATIC_DI2, m_di2); DDX_Control(pDX, IDC_STATIC_DI1, m_di1); DDX_Control(pDX, IDC_CHECK_DO8, m_do8); DDX_Control(pDX, IDC_CHECK_DO7, m_do7); DDX_Control(pDX, IDC_CHECK_DO6, m_do6); DDX_Control(pDX, IDC_CHECK_DO5, m_do5); DDX_Control(pDX, IDC_CHECK_DO4, m_do4); DDX_Control(pDX, IDC_CHECK_DO3, m_do3); DDX_Control(pDX, IDC_CHECK_DO2, m_do2); DDX_Control(pDX, IDC_CHECK_DO1, m_do1); DDX_Control(pDX, IDC_BUTTON_SHUTDOWN, m_button_shutdown); DDX_Control(pDX, IDC_BUTTON_CONFIGURE, m_button_config); DDX_Control(pDX, IDC_COMBO_SLOT, m_comb_slot); DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_comb_baudrate); DDX_Control(pDX, IDC_BUTTON_REMOVENODE, m_button_removenode); DDX_Control(pDX, IDC_BUTTON_ADDNODE, m_button_addnode); DDX_Control(pDX, IDC_STATIC_ON, m_pic_on); DDX_Control(pDX, IDC_STATIC_OFF, m_pic_off); DDX_Control(pDX, IDC_STATIC_NODE, m_pic_node); DDX_Control(pDX, IDC_COMBO_NODE, m_comb_node); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDemo1Dlg, CDialog) //{{AFX_MSG_MAP(CDemo1Dlg) ON_BN_CLICKED(IDC_BUTTON_ADDNODE, OnButtonAddnode) ON_CBN_SELCHANGE(IDC_COMBO_NODE, OnSelchangeComboNode) ON_BN_CLICKED(IDC_BUTTON_REMOVENODE, OnButtonRemovenode) ON_BN_CLICKED(IDC_BUTTON_CONFIGURE, OnButtonConfigure) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON_SHUTDOWN, OnButtonShutdown) ON_WM_TIMER() 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) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDemo1Dlg message handlers CString strText; BYTE AddNode[127]; // To remeber which node had been added BYTE IsConfig = 0; BYTE DO_data = 0; // Output do data BYTE DO_temp = 0; // Output do temp data BYTE DI_data = 0; // Input di data BOOL CDemo1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 CenterWindow(GetDesktopWindow()); // center to the hpc screen // TODO: Add extra initialization here memset(AddNode,0,127); m_pic_on.ShowWindow(SW_HIDE); m_pic_off.ShowWindow(SW_HIDE); m_button_config.ShowWindow(SW_SHOW); m_button_shutdown.ShowWindow(SW_HIDE); m_button_addnode.ShowWindow(SW_SHOW); m_button_removenode.ShowWindow(SW_HIDE); m_button_addnode.EnableWindow(FALSE); m_button_removenode.EnableWindow(FALSE); m_comb_node.EnableWindow(FALSE); m_comb_slot.SetCurSel(0); m_comb_baudrate.SetCurSel(3); m_comb_node.SetCurSel(0); m_do1.EnableWindow(FALSE); m_do2.EnableWindow(FALSE); m_do3.EnableWindow(FALSE); m_do4.EnableWindow(FALSE); m_do5.EnableWindow(FALSE); m_do6.EnableWindow(FALSE); m_do7.EnableWindow(FALSE); m_do8.EnableWindow(FALSE); return TRUE; // return TRUE unless you set the focus to a control } void CDemo1Dlg::OnDestroy() { int ret; CDialog::OnDestroy(); // TODO: Add your message handler code here if(IsConfig){ ret = I87123_ShutdownMaster(); if(ret){ strText.Format(_T("Shutdown Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); } KillTimer(123); } } void CDemo1Dlg::OnButtonConfigure() { int ret; // TODO: Add your control notification handler code here ChangeSlotTo87K(m_comb_slot.GetCurSel()); ret = I87123_Configure(m_comb_baudrate.GetCurSel()); // Configure I-87123 Baudrate if(ret){ strText.Format(_T("Configure Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); } else{ IsConfig = 1; m_button_config.ShowWindow(SW_HIDE); m_button_shutdown.ShowWindow(SW_SHOW); m_button_addnode.EnableWindow(TRUE); m_button_removenode.EnableWindow(TRUE); m_comb_node.EnableWindow(TRUE); m_comb_slot.EnableWindow(FALSE); m_comb_baudrate.EnableWindow(FALSE); m_do1.EnableWindow(TRUE); m_do2.EnableWindow(TRUE); m_do3.EnableWindow(TRUE); m_do4.EnableWindow(TRUE); m_do5.EnableWindow(TRUE); m_do6.EnableWindow(TRUE); m_do7.EnableWindow(TRUE); m_do8.EnableWindow(TRUE); SetTimer(123,20,NULL); } } void CDemo1Dlg::OnButtonShutdown() { int ret; // TODO: Add your control notification handler code here ret = I87123_ShutdownMaster(); if(ret){ strText.Format(_T("ShutdownMaster Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); } else{ DO_data = 0; DO_temp = 0; IsConfig = 0; m_button_config.ShowWindow(SW_SHOW); m_button_shutdown.ShowWindow(SW_HIDE); m_button_addnode.EnableWindow(FALSE); m_button_removenode.EnableWindow(FALSE); m_comb_node.EnableWindow(FALSE); memset(AddNode,0,127); OnSelchangeComboNode(); m_comb_slot.EnableWindow(TRUE); m_comb_baudrate.EnableWindow(TRUE); m_do1.EnableWindow(FALSE); m_do2.EnableWindow(FALSE); m_do3.EnableWindow(FALSE); m_do4.EnableWindow(FALSE); m_do5.EnableWindow(FALSE); m_do6.EnableWindow(FALSE); m_do7.EnableWindow(FALSE); m_do8.EnableWindow(FALSE); m_do1.SetCheck(0); m_do2.SetCheck(0); m_do3.SetCheck(0); m_do4.SetCheck(0); m_do5.SetCheck(0); m_do6.SetCheck(0); m_do7.SetCheck(0); m_do8.SetCheck(0); m_di1.SetBitmap(m_pic_off.GetBitmap()); // If read DI error, to set all DI error m_di2.SetBitmap(m_pic_off.GetBitmap()); m_di3.SetBitmap(m_pic_off.GetBitmap()); m_di4.SetBitmap(m_pic_off.GetBitmap()); m_di5.SetBitmap(m_pic_off.GetBitmap()); m_di6.SetBitmap(m_pic_off.GetBitmap()); m_di7.SetBitmap(m_pic_off.GetBitmap()); m_di8.SetBitmap(m_pic_off.GetBitmap()); KillTimer(123); } } void CDemo1Dlg::OnButtonAddnode() { int ret; // TODO: Add your control notification handler code here ret = I87123_AddNode(m_comb_node.GetCurSel()+1); if(ret){ strText.Format(_T("AddNode Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); } else{ AddNode[m_comb_node.GetCurSel()] = 1; // The node hab been added OnSelchangeComboNode(); } } void CDemo1Dlg::OnButtonRemovenode() { int ret; // TODO: Add your control notification handler code here ret = I87123_RemoveNode(m_comb_node.GetCurSel()+1); if(ret){ strText.Format(_T("RemoveNode Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); } else{ AddNode[m_comb_node.GetCurSel()] = 0; // The node hab been removed OnSelchangeComboNode(); } } void CDemo1Dlg::OnSelchangeComboNode() { // TODO: Add your control notification handler code here if(AddNode[m_comb_node.GetCurSel()]){ // The node hab been added m_pic_node.SetBitmap(m_pic_on.GetBitmap()); m_button_addnode.ShowWindow(SW_HIDE); m_button_removenode.ShowWindow(SW_SHOW); } else{ // The node hab not been added m_pic_node.SetBitmap(m_pic_off.GetBitmap()); m_button_addnode.ShowWindow(SW_SHOW); m_button_removenode.ShowWindow(SW_HIDE); } } void CDemo1Dlg::OnCheckDo1() { // TODO: Add your control notification handler code here if(m_do1.GetCheck()){ DO_temp = DO_temp | 0x01; // If the DO 1 is checked, the bit 1 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xFE; // If the DO 1 isn't checked, the bit 1 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo2() { // TODO: Add your control notification handler code here if(m_do2.GetCheck()){ DO_temp = DO_temp | 0x02; // If the DO 2 is checked, the bit 2 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xFD; // If the DO 2 isn't checked, the bit 2 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo3() { // TODO: Add your control notification handler code here if(m_do3.GetCheck()){ DO_temp = DO_temp | 0x04; // If the DO 3 is checked, the bit 3 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xFB; // If the DO 3 isn't checked, the bit 3 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo4() { // TODO: Add your control notification handler code here if(m_do4.GetCheck()){ DO_temp = DO_temp | 0x08; // If the DO 4 is checked, the bit 4 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xF7; // If the DO 4 isn't checked, the bit 4 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo5() { // TODO: Add your control notification handler code here if(m_do5.GetCheck()){ DO_temp = DO_temp | 0x10; // If the DO 5 is checked, the bit 5 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xEF; // If the DO 5 isn't checked, the bit 5 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo6() { // TODO: Add your control notification handler code here if(m_do6.GetCheck()){ DO_temp = DO_temp | 0x20; // If the DO 6 is checked, the bit 6 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xDF; // If the DO 6 isn't checked, the bit 6 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo7() { // TODO: Add your control notification handler code here if(m_do7.GetCheck()){ DO_temp = DO_temp | 0x40; // If the DO 7 is checked, the bit 7 of DO_temp is 1 } else{ DO_temp = DO_temp & 0xBF; // If the DO 7 isn't checked, the bit 7 of DO_temp is 0 } } void CDemo1Dlg::OnCheckDo8() { // TODO: Add your control notification handler code here if(m_do8.GetCheck()){ DO_temp = DO_temp | 0x80; // If the DO 8 is checked, the bit 8 of DO_temp is 1 } else{ DO_temp = DO_temp & 0x7F; // If the DO 8 isn't checked, the bit 8 of DO_temp is 0 } } void CDemo1Dlg::OnTimer(UINT nIDEvent) { int ret; int static RET=0; // TODO: Add your message handler code here and/or call default if(DO_data != DO_temp){ // If DO check box has changed, the DO_data will be output DO_data = DO_temp; if(AddNode[m_comb_node.GetCurSel()]){ // Check the node if had been added ret = I87123_WriteDO(m_comb_node.GetCurSel()+1, 0, DO_data); if(ret){ strText.Format(_T("Write DO Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); } } } if(AddNode[m_comb_node.GetCurSel()]){ // Check the node if had been added ret = I87123_ReadDI(m_comb_node.GetCurSel()+1, 0, &DI_data); // Read DI data if(ret){ if(!RET){ // To avoid the error loop RET = ret; strText.Format(_T("Read DI Error Code = %d"),ret); MessageBox(strText,_T("Error"),MB_ICONERROR | MB_OK); m_di1.SetBitmap(m_pic_off.GetBitmap()); // If read DI error, to set all DI error m_di2.SetBitmap(m_pic_off.GetBitmap()); m_di3.SetBitmap(m_pic_off.GetBitmap()); m_di4.SetBitmap(m_pic_off.GetBitmap()); m_di5.SetBitmap(m_pic_off.GetBitmap()); m_di6.SetBitmap(m_pic_off.GetBitmap()); m_di7.SetBitmap(m_pic_off.GetBitmap()); m_di8.SetBitmap(m_pic_off.GetBitmap()); } } else{ RET = ret; if(DI_data & 0x01){ // If DI bit 1 is ON m_di1.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 1 is OFF m_di1.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x02){ // If DI bit 2 is ON m_di2.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 2 is OFF m_di2.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x04){ // If DI bit 3 is ON m_di3.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 3 is OFF m_di3.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x08){ // If DI bit 4 is ON m_di4.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 4 is OFF m_di4.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x10){ // If DI bit 5 is ON m_di5.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 5 is OFF m_di5.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x20){ // If DI bit 6 is ON m_di6.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 6 is OFF m_di6.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x40){ // If DI bit 7 is ON m_di7.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 7 is OFF m_di7.SetBitmap(m_pic_off.GetBitmap()); } if(DI_data & 0x80){ // If DI bit 8 is ON m_di8.SetBitmap(m_pic_on.GetBitmap()); } else{ // If DI bit 8 is OFF m_di8.SetBitmap(m_pic_off.GetBitmap()); } } } CDialog::OnTimer(nIDEvent); }