接收函数中,当正确接收十六进制数据后,运行delete []mm;语句后直接会跳到
DWORD number=ReadComm(m_pOutbuffer,4096);去再读串口,导致程序出错。
代码如下:
LRESULT CMAIN:: OnCommNotify(WPARAM wParam,LPARAM lParam)
{
m_pOutbuffer=new unsigned char[4096];
memset(m_pOutbuffer, 0, 4096);
if(((wParam&EV_RXCHAR)!=EV_RXCHAR)||(!m_bConnected))
{
SetEvent(m_hPostMsgEvent);
return 0L;
}
else
{
DWORD number=ReadComm(m_pOutbuffer,4096);
int num=int(number);
char *mm=new char[num*2];
for(int i=0;i<num;i++)
{
int temp=(int)m_pOutbuffer[i];
int temph=temp/16;
int templ=temp%16;
if(temph>=0&&temph<10)
   mm[i*2]=temph+0x30;
else if(temph==10)
   mm[i*2]='A';
else if(temph==11)
   mm[i*2]='B';
else if(temph==12)
   mm[i*2]='C';
else if(temph==13)
   mm[i*2]='D';
else if(temph==14)
   mm[i*2]='E';
else if(temph==15)
   mm[i*2]='F'; if(templ>=0&&templ<10)
   mm[i*2+1]=templ+0x30;
else if(templ==10)
   mm[i*2+1]='A';
else if(templ==11)
   mm[i*2+1]='B';
else if(templ==12)
   mm[i*2+1]='C';
else if(templ==13)
   mm[i*2+1]='D';
else if(templ==14)
   mm[i*2+1]='E';
else if(templ==15)
   mm[i*2+1]='F';
}
mm[num*2]='\0';

str_receive=mm;
m_editSend.ReplaceSel(str_receive);

SetEvent(m_hPostMsgEvent);
delete []mm;
}
delete []m_pOutbuffer;
return 0L;
}