// TxType DemoDlg.cpp : implementation file // #include "stdafx.h" #include "TxType Demo.h" #include "TxType DemoDlg.h" #include "pisocpm100.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTxTypeDemoDlg dialog CTxTypeDemoDlg::CTxTypeDemoDlg(CWnd* pParent /*=NULL*/) : CDialog(CTxTypeDemoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTxTypeDemoDlg) // 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 CTxTypeDemoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTxTypeDemoDlg) DDX_Control(pDX, IDC_LIST_RECEIVE, m_list_receive); DDX_Control(pDX, IDC_EDIT_TXTYPE, m_edit_txtype); DDX_Control(pDX, IDC_EDIT_SYNCID, m_edit_syncid); DDX_Control(pDX, IDC_EDIT_PDOCOBID, m_edit_pdocobid); DDX_Control(pDX, IDC_EDIT_CYCLIC, m_edit_cyclic); DDX_Control(pDX, IDC_COMBO_NODEID, m_cmb_nodeid); DDX_Control(pDX, IDC_COMBO_BOARD, m_cmb_board); DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_cmb_baudrate); DDX_Control(pDX, IDC_BUTTON_TXTYPE, m_btn_txtype); DDX_Control(pDX, IDC_BUTTON_SHUTDOWN, m_btn_shutdown); DDX_Control(pDX, IDC_BUTTON_SENDSYNC, m_btn_sendsync); DDX_Control(pDX, IDC_BUTTON_RTR, m_btn_rtr); DDX_Control(pDX, IDC_BUTTON_REMOVE, m_btn_remove); DDX_Control(pDX, IDC_BUTTON_CONFIGURE, m_btn_configure); DDX_Control(pDX, IDC_BUTTON_CLEAR, m_btn_clear); DDX_Control(pDX, IDC_BUTTON_ADDNODE, m_btn_addnode); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTxTypeDemoDlg, CDialog) //{{AFX_MSG_MAP(CTxTypeDemoDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_CBN_SELCHANGE(IDC_COMBO_BOARD, OnSelchangeComboBoard) ON_CBN_EDITCHANGE(IDC_COMBO_BOARD, OnEditchangeComboBoard) ON_CBN_SELENDCANCEL(IDC_COMBO_BOARD, OnSelendcancelComboBoard) ON_CBN_SELCHANGE(IDC_COMBO_BAUDRATE, OnSelchangeComboBaudrate) ON_CBN_SELCHANGE(IDC_COMBO_NODEID, OnSelchangeComboNodeid) ON_CBN_EDITCHANGE(IDC_COMBO_NODEID, OnEditchangeComboNodeid) ON_CBN_SELENDCANCEL(IDC_COMBO_NODEID, OnSelendcancelComboNodeid) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON_CONFIGURE, OnButtonConfigure) ON_BN_CLICKED(IDC_BUTTON_SHUTDOWN, OnButtonShutdown) ON_BN_CLICKED(IDC_BUTTON_ADDNODE, OnButtonAddnode) ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove) ON_BN_CLICKED(IDC_BUTTON_TXTYPE, OnButtonTxtype) ON_BN_CLICKED(IDC_BUTTON_RTR, OnButtonRtr) ON_BN_CLICKED(IDC_BUTTON_SENDSYNC, OnButtonSendsync) ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CTxTypeDemoDlg message handlers BYTE Board; BYTE NodeId; BYTE Baudrate; WORD MsgCnt=0; CString tempstr; BOOL CTxTypeDemoDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 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 // TODO: Add extra initialization here m_edit_pdocobid.SetWindowText("182"); m_edit_txtype.SetWindowText("FF"); m_edit_syncid.SetWindowText("80"); m_edit_cyclic.SetWindowText("1000"); Board = 0; Baudrate = 0; NodeId = 1; m_cmb_board.SetCurSel(0); m_cmb_baudrate.SetCurSel(0); m_cmb_nodeid.SetCurSel(1); m_cmb_board.EnableWindow(TRUE); m_cmb_baudrate.EnableWindow(TRUE); m_btn_configure.EnableWindow(TRUE); m_cmb_nodeid.EnableWindow(FALSE); m_btn_shutdown.EnableWindow(FALSE); m_btn_addnode.EnableWindow(FALSE); m_btn_remove.EnableWindow(FALSE); m_btn_txtype.EnableWindow(FALSE); m_btn_rtr.EnableWindow(FALSE); m_btn_sendsync.EnableWindow(FALSE); m_btn_clear.EnableWindow(FALSE); m_edit_pdocobid.EnableWindow(FALSE); m_edit_txtype.EnableWindow(FALSE); m_edit_syncid.EnableWindow(FALSE); m_edit_cyclic.EnableWindow(FALSE); return TRUE; // return TRUE unless you set the focus to a control } void CTxTypeDemoDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CTxTypeDemoDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CTxTypeDemoDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } ////////////////// Destroy the demo ///////////////////////// void CTxTypeDemoDlg::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here CPM100_ShutdownMaster(Board); CPM100_CloseBoard(Board); } //////////////// For Board Combo Box /////////////////////// // When the Combo Box has changed the selection void CTxTypeDemoDlg::OnSelchangeComboBoard() { // TODO: Add your control notification handler code here Board = m_cmb_board.GetCurSel(); } // When the Combo Box has changed the text void CTxTypeDemoDlg::OnEditchangeComboBoard() { char TempText[10],*end; // TODO: Add your control notification handler code here m_cmb_board.GetWindowText(TempText,10); Board = (BYTE)strtol(TempText,&end,10); if(Board > 15) Board = 15; } // When the Combo Box has lose focus void CTxTypeDemoDlg::OnSelendcancelComboBoard() { // TODO: Add your control notification handler code here m_cmb_board.SetCurSel(Board); } //////////////// For Baudrate Combo Box /////////////////////// // When the Combo Box has changed the selection void CTxTypeDemoDlg::OnSelchangeComboBaudrate() { // TODO: Add your control notification handler code here Baudrate = m_cmb_baudrate.GetCurSel(); } //////////////// For Node ID Combo Box /////////////////////// // When the Combo Box has changed the selection void CTxTypeDemoDlg::OnSelchangeComboNodeid() { // TODO: Add your control notification handler code here NodeId = m_cmb_nodeid.GetCurSel(); if(NodeId == 0) NodeId = 1; } // When the Combo Box has changed the text void CTxTypeDemoDlg::OnEditchangeComboNodeid() { char TempText[10],*end; // TODO: Add your control notification handler code here m_cmb_nodeid.GetWindowText(TempText,10); NodeId = (BYTE)strtol(TempText,&end,10); if(NodeId > 127) NodeId = 127; // The range of Node Id is 1 ~ 127 else if(NodeId == 0) NodeId = 1; } // When the Combo Box has lose focus void CTxTypeDemoDlg::OnSelendcancelComboNodeid() { // TODO: Add your control notification handler code here m_cmb_nodeid.SetCurSel(NodeId); } //////////////////////////////////////////////////////////////// void CTxTypeDemoDlg::OnButtonConfigure() { int ret; // TODO: Add your control notification handler code here ret = CPM100_ActiveBoard(Board); if(ret){ tempstr.Format("ActiveBoard failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } ret = CPM100_InitMaster(Board,Baudrate); if(ret){ tempstr.Format("Initialize Master failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } SetTimer(123,10,NULL); m_cmb_board.EnableWindow(FALSE); m_cmb_baudrate.EnableWindow(FALSE); m_btn_configure.EnableWindow(FALSE); m_cmb_nodeid.EnableWindow(TRUE); m_btn_shutdown.EnableWindow(TRUE); m_btn_addnode.EnableWindow(TRUE); } void CTxTypeDemoDlg::OnButtonShutdown() { int ret; // TODO: Add your control notification handler code here ret = CPM100_ShutdownMaster(Board); if(ret){ tempstr.Format("Master shutdown failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } ret = CPM100_CloseBoard(Board); if(ret){ tempstr.Format("Close board failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } KillTimer(123); m_cmb_board.EnableWindow(TRUE); m_cmb_baudrate.EnableWindow(TRUE); m_btn_configure.EnableWindow(TRUE); m_cmb_nodeid.EnableWindow(FALSE); m_btn_shutdown.EnableWindow(FALSE); m_btn_addnode.EnableWindow(FALSE); m_btn_remove.EnableWindow(FALSE); m_btn_txtype.EnableWindow(FALSE); m_btn_rtr.EnableWindow(FALSE); m_btn_sendsync.EnableWindow(FALSE); m_btn_clear.EnableWindow(FALSE); m_edit_pdocobid.EnableWindow(FALSE); m_edit_txtype.EnableWindow(FALSE); m_edit_syncid.EnableWindow(FALSE); m_edit_cyclic.EnableWindow(FALSE); } void CTxTypeDemoDlg::OnButtonAddnode() { int ret; // TODO: Add your control notification handler code here ret = CPM100_AddNode(Board,NodeId); if(ret){ tempstr.Format("Node add failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } MsgCnt = 0; m_list_receive.ResetContent(); m_cmb_nodeid.EnableWindow(FALSE); m_btn_addnode.EnableWindow(FALSE); m_btn_remove.EnableWindow(TRUE); m_btn_txtype.EnableWindow(TRUE); m_btn_rtr.EnableWindow(TRUE); m_btn_sendsync.EnableWindow(TRUE); m_btn_clear.EnableWindow(TRUE); m_edit_pdocobid.EnableWindow(TRUE); m_edit_txtype.EnableWindow(TRUE); m_edit_syncid.EnableWindow(TRUE); m_edit_cyclic.EnableWindow(TRUE); } void CTxTypeDemoDlg::OnButtonRemove() { int ret; // TODO: Add your control notification handler code here ret = CPM100_RemoveNode(Board,NodeId); if(ret){ tempstr.Format("Node remove failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } m_cmb_nodeid.EnableWindow(TRUE); m_btn_addnode.EnableWindow(TRUE); m_btn_remove.EnableWindow(FALSE); m_btn_txtype.EnableWindow(FALSE); m_btn_rtr.EnableWindow(FALSE); m_btn_sendsync.EnableWindow(FALSE); m_btn_clear.EnableWindow(FALSE); m_edit_pdocobid.EnableWindow(FALSE); m_edit_txtype.EnableWindow(FALSE); m_edit_syncid.EnableWindow(FALSE); m_edit_cyclic.EnableWindow(FALSE); } void CTxTypeDemoDlg::OnButtonTxtype() { int ret; char TempText[10],*end; BYTE TxType; WORD CobId; // TODO: Add your control notification handler code here m_edit_pdocobid.GetWindowText(TempText,10); CobId = (WORD)strtol(TempText,&end,16); m_edit_txtype.GetWindowText(TempText,10); TxType = (BYTE)strtol(TempText,&end,16); ret = CPM100_PDOTxType(Board, NodeId, CobId, TxType); if(ret){ tempstr.Format("Set transmission type failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } else{ tempstr.Format("Set type OK"); MessageBox(tempstr,"OK",MB_OK); } } void CTxTypeDemoDlg::OnButtonRtr() { int ret,i; char TempText[10],*end,PDOString[200]; BYTE RDatalen,RData[8]; WORD CobId; DWORD UpperTime,LowerTime; double TimeStamp; // TODO: Add your control notification handler code here m_edit_pdocobid.GetWindowText(TempText,10); CobId = (WORD)strtol(TempText,&end,16); ret = CPM100_PDORemote(Board, CobId, &RDatalen, RData, &UpperTime, &LowerTime); if(ret){ tempstr.Format("Read PDO failure, error = %d",ret); MessageBox(tempstr,"Error",MB_OK|MB_ICONSTOP); return; } MsgCnt = MsgCnt + 1; sprintf(PDOString,"%03d -- ",MsgCnt); if(RDatalen){ for(i=0;i