void CIntRamView::OnRcvData()      //每次讀取一筆buffer中的data,重複256次

............................
Item.col++;         //①每讀取一筆數,列號加1
if (Item.col > m_pGridCtrl->GetColumnCount()-1)  //②當讀滿一行(16列)時,列號指向下一行的開頭。
{
Item.col = 1;           //③列號指向下一行的開頭。
Item.row ++;           //④行號加1(指向下一行)
if (Item.row > m_pGridCtrl->GetRowCount()-1)  //⑤當滿16行、16列的時候,顯示內存溢出,并結束Read
{
AfxMessageBox("Memory overflow");   // ⑥提示內存溢出
break;                              //⑦結束Read
}
}
............................
 }
當讀完第256筆數后,软件會依次執行以上代碼,可是當運行完⑤后,程序會自動跳到void CIntRamView::OnRcvData(),再次讀取buffer中的data,然後到第⑤步后繼續重複,當重複一段時間后才能執行到⑥、⑦操作(本應該在第一次執行完⑤后就繼續執行⑥、⑦操作,然後結束讀取)。
请问大家这是为什么啊,谢谢啦!

解决方案 »

  1.   

    此处的break是否相当于return?然后就继续调用此函数,然后遇到此条件不满足则继续执行……
      

  2.   

    下面是整个成员函数:
    void CIntRamView::OnRcvData()
    {
    CByteArray pucRcvData;
    pucRcvData.Copy(((CMainFrame*)AfxGetMainWnd())->m_RcvData);
    CString strTemp;
    strTemp.Format("%d",pucRcvData.GetSize());
    // AfxMessageBox(strTemp);
    //-----------------------------------------------------
    // Set Grid cell with data read from chip
    //-----------------------------------------------------
        GV_ITEM Item;
            Item.mask = GVIF_TEXT | GVIF_FORMAT;
            Item.row = nReadTimes*nNumRead/16+1;
           Item.col = nReadTimes*nNumRead%16+1;
         Item.nFormat = DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_CENTER;
    // AfxMessageBox("ready to write into grid box");
    for(int i = 0 ; i < nNumRead ; i++)
    {
    Item.strText.Format(_T("%2.2X"),pucRcvData.GetAt(i));
    CString strTemp; strTemp.Format(_T("Data received is %X"),(UCHAR)(pucRcvData.GetAt(i)));
    m_pGridCtrl->SetItem(&Item);
    // AfxMessageBox(strTemp); Item.col++;
    if (Item.col > m_pGridCtrl->GetColumnCount()-1)
    {
    Item.col = 1;
    Item.row ++;
    if (Item.row > m_pGridCtrl->GetRowCount()-1)
    {
    AfxMessageBox("Memory overflow");
    break;
    }
    }
    }
    // AfxMessageBox("Grid Cells has been set");
    nReadTimes ++;
    if (nReadTimes*nNumRead < 0xFF)        // nNumRead恒为1,nReadTimes初值为0
    {  
    //----------------------------------------
    // Modify start address for a new transfer
    //----------------------------------------
    // if massive mode
    arySDTask.SetAt(2,0);
    arySDTask.SetAt(3,nReadTimes*nNumRead);
    // AfxMessageBox("Modify start addr for a new transfer");
         ((CMainFrame*)AfxGetMainWnd())->SendData(arySDTask);
    /* m_nTimer = SetTimer(1,100000,NULL);
    if (!m_nTimer)
    AfxMessageBox("Set timer failed");
    */ }
    else
    {
    AfxMessageBox("Internal RAM read completed successfully, \n release serial port");
    ((CMainFrame*)AfxGetMainWnd())->fnRelControl(this, 4);  //release control 
    arySDTask.RemoveAll();
    nReadTimes = 0;
    Invalidate();          //updata data display
    //------------------------
    // Call to notify user that read process finished
    //------------------------
    if (((CICEApp*)AfxGetApp())->pOutputDoc != NULL)
    {
    CString *strDisplay = new CString;
    strDisplay->Format("INFO : Read data from Internal RAM was accomplished successfully");
    ((COutputDoc*)((CICEApp*)AfxGetApp())->pOutputDoc)->fnSetDisplay(strDisplay,TRUE);
    ((COutputDoc*)((CICEApp*)AfxGetApp())->pOutputDoc)->UpdateAllViews(NULL);
    }
    }
    }
    按理说 ,函数在执行完最后的Internal RAM read completed successfully后会结束程序执行的,可是程序最后会死掉,当我把“nReadTimes*nNumRead < 0xFF”给的很小的时候,比如“nReadTimes*nNumRead < 0x0F”的时候程序就不会死掉,这是为什么啊?谢谢啦
      

  3.   

    break是直接跳出for(int i = 0 ; i < nNumRead ; i++)循环的。
      

  4.   

    您说的跳到void CIntRamView::OnRcvData()是此函数又重新执行了吗?
      

  5.   

    帮你整理下代码void CIntRamView::OnRcvData() 

    CByteArray pucRcvData; 
    pucRcvData.Copy(((CMainFrame*)AfxGetMainWnd())->m_RcvData); 
    CString strTemp; 
    strTemp.Format("%d",pucRcvData.GetSize());  GV_ITEM Item; 
    Item.mask = GVIF_TEXT | GVIF_FORMAT; 
    Item.row = nReadTimes*nNumRead/16+1; 
    Item.col = nReadTimes*nNumRead%16+1; 
    Item.nFormat = DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_CENTER;  for(int i = 0 ; i < nNumRead ; i++) 

    Item.strText.Format(_T("%2.2X"),pucRcvData.GetAt(i)); 
    CString strTemp; 
    strTemp.Format(_T("Data received is %X"),(UCHAR)(pucRcvData.GetAt(i))); 
    m_pGridCtrl->SetItem(&Item);  Item.col++; 
    if (Item.col > m_pGridCtrl->GetColumnCount()-1) 

    Item.col = 1; 
    Item.row ++; 
    if (Item.row > m_pGridCtrl->GetRowCount()-1) //⑤

    AfxMessageBox("Memory overflow"); //⑥
    break;   //⑦

    }  }  nReadTimes ++; 
    if (nReadTimes*nNumRead < 0xFF)        // nNumRead恒为1,nReadTimes初值为0 
    {  
    arySDTask.SetAt(2,0); 
    arySDTask.SetAt(3,nReadTimes*nNumRead); 
    ((CMainFrame*)AfxGetMainWnd())->SendData(arySDTask); 

    else 

    AfxMessageBox("Internal RAM read completed successfully, \n release serial port"); 
    ((CMainFrame*)AfxGetMainWnd())->fnRelControl(this, 4);   
    arySDTask.RemoveAll(); 
    nReadTimes = 0; 
    Invalidate();         
    if (((CICEApp*)AfxGetApp())->pOutputDoc != NULL) 

    CString *strDisplay = new CString; 
    strDisplay->Format("INFO : Read data from Internal RAM was accomplished successfully"); 
    ((COutputDoc*)((CICEApp*)AfxGetApp())->pOutputDoc)->fnSetDisplay(strDisplay,TRUE); 
    ((COutputDoc*)((CICEApp*)AfxGetApp())->pOutputDoc)->UpdateAllViews(NULL); 


    } 向看到底有有没有执行哪一行,可以设置断点,再单步调试啊
      

  6.   

    不是重新执行,是重复执行,重复执行的时候
         Item.row = nReadTimes*nNumRead/16+1; 
        Item.col = nReadTimes*nNumRead%16+1; 
    他们的值都是16,所以我认为是重复执行了,重复执行的是从void CIntRamView::OnRcvData()开始到  if (Item.row > m_pGridCtrl->GetRowCount()-1) //⑤
     他俩之间的代码。
      

  7.   


    有循环啊:for(int i = 0 ; i < nNumRead ; i++)
      

  8.   

    上面的这部分代码在执行好多次以后(大概十几次,有时二十几次),然后会依次弹出好几次(5~10次)对话框“Memory overflow”,然后继续调试的时候就提示这样的对话框“Unhandled   exception   in   ICE.exe:0xC0000005:   Access   Violation. ”
      

  9.   

    somebady can help me!!!
      

  10.   

    你把这两行注掉试试
    Invalidate();          //updata data display ((COutputDoc*)((CICEApp*)AfxGetApp())->pOutputDoc)->UpdateAllViews(NULL);