// demo2Dlg.cpp : implementation file // #include "stdafx.h" #include "demo2.h" #include "demo2Dlg.h" #include #include #include "I87KCAN.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDemo2Dlg dialog CDemo2Dlg::CDemo2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDemo2Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CDemo2Dlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDemo2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDemo2Dlg) DDX_Control(pDX, IDC_EDIT_RECDATA, m_recedata); DDX_Control(pDX, IDC_EDIT_SLOT, m_slot); DDX_Control(pDX, IDC_EDIT_NODE, m_node); DDX_Control(pDX, IDC_EDIT_DLEN, m_dlen); DDX_Control(pDX, IDC_EDIT_DATA, m_data); DDX_Control(pDX, IDC_COMBO_BAUD, m_baud); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDemo2Dlg, CDialog) //{{AFX_MSG_MAP(CDemo2Dlg) ON_BN_CLICKED(IDC_BUTTON_CONFIG, OnButtonConfig) ON_BN_CLICKED(IDC_BUTTON_OUTPUT, OnButtonOutput) ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect) ON_WM_TIMER() ON_BN_CLICKED(IDC_BUTTON_RX, OnButtonRx) ON_BN_CLICKED(IDC_BUTTON_TX, OnButtonTx) ON_WM_DESTROY() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDemo2Dlg message handlers #define comport 1 wchar_t wTempStr[100]; char Data[100],StrBuf[120];; unsigned char Node,DLen; int timercount = 5; BOOL CDemo2Dlg::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 return TRUE; // return TRUE unless you set the focus to a control } void CDemo2Dlg::OnButtonConfig() { int Ret; unsigned long baud; wchar_t wTempValue[10]; char TempValue[10]; //char TempBuf[100]; // TODO: Add your control notification handler code here Ret=Open_Com(comport,115200UL,8,0,1); // For WinCon communicate with slot module if (Ret){ Ret = Close_Com(comport); if(Ret){ MessageBox(_T("Close com Err"),NULL,NULL); return; } Ret=Open_Com(comport,115200UL,8,0,1); if (Ret){ wsprintf(wTempStr,_T("Open Com Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); return; } } m_slot.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); ChangeSlotTo87K((BYTE)(atoi(TempValue))); // Change slot to I-87120 switch(m_baud.GetCurSel()){ // Set CAN port baud rate case 0: baud = 125000L; break; case 1: baud = 250000L; break; case 2: baud = 500000L; break; } Ret=I87KCANInit(baud,0,0,0x00000000L,0xffffffffL,0,0); // Configure CAN port if (Ret){ wsprintf(wTempStr,_T("I87KCAN Initialize Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); Ret = Close_Com(comport); if(Ret) MessageBox(_T("Close com Err"),NULL,NULL); return; } memset(Data,0,100); } void CDemo2Dlg::OnDestroy() { wchar_t wTempValue[10]; char TempValue[10]; char TempBuf[100]; CDialog::OnDestroy(); // TODO: Add your message handler code here m_node.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); Node = (BYTE)(atoi(TempValue)); // Get DeviceNet slave node Sleep(500); sprintf(TempBuf,"_C%02X3\n",Node); //Close Allocate Connection message Send_Cmd(comport,TempBuf,0); Sleep(500); ResetI87KCAN(); Close_Com(comport); KillTimer(1); } void CDemo2Dlg::OnButtonConnect() { wchar_t wTempValue[10]; char TempValue[10]; char TempBuf[100]; // TODO: Add your control notification handler code here m_node.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); Node = (BYTE)(atoi(TempValue)); // Get DeviceNet slave node Sleep(500); sprintf(TempBuf,"_A%02X3\n",Node); //Send Allocate Connection message Send_Cmd(comport,TempBuf,0); Sleep(500); sprintf(TempBuf,"_S%02X28813\n",Node); //Set Connection Expected_Packet_Rate 5000ms Send_Cmd(comport,TempBuf,0); OnButtonOutput(); SetTimer(1,5,NULL); //SetTimer(2,100,NULL); } void CDemo2Dlg::OnButtonOutput() { wchar_t wTempValue[100]; char TempValue[10]; // TODO: Add your control notification handler code here _PIDLData m_node.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); Node = (BYTE)(atoi(TempValue)); // Get DeviceNet slave node m_dlen.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); DLen = (BYTE)(atoi(TempValue)); // Get data length m_data.GetWindowText(wTempValue,100); wcstombs(Data,wTempValue,100); sprintf(StrBuf,"_P%02X%X%s\n",Node,DLen,Data); //Send Master's I/O Poll Command Message //Send_Cmd(comport,StrBuf,0); } void CDemo2Dlg::OnButtonRx() { char TempBuf[100]; // TODO: Add your control notification handler code here sprintf(TempBuf,"_Tr\n"); Send_Cmd(comport,TempBuf,0); } void CDemo2Dlg::OnButtonTx() { char TempBuf[100]; // TODO: Add your control notification handler code here sprintf(TempBuf,"_Ts\n"); Send_Cmd(comport,TempBuf,0); } void CDemo2Dlg::OnTimer(UINT nIDEvent) { int Ret; char RecBuf[50]; wchar_t wRecBuf[100]; WORD m_WT; // TODO: Add your message handler code here and/or call default Ret=Receive_Cmd(comport,RecBuf,200,0,&m_WT); if(!Ret){ mbstowcs(wRecBuf,RecBuf,100); m_recedata.SetWindowText(wRecBuf); } if(!(timercount--)){ Send_Cmd(comport,StrBuf,0); // Polling output data timercount = 5; } CDialog::OnTimer(nIDEvent); }