⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfcjogdlg.cpp

📁 介绍控制器运动的例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MFCJogDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCJog.h"
#include "MFCJogDlg.h"
#include "ReadStatus.h" // **ADDED** The Event Class
#include <bitset>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMFCJogDlg dialog

CMFCJogDlg::CMFCJogDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCJogDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFCJogDlg)
	m_Transport = 2;
	m_BusType = FALSE;
	m_lblConnected = _T("Disconnected");
	m_Ver = _T("v?.?.?.?");
	m_BPS = 38400;
	m_CardNbr = 0;
	m_IP = _T("172.26.128.152");
	m_Port = 1;
	m_Master = 0;
	m_Axis = 0;
	m_24vEnable = -1;
	m_KAM = -1;
	m_Motion = -1;
	m_Drive =-1;
	m_KAMR = -1;
	m_JogOffset = 0;
	m_SetPoint = 0;
	m_CurrAxis = _T("");
	m_JActive = -1;
	m_JSpeed = -1;
	m_JStop = -1;
	m_JLimitsON = FALSE;
	m_JLockoutON = FALSE;
	m_JogLimit1 = 0.0f;
	m_JogLimit2 = 0.0f;
	m_JogVel = 0.0f;
	m_JogJrk = 0.0f;
	m_JogDec = 0.0f;
	m_JogAcc = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

  // **ADDED** 
  // Create Interfaces using Prog ID
  m_Cntl = new IControl;
  m_Cntl->CreateDispatch(_T("BoxBridge.Control.1"));
  m_Stat = new IStatus;
  m_Stat->CreateDispatch(_T("BoxBridge.Status.1"));

  // Initialize working data
  m_workerEvent = NULL;
  m_StopWork = true;
  m_axisMsgId = 0; 
  m_masterMsgId = 0;
  // **ADDED**
}

// **ADDED** 
CMFCJogDlg::~CMFCJogDlg()
{
  // Need to Clean up COM
  if(m_Cntl){
    m_Cntl->Disconnect();
    delete m_Cntl;
  }
  if(m_Stat){
    m_Stat->Disconnect();
    delete m_Stat;
  }

  m_StopWork = true;
  if(m_workerEvent){
    delete m_workerEvent;
  } 
  // CoUninitialize();       // Clean up of COM done automatically by MFC
}
// **ADDED** 

void CMFCJogDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCJogDlg)
	DDX_Radio(pDX, IDC_RDO_TRANSPORT, m_Transport);
	DDX_Check(pDX, IDC_CHK_PCI, m_BusType);
	DDX_Text(pDX, IDC_LBL_CONNECT, m_lblConnected);
	DDX_Text(pDX, IDC_LBL_VER, m_Ver);
	DDX_Text(pDX, IDC_TXT_BPS, m_BPS);
	DDX_Text(pDX, IDC_TXT_CARDNBR, m_CardNbr);
	DDX_Text(pDX, IDC_TXT_IP, m_IP);
	DDX_Text(pDX, IDC_TXT_PORT, m_Port);
	DDX_CBIndex(pDX, IDC_CBO_MASTER, m_Master);
	DDX_CBIndex(pDX, IDC_CBO_AXIS, m_Axis);
	DDX_Radio(pDX, IDC_LED_24VENABLE, m_24vEnable);
	DDX_Radio(pDX, IDC_LED_KAM, m_KAM);
	DDX_Radio(pDX, IDC_LED_MOTION, m_Motion);
	DDX_Radio(pDX, IDC_LED_DRIVE, m_Drive);
	DDX_Radio(pDX, IDC_LED_KAMR, m_KAMR);
	DDX_Text(pDX, IDC_TXT_JOGOFFSET, m_JogOffset);
	DDX_Text(pDX, IDC_TXT_SETPOINT, m_SetPoint);
	DDX_Text(pDX, IDC_LBL_AXIS, m_CurrAxis);
	DDX_Radio(pDX, IDC_LED_JACTIVE, m_JActive);
	DDX_Radio(pDX, IDC_LED_JSPEED, m_JSpeed);
	DDX_Radio(pDX, IDC_LED_JSTOP, m_JStop);
	DDX_Check(pDX, IDC_JLIMITS, m_JLimitsON);
	DDX_Check(pDX, IDC_JLOCKOUT, m_JLockoutON);
	DDX_Text(pDX, IDC_TXT_JOGLIMIT1, m_JogLimit1);
	DDX_Text(pDX, IDC_TXT_JOGLIMIT2, m_JogLimit2);
	DDX_Text(pDX, IDC_TXT_JOGVEL, m_JogVel);
	DDX_Text(pDX, IDC_TXT_JOGJRK, m_JogJrk);
	DDX_Text(pDX, IDC_TXT_JOGDEC, m_JogDec);
	DDX_Text(pDX, IDC_TXT_JOGACC, m_JogAcc);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMFCJogDlg, CDialog)
	//{{AFX_MSG_MAP(CMFCJogDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
	ON_BN_CLICKED(IDC_LED_24VENABLE, OnLed24venable)
	ON_BN_CLICKED(IDC_LED_KAM, OnLedKam)
	ON_BN_CLICKED(IDC_LED_MOTION, OnLedMotion)
	ON_BN_CLICKED(IDC_LED_KAMR, OnLedKamr)
	ON_BN_CLICKED(IDC_LED_DRIVE, OnLedDrive)
	ON_BN_CLICKED(IDC_LED_JSTOP, OnLedJstop)
	ON_BN_CLICKED(IDC_LED_JSPEED, OnLedJspeed)
	ON_BN_CLICKED(IDC_LED_JACTIVE, OnLedJactive)
	ON_BN_CLICKED(IDC_UPD_JLIMITS, OnUpdJlimits)
	ON_CBN_SELCHANGE(IDC_CBO_AXIS, OnSelchangeCboAxis)
	ON_CBN_SELCHANGE(IDC_CBO_MASTER, OnSelchangeCboMaster)
	ON_BN_CLICKED(IDC_JOG_STOP, OnJogStop)
	ON_BN_CLICKED(IDC_JOG_POS, OnJogPos)
	ON_BN_CLICKED(IDC_JOG_NEG, OnJogNeg)
	//}}AFX_MSG_MAP
  ON_MESSAGE(WMU_UpdateStats,OnUpdateStats)   // **ADDED**
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCJogDlg message handlers

