// demo1Dlg.cpp : implementation file // #include "stdafx.h" #include "demo1.h" #include "demo1Dlg.h" #include #include #include "I87KCAN.h" #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_EDIT_RECDATA, m_recedata); DDX_Control(pDX, IDC_EDIT_NODE, m_node); DDX_Control(pDX, IDC_EDIT_DATA, m_data); DDX_Control(pDX, IDC_EDIT_CHANNEL, m_channel); DDX_Control(pDX, IDC_EDIT_SLOT, m_slot); DDX_Control(pDX, IDC_COMBO_BAUD, m_baud); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDemo1Dlg, CDialog) //{{AFX_MSG_MAP(CDemo1Dlg) ON_BN_CLICKED(IDC_BUTTON_DI, OnButtonDi) ON_BN_CLICKED(IDC_BUTTON_AI, OnButtonAi) ON_BN_CLICKED(IDC_BUTTON_DO, OnButtonDo) ON_BN_CLICKED(IDC_BUTTON_AO, OnButtonAo) ON_WM_TIMER() ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON_RX, OnButtonRx) ON_BN_CLICKED(IDC_BUTTON_TX, OnButtonTx) ON_BN_CLICKED(IDC_BUTTON_CONFIG, OnButtonConfig) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDemo1Dlg message handlers #define comport 0 wchar_t wTempStr[100]; int tempcount=5; BYTE tdata=0; 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 return TRUE; // return TRUE unless you set the focus to a control } void CDemo1Dlg::OnButtonConfig() { int Ret; unsigned long baud; wchar_t wTempValue[10]; char TempValue[10]; // TODO: Add your control notification handler code here Ret=Open_Com(comport,115200UL,8,0,0); // For WinPac 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,0); 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 = 10000L; break; case 1: baud = 20000L; break; case 2: baud = 50000L; break; case 3: baud = 125000L; break; case 4: baud = 250000L; break; case 5: baud = 500000L; break; case 6: baud = 800000L; break; case 7: baud = 1000000L; 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; } SetTimer(1,5,NULL); } void CDemo1Dlg::OnButtonDi() { unsigned char node,channel; 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 CANopen slave node m_channel.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); channel = (BYTE)(atoi(TempValue)); // Get I/O channel sprintf(TempBuf,"_DI%03X%02X\n",0x600+node,channel); Send_Cmd(comport,TempBuf,0); } void CDemo1Dlg::OnButtonAi() { unsigned char node,channel; 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 CANopen slave node m_channel.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); channel = (BYTE)(atoi(TempValue)); // Get I/O channel sprintf(TempBuf,"_AI%03X%02X\n",0x600+node,channel); Send_Cmd(comport,TempBuf,0); } void CDemo1Dlg::OnButtonDo() { unsigned char node,channel,data; wchar_t wTempValue[10]; char TempValue[10],*endtemp; 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 CANopen slave node m_channel.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); channel = (BYTE)(atoi(TempValue)); // Get I/O channel m_data.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); data = (BYTE)strtol(TempValue,&endtemp,16); // Get I/O data sprintf(TempBuf,"_DO%03X%02X%02X\n",0x600+node,channel,data); Send_Cmd(comport,TempBuf,0); } void CDemo1Dlg::OnButtonAo() { unsigned char node,channel; unsigned int data; wchar_t wTempValue[10]; char TempValue[10],*endtemp; 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 CANopen slave node m_channel.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); channel = (BYTE)(atoi(TempValue)); // Get I/O channel m_data.GetWindowText(wTempValue,10); wcstombs(TempValue,wTempValue,10); data = (WORD)strtol(TempValue,&endtemp,16); // Get I/O data sprintf(TempBuf,"_AO%03X%02X%04X\n",0x600+node,channel,data); Send_Cmd(comport,TempBuf,0); } void CDemo1Dlg::OnButtonRx() { char TempBuf[100]; // TODO: Add your control notification handler code here sprintf(TempBuf,"_Tr\n"); Send_Cmd(comport,TempBuf,0); } void CDemo1Dlg::OnButtonTx() { char TempBuf[100]; // TODO: Add your control notification handler code here sprintf(TempBuf,"_Ts\n"); Send_Cmd(comport,TempBuf,0); } void CDemo1Dlg::OnTimer(UINT nIDEvent) { int Ret; char RecBuf[50]; wchar_t wRecBuf[100]; WORD m_WT; CString strtemp; // 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(!(tempcount--)){ strtemp.Format(_T("%x"),tdata++); m_data.SetWindowText(strtemp); OnButtonDo(); Sleep(100); OnButtonDi(); tempcount = 5; } */ CDialog::OnTimer(nIDEvent); } void CDemo1Dlg::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here ResetI87KCAN(); Close_Com(comport); }