Debug Assertion Failed! 求助
程序运行一小会就出现了这个问题。代码如下:
Debug Assertion Failed!
Program: ****.exe
File: dbgheap.c
line: 1044
Expression: _CrtIsValidHeapPoint(pUserData)
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts
调试堆栈是正在运行。
代码如下:
#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()
/////////////////////////////////////////////////////////////////////////////
// CTempDlg dialog
CTempDlg::CTempDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTempDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTempDlg)
m_strT1 = _T("");
m_strT10 = _T("");
m_strT2 = _T("");
m_strT3 = _T("");
m_strT4 = _T("");
m_strT5 = _T("");
m_strT6 = _T("");
m_strT7 = _T("");
m_strT8 = _T("");
m_strT9 = _T("");
m_strTime = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bAutoSend=FALSE;
m_bAutoTran=FALSE;
m_nCycleTime=1000;
}
void CTempDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTempDlg)
DDX_Control(pDX, IDC_BUTTON_Send, m_ctrButtonSend);
DDX_Control(pDX, IDC_EDIT_TIME, m_ctrTime);
DDX_Control(pDX, IDC_EDIT_T9, m_ctrT9);
DDX_Control(pDX, IDC_EDIT_T8, m_ctrT8);
DDX_Control(pDX, IDC_EDIT_T7, m_ctrT7);
DDX_Control(pDX, IDC_EDIT_T6, m_ctrT6);
DDX_Control(pDX, IDC_EDIT_T5, m_ctrT5);
DDX_Control(pDX, IDC_EDIT_T4, m_ctrT4);
DDX_Control(pDX, IDC_EDIT_T3, m_ctrT3);
DDX_Control(pDX, IDC_EDIT_T2, m_ctrT2);
DDX_Control(pDX, IDC_EDIT_T10, m_ctrT10);
DDX_Control(pDX, IDC_EDIT_T1, m_ctrT1);
DDX_Text(pDX, IDC_EDIT_T1, m_strT1);
DDX_Text(pDX, IDC_EDIT_T10, m_strT10);
DDX_Text(pDX, IDC_EDIT_T2, m_strT2);
DDX_Text(pDX, IDC_EDIT_T3, m_strT3);
DDX_Text(pDX, IDC_EDIT_T4, m_strT4);
DDX_Text(pDX, IDC_EDIT_T5, m_strT5);
DDX_Text(pDX, IDC_EDIT_T6, m_strT6);
DDX_Text(pDX, IDC_EDIT_T7, m_strT7);
DDX_Text(pDX, IDC_EDIT_T8, m_strT8);
DDX_Text(pDX, IDC_EDIT_T9, m_strT9);
DDX_Text(pDX, IDC_EDIT_TIME, m_strTime);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTempDlg, CDialog)
//{{AFX_MSG_MAP(CTempDlg)
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_Send, OnBUTTONSend)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_EDIT_TIME, OnChangeEditTime)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTempDlg message handlers
BOOL CTempDlg::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_nBaud=9600;//波特率
m_nCom=1;//串口号
m_cParity='N';//奇偶校验
m_nDatabits=8;//数据位
m_nStopbits=1;//停止位
m_dwCommEvents = EV_RXFLAG | EV_RXCHAR;
CString strStatus;
if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
{
m_Port.StartMonitoring();
//"当前状态:串口打开,无奇偶校验,8数据位,1停止位");
}
else
{
AfxMessageBox("没有发现此串口");
}
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TIME);
CString strText;
strText.Format("%d",m_nCycleTime);
pEdit->SetWindowText(strText);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTempDlg::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 CTempDlg::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 CTempDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LONG CTempDlg::OnCommunication(WPARAM ch, LPARAM port)
{
CString str;
str.Format("%02X ",ch);
m_strRCV+=str;
unsigned char dat[512];
int a[512];
int len1=Str2Hex(m_strRCV,dat);
int i=0,k=0,j=0,n,l;
//数据处理
int f[512],d[512];
n=len1/25;
for (l=0;l<n;l++)
{
for(j=0;j<25;j++)
{
d[j]=dat[j+(n-1)*25];
}
for (i=0;i<20;i++)
{
a[i]=d[i+3];
}
}
//显示转换的数据
m_strT1.Empty();
m_strT2.Empty();
m_strT3.Empty();
m_strT4.Empty();
m_strT5.Empty();
m_strT6.Empty();
m_strT7.Empty();
m_strT8.Empty();
m_strT9.Empty();
m_strT9.Empty();
m_strT10.Empty();
UpdateData(FALSE);
for (k=0;k<10;k++)
{
f[k]=a[2*k]*256+a[2*k+1];
f[k]=78*(f[k]-1450)/366+22;
}
CString str1;
str1.Format("%2d ",f[0]);
int nLen1=m_ctrT1.GetWindowTextLength();
m_ctrT1.SetSel(nLen1, nLen1);
m_ctrT1.ReplaceSel(str1);
nLen1=str1.GetLength();
m_strT1=str1;
CString str2;
str2.Format("%2d ",f[1]);
int nLen2=m_ctrT2.GetWindowTextLength();
m_ctrT2.SetSel(nLen2, nLen2);
m_ctrT2.ReplaceSel(str2);
nLen2=str2.GetLength();
m_strT2=str2;
CString str3;
str3.Format("%2d ",f[2]);
int nLen3=m_ctrT3.GetWindowTextLength();
m_ctrT3.SetSel(nLen3, nLen3);
m_ctrT3.ReplaceSel(str3);
nLen3=str3.GetLength();
m_strT3=str3;
CString str4;
str4.Format("%2d ",f[3]);
int nLen4=m_ctrT4.GetWindowTextLength();
m_ctrT4.SetSel(nLen4, nLen4);
m_ctrT4.ReplaceSel(str4);
nLen4=str4.GetLength();
m_strT4=str4;
CString str5;
str5.Format("%2d ",f[4]);
int nLen5=m_ctrT5.GetWindowTextLength();
m_ctrT5.SetSel(nLen5, nLen5);
m_ctrT5.ReplaceSel(str5);
nLen5=str5.GetLength();
m_strT5=str5;
CString str6;
str6.Format("%2d ",f[5]);
int nLen6=m_ctrT6.GetWindowTextLength();
m_ctrT6.SetSel(nLen6, nLen6);
m_ctrT6.ReplaceSel(str6);
nLen6=str6.GetLength();
m_strT6=str6;
CString str7;
str7.Format("%2d ",f[6]);
int nLen7=m_ctrT7.GetWindowTextLength();
m_ctrT7.SetSel(nLen7, nLen7);
m_ctrT7.ReplaceSel(str7);
nLen7=str7.GetLength();
m_strT7=str7;
CString str8;
str8.Format("%2d ",f[7]);
int nLen8=m_ctrT8.GetWindowTextLength();
m_ctrT8.SetSel(nLen8, nLen8);
m_ctrT8.ReplaceSel(str8);
nLen8=str8.GetLength();
m_strT8=str8;
CString str9;
str9.Format("%2d ",f[8]);
int nLen9=m_ctrT9.GetWindowTextLength();
m_ctrT9.SetSel(nLen9, nLen9);
m_ctrT9.ReplaceSel(str9);
nLen9=str9.GetLength();
m_strT9=str9;
CString str10;
str10.Format("%2d ",f[9]);
int nLen10=m_ctrT10.GetWindowTextLength();
m_ctrT10.SetSel(nLen10, nLen10);
m_ctrT10.ReplaceSel(str10);
nLen10=str10.GetLength();
m_strT10=str10;
return 0;
}
unsigned char CTempDlg::HexChar(unsigned char c)
{
if((c>='0')&&(c<='9'))
return c-0x30;
else if((c>='A')&&(c<='F'))
return c-'A'+10;
else if((c>='a')&&(c<='f'))
return c-'a'+10;
else
return 0x10;
}
int CTempDlg::Str2Hex(CString str,unsigned char* data)
{
int t,t1;
int rlen=0,len=str.GetLength();
//data.SetSize(len/2);
for(int i=0;i<len;)
{
char l,h=str[i];
if(h==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
l=str[i];
t=HexChar(h);
t1=HexChar(l);
if((t==16)||(t1==16))
break;
else
t=t*16+t1;
i++;
data[rlen]=(char)t;
rlen++;
}
return rlen;
}
void CTempDlg::OnBUTTONSend()
{
m_bAutoSend=!m_bAutoSend;//发送标志
if(m_bAutoSend)
m_ctrButtonSend.SetWindowText("发送");
else
m_ctrButtonSend.SetWindowText("停止发送");
if(m_bAutoSend)//判断是否自动发送
{
SetTimer(1,m_nCycleTime,NULL);
}
else
{
KillTimer(1);
}
}
void CTempDlg::send()
{
UpdateData(TRUE);
CString str;
str="01 04 00 00 00 0a 70 0d";//发送数据
unsigned char data[512];
int len=Str2Hex(str,data);
m_Port.WriteToPort(data,len);
}
void CTempDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1:
send();
break;
}
CDialog::OnTimer(nIDEvent);
}
void CTempDlg::OnChangeEditTime()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TIME);
CString strText;
pEdit->GetWindowText(strText);
m_nCycleTime=atoi(strText);
}