BOOL CMFCJogDlg::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
	
	// TODO: Add extra initialization here
	
	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 CMFCJogDlg::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 CMFCJogDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// **ADDED** 
// Code behind buttons
//
void CMFCJogDlg::OnBtnConnect() 
{
  try{
  //******************
  // Connect to the Device on all interfaces
  UpdateData();

  m_Ver = CString("v") + m_Cntl->GetBstrVersion();  // Version

  m_Cntl->SetNBPS(m_BPS);               // Set BPS
  m_Cntl->SetNPort(m_Port);             // Set Port
  m_Cntl->SetBstrIP(m_IP);              // Set IP Address
  m_Cntl->SetNBus(m_BusType?1:0);       // Set Bus, checked(true) is ISA, else PCI
  m_Cntl->Connect(m_Transport,m_CardNbr);//Connect to Transport
  m_CardNbr = m_Cntl->GetNCard();       // One way to use USB is to grab the first unit, but then need to directly connect after that

  m_Stat->SetNStatusWaitRate(7);        // Minimum time between status events
  m_Stat->SetNBPS(m_BPS);               // Set BPS
  m_Stat->SetNPort(m_Port);             // Set Port
  m_Stat->SetBstrIP(m_IP);              // Set IP Address
  m_Stat->SetNBus(m_BusType?1:0);       // Set Bus, checked(true) is ISA, else PCI
  m_Stat->Connect(m_Transport,m_CardNbr);//Connect to Transport

  if(!(m_Cntl->GetIsOffline() || m_Stat->GetIsOffline())) // Verify Connection
    m_lblConnected = _T("Connected");

  UpdateData(FALSE);

  // Turn on event handler
  m_workerEvent = new ReadStatus(this);

  // Populate current Jog Settings
  OnSelchangeCboMaster();
  OnSelchangeCboAxis();

  // *** 
  // Error Handling should go everywhere a call to Communications Server is made 
  // In this sample it is only placed here to aid readablility.
  // ***
  // Error Handling - the errors specifically thrown by Communications Server
  // show up as COM exceptions and can be caught with the COleDispatchException class.
  }catch(COleDispatchException* e) {
    ostringstream errmsg;
    unsigned short errnbr = e->m_scError;
    errmsg << "ERROR NO: " << hex << e->m_scError << dec << "(" << errnbr << ")\nERROR DESC: " << string(e->m_strDescription) << "\nERROR INTERFACE: " << string(e->m_strSource);
    MessageBox(errmsg.str().c_str(), "COM Error", MB_OK);
	}
	catch(CException *e) { 
		TCHAR szCause[1024];
		e->GetErrorMessage(szCause, 1024);   
    MessageBox(szCause, "non-COM Error", MB_OK);
	}
} 

void CMFCJogDlg::OnUpdJlimits() 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -