www.pudn.com > VC6.0.rar > Demo01Dlg.cpp, change:2004-03-17,size:7478b


// Demo01Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Demo01.h" 
#include "Demo01Dlg.h" 
# include	"dmc1000.h"		//包含库头文件 
//定义四个轴的常量 
const int XCH = 0; 
const int YCH = 1; 
const int ZCH = 2; 
const int UCH = 3; 
/******************************************** 
	雷赛DMC1000控制卡编程示例之一 
 
?-主要演示功能: 
	1. 控制卡的基本操作 
	2. 单轴驱动脉冲,驱动模式 
	3. T形和S形驱动方式选择 
	4. 减速或急停控制,外部减速停 
	5. 位置显示,位置清零及指定轴状态显示 
	6. 定量驱动和持续驱动选择 
 
!-关键函数: 
	1. 控制卡的基本操作函数 
		d1000_board_init, d1000_board_close, d1000_get_board_base 
		d1000_get_irq_channel 
 
	2. 单轴驱动,T形及S形选择 
		d1000_set_pls_outmode, d1000_set_pls_logic 
		d1000_start_tv_move		持续驱动 
		d1000_start_sv_move 
		d1000_start_tr_move		定量驱动 
		d1000_start_sr_move 
 
	3. 减速或急停 
		d1000_decel_stop	当减速时间为0时为急停 
		d1000_set_sd_enable 
 
	4. 位置显示及状态显示 
		d1000_get_abs_position 
		d1000_set_position 
		d1000_check_done 
		d1000_get_axis_status 
 
#-注意事项: 
	1. 本例虽然只提供了四个驱动函数,实质上其它驱动函数都一致 
 
 
	Writen by LEISAI-SUPPORT 
	FAX: 0755 - 26402718 
	TEL: 0755 - 26434329 
	EMAIL: support@leisai.com 
	HTTP: www.leisai.com	 
********************************************/ 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo01Dlg dialog 
 
CDemo01Dlg::CDemo01Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CDemo01Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDemo01Dlg) 
	m_nAxisST	= 0; 
	m_nSpeedST	= 0; 
	m_nMoveST	= 0; 
	m_nSpeed	= 3200; 
	m_fAccel	= 0.1; 
	m_bPMode	= TRUE; 
	m_bPLogic	= TRUE; 
	m_bDecel	= TRUE; 
	m_nStart	= 1600; 
	m_bDir		= TRUE; 
	m_nPulse	= 6400; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CDemo01Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDemo01Dlg) 
	DDX_Radio(pDX, IDC_RADIO_AXISST, m_nAxisST); 
	DDX_Radio(pDX, IDC_RADIO_SPEEDST, m_nSpeedST); 
	DDX_Radio(pDX, IDC_RADIO_MOVEST, m_nMoveST); 
	DDX_Text(pDX, IDC_EDIT_SPEED, m_nSpeed); 
	DDX_Text(pDX, IDC_EDIT_ACCEL, m_fAccel); 
	DDX_Check(pDX, IDC_CHECK_PULSEMODE, m_bPMode); 
	DDX_Check(pDX, IDC_CHECK_PULSELOGIC, m_bPLogic); 
	DDX_Check(pDX, IDC_CHECK_DECEL, m_bDecel); 
	DDX_Text(pDX, IDC_EDIT_START, m_nStart); 
	DDX_Check(pDX, IDC_CHECK_DIR, m_bDir); 
	DDX_Text(pDX, IDC_EDIT_PULSE, m_nPulse); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CDemo01Dlg, CDialog) 
	//{{AFX_MSG_MAP(CDemo01Dlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_BN_CLICKED(IDC_BUTTON_CLSPOSI, OnButtonClsposi) 
	ON_BN_CLICKED(IDC_CHECK_PULSEMODE, OnCheckPulsemode) 
	ON_BN_CLICKED(IDC_CHECK_PULSELOGIC, OnCheckPulselogic) 
	ON_BN_CLICKED(IDC_CHECK_DIR, OnCheckDir) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo01Dlg message handlers 
 
