// RecMsgDlg.cpp : implementation file // #include "stdafx.h" #include "RecMsg.h" #include "RecMsgDlg.h" #include "..\87CANlib\ver_110\I87KCAN.h" #include #include #define MAX_LINE 1000 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CRecMsgDlg dialog CRecMsgDlg::CRecMsgDlg(CWnd* pParent /*=NULL*/) : CDialog(CRecMsgDlg::IDD, pParent) { //{{AFX_DATA_INIT(CRecMsgDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CRecMsgDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CRecMsgDlg) DDX_Control(pDX, IDC_OVERFLOW_STATIC, m_OverFlow); DDX_Control(pDX, IDC_STATUS_STATIC, m_CANStatus); DDX_Control(pDX, IDC_SLOT_COMBO, m_Slot); DDX_Control(pDX, IDC_RUN_BUTTON, m_Run); DDX_Control(pDX, IDC_Reset_BUTTON, m_Reset); DDX_Control(pDX, IDC_MSG_LIST_CTRL, m_MsgListCtrl); DDX_Control(pDX, IDC_INIT_BUTTON, m_Init); DDX_Control(pDX, IDC_BAUD_COMBO, m_Baud); DDX_Control(pDX, IDC_CLEAR_BUTTON, m_Clear); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CRecMsgDlg, CDialog) //{{AFX_MSG_MAP(CRecMsgDlg) ON_CBN_SELENDOK(IDC_SLOT_COMBO, OnSelendokSlotCombo) ON_WM_TIMER() ON_WM_DESTROY() ON_BN_CLICKED(IDC_Reset_BUTTON, OnResetBUTTON) ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton) ON_BN_CLICKED(IDC_INIT_BUTTON, OnInitButton) ON_BN_CLICKED(IDC_RUN_BUTTON, OnRunButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CRecMsgDlg message handlers #define Comport 1 WORD OldSlotSelect,PortEnable,TimerEnable,MsgFormat,LineCnt; BYTE OldCANReg=0,OldOverflogFlag; void CRecMsgDlg::FormInit(void) { WORD TempRet; //Initiate all objects and parameters used in the dialog m_Reset.EnableWindow(FALSE); m_Run.EnableWindow(FALSE); m_CANStatus.SetWindowText(_T("****")); if (TimerEnable) KillTimer(12345); TimerEnable=0; LineCnt=0; TempRet=Close_Com(Comport); if (TempRet) return; PortEnable=0; } BOOL CRecMsgDlg::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 //initial button m_Reset.EnableWindow(FALSE); m_Run.EnableWindow(FALSE); m_CANStatus.SetWindowText(_T("****")); //initial button FormInit(); //parameter initialize m_Slot.SetCurSel(0); OldSlotSelect=0; m_Baud.SetCurSel(11); //initiate list control object m_MsgListCtrl.InsertColumn(0,_T("NO."),LVCFMT_CENTER,34); m_MsgListCtrl.InsertColumn(1,_T("Mode"),LVCFMT_CENTER,42); m_MsgListCtrl.InsertColumn(2,_T("ID"),LVCFMT_CENTER,70); m_MsgListCtrl.InsertColumn(3,_T("RTR"),LVCFMT_CENTER,34); m_MsgListCtrl.InsertColumn(4,_T("L"),LVCFMT_CENTER,19); m_MsgListCtrl.InsertColumn(5,_T("D0"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(6,_T("D1"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(7,_T("D2"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(8,_T("D3"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(9,_T("D4"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(10,_T("D5"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(11,_T("D6"),LVCFMT_CENTER,35); m_MsgListCtrl.InsertColumn(12,_T("D7"),LVCFMT_CENTER,35); return TRUE; // return TRUE unless you set the focus to a control } void CRecMsgDlg::OnSelendokSlotCombo() { //Check if the slot combo box is changed or not, and do something for different situation if (m_Slot.GetCurSel()!=OldSlotSelect){ FormInit(); OldSlotSelect=m_Slot.GetCurSel(); return; } } void CRecMsgDlg::OnTimer(UINT nIDEvent) { BYTE TempCANReg,TempOverflogFlag,Mode,RTR,DataLen,Data[8]; wchar_t wTempStr[200]; WORD Ret; DWORD ID; //Get I-87KCAN status from I-87KCAN module Ret=GetStatus(&TempCANReg,&TempOverflogFlag); if (Ret){ FormInit(); wsprintf(wTempStr,_T("Get Status Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); return; } if (TempCANReg!=OldCANReg){ wsprintf(wTempStr,_T("0x%02X"),TempCANReg); m_CANStatus.SetWindowText(wTempStr); OldCANReg=TempCANReg; } //Show the I-87KCAN status if (TempOverflogFlag!=OldOverflogFlag){ wsprintf(wTempStr,_T("0x%02X"),TempCANReg); m_OverFlow.SetWindowText(wTempStr); OldOverflogFlag=TempOverflogFlag; } //Get CAN message from I-87KCAN Ret=GetCANMsg(&Mode,&ID,&RTR,&DataLen,Data); if (Ret){ if (Ret==CAN87K_FIFO_EMPTY) return; FormInit(); wsprintf(wTempStr,_T("Get Message Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); return; } //Show the receive CAN message information in the list control object if (RTR){ CString strText; strText.Format(_T("%03d"),LineCnt); m_MsgListCtrl.InsertItem(LineCnt,strText); m_MsgListCtrl.SetItemText(LineCnt,1,Mode?_T("2.0B"):_T("2.0A")); strText.Format(_T("%lX"),ID); m_MsgListCtrl.SetItemText(LineCnt,2, strText); m_MsgListCtrl.SetItemText(LineCnt,3, RTR?_T("Y"):_T("N")); strText.Format(_T("%d"),DataLen); m_MsgListCtrl.SetItemText(LineCnt,4, strText); } else{ CString strText; strText.Format(_T("%03d"),LineCnt); m_MsgListCtrl.InsertItem(LineCnt,strText); m_MsgListCtrl.SetItemText(LineCnt,1,Mode?_T("2.0B"):_T("2.0A")); strText.Format(_T("%lX"),ID); m_MsgListCtrl.SetItemText(LineCnt,2, strText); m_MsgListCtrl.SetItemText(LineCnt,3, RTR?_T("Y"):_T("N")); strText.Format(_T("%d"),DataLen); m_MsgListCtrl.SetItemText(LineCnt,4, strText); for (int i=0;i5){ CSize Rolling; Rolling.cx=0; Rolling.cy=30*(LineCnt-5); m_MsgListCtrl.Scroll(Rolling); } if (LineCnt==MAX_LINE){ m_MsgListCtrl.DeleteAllItems( ); LineCnt=0; } CDialog::OnTimer(nIDEvent); } void CRecMsgDlg::OnDestroy() { CDialog::OnDestroy(); //recover the timer setting and COM 1 setting if (TimerEnable) KillTimer(12345); if (PortEnable) Close_Com(Comport); } void CRecMsgDlg::OnResetBUTTON() { //Send command to reset the I-87KCAN module WORD Ret; wchar_t wTempStr[200]; Ret=ResetI87KCAN(); if (Ret){ FormInit(); wsprintf(wTempStr,_T("Get Status Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); return; } } void CRecMsgDlg::OnClearButton() { //Clear all information stored in the list control object m_MsgListCtrl.DeleteAllItems( ); LineCnt=0; } void CRecMsgDlg::OnInitButton() { wchar_t wTempStr[100]; BYTE TempBt0=0,TempBt1=0; WORD Ret; CString TempStr; DWORD BaudMap[]={5000UL,10000UL,20000UL,25000UL,50000UL,100000UL,125000UL, 20000UL,250000UL,500000UL,800000UL,1000000UL}; //Check COM 1 status and open COM 1 if (Get_Com_Status(Comport) || PortEnable){ Ret=Close_Com(Comport); if (Ret) return; if (TimerEnable) KillTimer(12345); TimerEnable=0; PortEnable=0; } Ret=Open_Com(Comport,115200UL,8,0,1); if (Ret){ FormInit(); wsprintf(wTempStr,_T("Open Com1 Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); return; } PortEnable=1; //Select the slot where the I-87KCAN is plugged. ChangeSlotTo87K(m_Slot.GetCurSel()+1); //configue the I-87KCAN setting and initiate the I-87KCAN Ret=I87KCANInit(BaudMap[m_Baud.GetCurSel()],0,0,0x00000000UL,0xffffffffUL,0,0); if (Ret){ wsprintf(wTempStr,_T("Initiate I-87KCAN Error, Error Code=%d"),Ret); MessageBox(wTempStr,_T("Error"),MB_OK); return; } //start the objects and parameters used in the dialog m_Reset.EnableWindow(TRUE); m_Run.EnableWindow(TRUE); m_CANStatus.SetWindowText(_T("0x0C")); m_OverFlow.SetWindowText(_T("0x00")); if (!TimerEnable) SetTimer(12345,1,NULL); TimerEnable=1; } void CRecMsgDlg::OnRunButton() { //Let the tiemr stop or continue if (TimerEnable){ m_Run.SetWindowText(_T("Go")); KillTimer(12345); TimerEnable=0; } else{ m_Run.SetWindowText(_T("Pause")); SetTimer(12345,1,NULL); TimerEnable=1; } }