这两周配合同事徐军搞了个FPGA测试软件。在测试过程中出现了几个问题,现在终于解决了,现在总结如下:
1、char *pTemp=new char[800];
然后是一些关于pTemp的操作,例如
for(int i=0;i<800;i++)
{
......;
pTemp++;
}
然后我用delete pTemp;程序运行就出现错误了,经过调试发现,应该char *pBuf; pBuf=pTemp;然后delete pBuf;就可以了。这样既不会产生内存泄漏,又不会删除空的内存。
2、软件定时刷新显示,我将从单片机要来的数据显示在CRichEditCtrl控件上面,代码如下:
CHARFORMAT CTestView::GetCharFormat(DWORD dwMask)
{
CHARFORMAT cf;
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = dwMask;
m_richEdit.GetSelectionCharFormat(cf);
return cf;
}
void CTestView::showData(CString szData,COLORREF crColorData)
{
int len;
CHARFORMAT cf = GetCharFormat(1); //初始化
if (cf.dwEffects & CFE_AUTOCOLOR)
cf.dwEffects -= CFE_AUTOCOLOR;
cf.dwMask= CFM_COLOR;
//写入数据太多时,richedit控件不能正确显示颜色,所以分段显示
int count = szData.GetLength()/5000;
long Base = 0;
for(int i=0;i<count;i++)
{
cf.crTextColor = crColorData;
m_richEdit.SetSelectionCharFormat(cf);
len = m_richEdit.GetWindowTextLength();
m_richEdit.SetSel(len,len);
m_richEdit.ReplaceSel(szData.Mid(Base,szData.GetLength()/count));
Base += szData.GetLength()/count;
}
cf.crTextColor = crColorData;
m_richEdit.SetSelectionCharFormat(cf);
len = m_richEdit.GetWindowTextLength();
m_richEdit.SetSel(len,len);
m_richEdit.ReplaceSel(szData.Mid(Base));
//m_richEdit.LineScroll(m_richEdit.GetLineCount());
m_richEdit.SetModify(FALSE);
}
void CTestView::showString(CString szTitle,COLORREF crColorTitle,
CString szData,COLORREF crColorData)
{
CString szText="";
CHARFORMAT cf= GetCharFormat(1);
if (cf.dwEffects & CFE_AUTOCOLOR)
cf.dwEffects -= CFE_AUTOCOLOR;
cf.dwMask = CFM_COLOR;
cf.crTextColor = crColorTitle;
m_richEdit.SetSelectionCharFormat(cf);
int len = m_richEdit.GetWindowTextLength();
m_richEdit.SetSel(len,len);
m_richEdit.ReplaceSel(szTitle);
m_richEdit.LineScroll(1);
m_richEdit.SetModify(FALSE);
cf.crTextColor = crColorData;
m_richEdit.SetSelectionCharFormat(cf);
len = m_richEdit.GetWindowTextLength();
m_richEdit.SetSel(len,len);
m_richEdit.ReplaceSel(szData);
m_richEdit.LineScroll(1);
m_richEdit.SetModify(FALSE);
cf.crTextColor = RGB(0,0,255);
m_richEdit.SetSelectionCharFormat(cf);
len = m_richEdit.GetWindowTextLength();
m_richEdit.SetSel(len,len);
m_richEdit.ReplaceSel(szText);
m_richEdit.LineScroll(1);
m_richEdit.SetModify(FALSE);
}
在OnInitialUpdate()函数中,我初始化m_richEdit如下:
//初始化m_richEdit
m_richEdit.Create(WS_VSCROLL|WS_CHILD|WS_VISIBLE|ES_AUTOVSCROLL|ES_MULTILINE|ES_WANTRETURN,
CRect(10,34,486,380), this, 1);
m_richEdit.ModifyStyleEx(1, WS_EX_CLIENTEDGE);
m_richEdit.SetReadOnly();
在定时显示里面,我调用showData()函数:
row=absoluteAddr;
while(*pTemp!='\0')
{
strtemp=pTemp;
for(i=0;i<48;i++)
pTemp++;
//显示行号
str.Format("%04x",row);
row += 0x10;
str += " ";
showData(str,RGB(128,128,0));
//显示前八个字节
str=strtemp.Left(24);
str += " ";
showData(str,RGB(0,0,255));
//显示后八个字节
str=strtemp.Mid(24,24);
showData(str,RGB(0,0,255));
//换行
str="\r\n";
showData(str,RGB(0,0,255));
}
后来发现一个问题,当显示行号为000f时候,即最后一行的时候总是回比前面的多往前缩进一个空格,考虑到最后一行还多加了一个回车换行,我把代码该成如下:
for(row=absoluteAddr;row<absoluteAddr+0x0F*0x10;row += 0x10)
{
strtemp=pTemp;
for(i=0;i<48;i++)
pTemp++;
//显示行号
str.Format("%04x",row);
str += " ";
showData(str,RGB(128,128,0));
//显示前八个字节
str=strtemp.Left(24);
str += " ";
showData(str,RGB(0,0,255));
//显示后八个字节
str=strtemp.Mid(24,24);
showData(str,RGB(0,0,255));
//换行
str="\r\n";
showData(str,RGB(0,0,255));
}
str.Format("%04x",row);
str += " ";
showData(str,RGB(128,128,0));
strtemp=pTemp;
//显示前八个字节
str=strtemp.Left(24);
str += " ";
showData(str,RGB(0,0,255));
//显示后八个字节
str=strtemp.Mid(24,24);
showData(str,RGB(0,0,255));