BOOL CDemo01Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// 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 
	 
	int nCard(0); 
	nCard = d1000_board_init(); 
	if( nCard <= 0 )//控制卡初始化 
		MessageBox("未找到控制卡!","警告"); 
	else{ 
		CString string("地址: 0x0000"); 
	//	string.Format("地址:0x%04X", (long)d1000_get_base_addr( 0 ) ); 
		GetDlgItem( IDC_STATIC_ADDRESS )->SetWindowText( string ); 
 
		string.Format("中断号:0x%02x", d1000_get_irq_channel( 0 )); 
		GetDlgItem( IDC_STATIC_IRQ )->SetWindowText( string ); 
	} 
	 
	SetTimer( IDC_TIMER, 100, NULL ); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// 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 CDemo01Dlg::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 CDemo01Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CDemo01Dlg::OnTimer(UINT nIDEvent)  
{ 
	static CString string ; 
	string.Format("状态:0x%02x,0x%02x,0x%02x,0x%02x # 位置:%ld,%ld,%ld,%ld", 
				d1000_get_axis_status( XCH ), 
				d1000_get_axis_status( YCH ), 
				d1000_get_axis_status( ZCH ), 
				d1000_get_axis_status( UCH ), 
				long( d1000_get_command_pos( XCH )), 
				long( d1000_get_command_pos( YCH )), 
				long( d1000_get_command_pos( ZCH )), 
				long( d1000_get_command_pos( UCH )) ); 
 
	GetDlgItem( IDC_STATIC_STAPOS )->SetWindowText( string ); 
 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CDemo01Dlg::OnDestroy()  
{ 
	KillTimer( IDC_TIMER );	//在关闭控制卡之前先关闭定时器 
	d1000_board_close();	//关闭控制卡(此句一定要有)  
 
	CDialog::OnDestroy(); 
	 
	 
} 
 
void CDemo01Dlg::OnOK()  
{//开始 
 
	if( !UpdateData( TRUE ) ) return; 
	if( d1000_check_done( m_nAxisST ) == 0 ) return;//若在运行,则返回 
 
	//设定参数 
	d1000_set_pls_outmode( m_nAxisST, m_bPMode?1:0); 
//	d1000_set_pls_logic( m_nAxisST, m_bPLogic?1:0); 
//	d1000_set_sd_enable( m_nAxisST, m_bDecel?1:0); 
 
	int dir = m_bDir?1:-1; 
 
	if( m_nMoveST == 0 ) 
	{//定量 
		m_nSpeedST == 0 ?//S形 
			d1000_start_s_move( m_nAxisST, m_nPulse*dir, m_nStart, m_nSpeed, m_fAccel ): 
			d1000_start_t_move( m_nAxisST, m_nPulse*dir, m_nStart, m_nSpeed, m_fAccel ); 
	} 
	else 
	{//持续 
		m_nSpeedST == 0 ?//S形 
			d1000_start_sv_move( m_nAxisST,	m_nStart, m_nSpeed*(m_bDir?1:-1), m_fAccel ): 
			d1000_start_tv_move( m_nAxisST, m_nStart, m_nSpeed*(m_bDir?1:-1), m_fAccel ); 
				 
	} 
//	CDialog::OnOK(); 
} 
 
void CDemo01Dlg::OnCancel()  
{ 
	if(   d1000_check_done( XCH ) == 0 //在运行 
		||d1000_check_done( YCH ) == 0  
		||d1000_check_done( ZCH ) == 0  
		||d1000_check_done( UCH ) == 0 ) 
	{//当减速时间为0时,为急停效果 
		d1000_decel_stop( XCH ); 
		d1000_decel_stop( YCH ); 
		d1000_decel_stop( ZCH ); 
		d1000_decel_stop( UCH ); 
		return; 
	} 
	CDialog::OnCancel(); 
} 
 
void CDemo01Dlg::OnButtonClsposi()  
{//位置清零 
	for( int i(0); i<4; i++) 
		d1000_set_command_pos( i, 0 ); 
} 
 
void CDemo01Dlg::UpdateControl() 
{ 
	GetDlgItem( IDC_CHECK_PULSEMODE )->SetWindowText( m_bPMode ?"脉冲+方向":"脉冲+脉冲");	 
	GetDlgItem( IDC_CHECK_PULSELOGIC )->SetWindowText( m_bPLogic?"脉冲逻辑:正":"脉冲逻辑:负"); 
	GetDlgItem( IDC_CHECK_DIR )->SetWindowText( m_bDir?"驱动方向:正":"驱动方向:反"); 
} 
 
void CDemo01Dlg::OnCheckPulsemode()  
{ 
	UpdateData( TRUE ); 
	UpdateControl();	 
} 
 
void CDemo01Dlg::OnCheckPulselogic()  
{ 
	UpdateData( TRUE ); 
	UpdateControl();	 
	 
} 
 
void CDemo01Dlg::OnCheckDir()  
{ 
	UpdateData( TRUE ); 
	UpdateControl();	 
	 
}