问题描述
- 这个程序0error 0warning,但是在执行的时候出现**dlg.exe已停止工作
-
// GT3Dlg.cpp : implementation file
//#include "stdafx.h"
#include "GT3.h"
#include "GT3Dlg.h"#include "GT400.H"
#ifdef DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE_;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CGT3Dlg dialogCGT3Dlg::CGT3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CGT3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGT3Dlg)
m_Axis_One_Check = TRUE;
m_Axis_Two_Check = FALSE;
m_One_Jerk = 0.00000002;
m_One_MaxAcc = 0.004;
m_One_Pos = -20000;
m_One_Vel = 5;
m_Two_Jerk = 0.00000002;
m_Two_MaxTwo = 0.004;
m_Two_Pos = -20000;
m_Two_Vel = 5;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CGT3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGT3Dlg)
DDX_Check(pDX, IDC_AXIS_ONE_CHECK, m_Axis_One_Check);
DDX_Check(pDX, IDC_AXIS_TWO_CHECK, m_Axis_Two_Check);
DDX_Text(pDX, IDC_S_ONE_JERK, m_One_Jerk);
DDV_MinMaxDouble(pDX, m_One_Jerk, 0., 1.e-002);
DDX_Text(pDX, IDC_S_ONE_MAXACC, m_One_MaxAcc);
DDV_MinMaxDouble(pDX, m_One_MaxAcc, 0., 0.5);
DDX_Text(pDX, IDC_S_ONE_POS, m_One_Pos);
DDX_Text(pDX, IDC_S_ONE_VEL, m_One_Vel);
DDV_MinMaxDouble(pDX, m_One_Vel, 0., 50.);
DDX_Text(pDX, IDC_S_TWO_JERK, m_Two_Jerk);
DDV_MinMaxDouble(pDX, m_Two_Jerk, 0., 1.e-002);
DDX_Text(pDX, IDC_S_TWO_MAXACC, m_Two_MaxTwo);
DDV_MinMaxDouble(pDX, m_Two_MaxTwo, 0., 0.5);
DDX_Text(pDX, IDC_S_TWO_POS, m_Two_Pos);
DDX_Text(pDX, IDC_S_TWO_VEL, m_Two_Vel);
DDV_MinMaxDouble(pDX, m_Two_Vel, 0., 50.);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CGT3Dlg, CDialog)
//{{AFX_MSG_MAP(CGT3Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_AXIS_OPEN, OnAxisOpen)
ON_BN_CLICKED(IDC_AXIS_SMOTION, OnAxisSmotion)
ON_BN_CLICKED(IDC_AXIS_STOP, OnAxisStop)
ON_BN_CLICKED(IDC_AXIS_CLOSE, OnAxisClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CGT3Dlg message handlersBOOL CGT3Dlg::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 return TRUE; // return TRUE unless you set the focus to a control
}
void CGT3Dlg::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 CGT3Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for paintingSendMessage(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 CGT3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CGT3Dlg::OnAxisOpen()
{
// TODO: Add your control notification handler code here////运动卡初始化函数////////////////////////////////////////////
short rtn;
rtn=GT_Open();
if (rtn!=0)
{
MessageBox("打开运动控制器设备失败!n 程序结束!","系统提示",MB_OK|MB_ICONERROR);
}
rtn=GT_Reset();
if (rtn!=0)
{
MessageBox("复位运动控制器失败!n 程序结束!","系统提示",MB_OK|MB_ICONERROR);
}
rtn=GT_SetSmplTm(200); error(rtn);
for (int i=1;i<5;++i)
{
rtn=GT_Axis(i);
rtn=GT_SetIntrMsk(0);
}////专用输入信号参数设置//////////////////////////////////////////
rtn=GT_LmtSns(0xff);
rtn=GT_EncSns(0);////控制轴初始化函数//////////////////////////////////////////////
rtn=GT_Axis(1); error(rtn); //1号轴
rtn=GT_ClrSts(); error(rtn);
rtn = GT_CtrlMode(0); error(rtn); //设置为输出模拟量
rtn=GT_CloseLp(); error(rtn);
rtn = GT_SetKp(1); error(rtn);
rtn = GT_SetKi(0); error(rtn);
rtn = GT_SetKd(0); error(rtn);
rtn=GT_SetKvff(0); error(rtn);
rtn=GT_SetKaff(0); error(rtn);
rtn=GT_Update(); error(rtn);
rtn=GT_AxisOn(); error(rtn);//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
rtn=GT_Axis(2); error(rtn); //2号轴初始化
rtn=GT_ClrSts(); error(rtn);
rtn = GT_CtrlMode(0); error(rtn);
rtn=GT_CloseLp(); error(rtn);
rtn = GT_SetKp(1); error(rtn);
rtn = GT_SetKi(0); error(rtn);
rtn = GT_SetKd(0); error(rtn);
rtn=GT_SetKvff(0); error(rtn);
rtn=GT_SetKaff(0); error(rtn);
rtn=GT_Update(); error(rtn);
rtn=GT_AxisOn(); error(rtn);
}void CGT3Dlg::OnAxisSmotion()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_Axis_One_Check&m_Axis_Two_Check)
{
short rtn;
UpdateData();
for(int i=1;i<3;i++)
{
rtn=GT_Axis(i);rtn=GT_ZeroPos();
rtn=GT_Update();
}
rtn=GT_Axis(1); error(rtn);
rtn=GT_PrflS(); error(rtn);
rtn=GT_SetJerk(m_One_Jerk); error(rtn);
rtn=GT_SetMAcc(m_One_MaxAcc); error(rtn);
rtn=GT_SetVel(m_One_Vel); error(rtn);rtn=GT_SetPos(m_One_Pos); error(rtn); rtn=GT_Axis(2); error(rtn); rtn=GT_PrflS(); error(rtn); rtn=GT_SetJerk(m_Two_Jerk); error(rtn); rtn=GT_SetMAcc(m_Two_MaxTwo); error(rtn); rtn=GT_SetVel(m_Two_Vel); error(rtn); rtn=GT_SetPos(m_Two_Pos); error(rtn); rtn=GT_MltiUpdt(0x3); error(rtn); } else { if (m_Axis_One_Check) { short rtn; UpdateData(); for(int i=1;i<3;i++) { rtn=GT_Axis(i); rtn=GT_ZeroPos(); rtn=GT_Update(); } rtn=GT_Axis(1); error(rtn); rtn=GT_PrflS(); error(rtn); rtn=GT_SetJerk(m_One_Jerk); error(rtn); rtn=GT_SetMAcc(m_One_MaxAcc); error(rtn); rtn=GT_SetVel(m_One_Vel); error(rtn); rtn=GT_SetPos(m_One_Pos); error(rtn); rtn=GT_Update(); error(rtn); } else { if (m_Axis_Two_Check) { short rtn; UpdateData(); for(int i=1;i<3;i++) { rtn=GT_Axis(i); rtn=GT_ZeroPos(); rtn=GT_Update(); } rtn=GT_Axis(2); error(rtn); rtn=GT_PrflS(); error(rtn); rtn=GT_SetJerk(m_Two_Jerk); error(rtn); rtn=GT_SetMAcc(m_Two_MaxTwo); error(rtn); rtn=GT_SetVel(m_Two_Vel); error(rtn); rtn=GT_SetPos(m_Two_Pos); error(rtn); rtn=GT_Update(); error(rtn); } else { MessageBox("您一根轴都没有选择!","系统提示"); } } }
}
void CGT3Dlg::OnAxisStop()
{
// TODO: Add your control notification handler code hereshort rtn; if (m_Axis_One_Check&m_Axis_Two_Check) { rtn=GT_Axis(1); error(rtn); rtn=GT_SmthStp(); error(rtn); rtn=GT_Axis(2); error(rtn); rtn=GT_SmthStp(); error(rtn); rtn=GT_MltiUpdt(0x3); error(rtn); } else { if (m_Axis_One_Check) { rtn=GT_Axis(1); error(rtn); rtn=GT_SmthStp(); error(rtn); rtn=GT_Update(); error(rtn); } else { if (m_Axis_Two_Check) { rtn=GT_Axis(2); error(rtn); rtn=GT_SmthStp(); error(rtn); rtn=GT_Update(); error(rtn); } else { MessageBox("您一根轴都没有选择!","系统提示"); } } }
}
void CGT3Dlg::OnAxisClose()
{
// TODO: Add your control notification handler code hereshort rtn; for(int i=1;i<3;i++) { rtn=GT_Axis(i); error(rtn); rtn=GT_AxisOff(); error(rtn); } rtn=GT_Close(); error(rtn);
}
void CGT3Dlg::OnOK()
{
// TODO: Add extra validation hereif (MessageBox("确定已关闭控制器了吗?","系统提示",MB_YESNO|MB_ICONQUESTION)==IDYES) { CDialog::OnOK(); }
}
void CGT3Dlg::error(short rtn)
{
switch (rtn)
{
case -1:
MessageBox("error:commuciation error!","系统提示",MB_ICONEXCLAMATION|MB_OK);
break;
case 0:
break;
case 1:
MessageBox("error:command error!","系统提示",MB_ICONEXCLAMATION|MB_OK);
break;
case 2:
case 3:
case 4:
case 5:
case 7:
MessageBox("error:parameter error!","系统提示",MB_ICONEXCLAMATION|MB_OK);
break;
case 6:
MessageBox("error:map error!","系统提示",MB_ICONEXCLAMATION|MB_OK);
default:
break;}
}
解决方案
这很正常,程序能编译不等于运行不出错
具体你应该调试程序。按f5而不是ctrl+f5启动程序,以调试模式运行,出错了会出现一个对话框,选择转到代码
然后找到发生错误的代码行,仔细调试,特别是指针的问题。
解决方案二:
出错的时候,直接用编译器调试跟踪一下,应该是你的代码有指针等没有处理好。导致程序异常了