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

📄 mrobot3demodlg.cpp

📁 使用VC写的关于windows和linux之间利用套接字实现通讯的源代码
💻 CPP
字号:
// MRobot3DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MRobot3Demo.h"
#include "MRobot3DemoDlg.h"

#include "m6e_api.h"//the header file of Mrobot should be included 

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <afxsock.h>

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMRobot3DemoDlg dialog

CMRobot3DemoDlg::CMRobot3DemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMRobot3DemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMRobot3DemoDlg)
	m_ModulesNum = 0;
	m_DeviceID=-1;
	m_Info = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	memset(m_aiIdMap,0,20*sizeof(int));
}

void CMRobot3DemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMRobot3DemoDlg)
	DDX_Text(pDX, IDC_EDIT1, m_ModulesNum);
	DDX_Text(pDX, IDC_EDIT2, m_Info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMRobot3DemoDlg, CDialog)
	//{{AFX_MSG_MAP(CMRobot3DemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_HOME, OnHome)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_RUN, OnRun)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_YUANKONG, OnYuankong)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMRobot3DemoDlg message handlers

BOOL CMRobot3DemoDlg::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
	AfxSocketInit(NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMRobot3DemoDlg::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 CMRobot3DemoDlg::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 CMRobot3DemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMRobot3DemoDlg::OnHome() 
{
	int ret;
	CString	m_strInitString;
	if(m_DeviceID == -1)
	{
		m_strInitString =_T("test");//intializtion string
		ret = RobotE_openDevice( &m_DeviceID, m_strInitString.GetBuffer(0) );//open the CAN 
		
		if(m_DeviceID < 0)
		{
			AfxMessageBox("No device found!");
			return;
		}
	}

	m_ModulesNum = RobotE_getModuleCount( m_DeviceID );

	if(m_ModulesNum <= 0)
	{
		AfxMessageBox("No module found!");
		return;
	}

	RobotE_getModuleIdMap(m_DeviceID,m_aiIdMap);
	RobotE_homeAll( m_DeviceID);  //home all modules
	RobotE_waitForHomeEndAll(m_DeviceID,10000);
	m_Info="Ready to work";
	UpdateData(FALSE);
	//GetDlgItem(IDC_HOME)->EnableWindow(FALSE);
}

void CMRobot3DemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	RobotE_closeDevice( m_DeviceID );
}

void CMRobot3DemoDlg::OnRun() 
{
	if(m_DeviceID < 0)
	{
		AfxMessageBox("No device found!");
		return;
	}

	if(m_ModulesNum <= 0)
	{
		AfxMessageBox("No module found!");
		return;
	}

	m_Info="Running";
	UpdateData(FALSE);
	GetDlgItem(IDC_RUN)->EnableWindow(FALSE);

	AfxBeginThread(&CMRobot3DemoDlg::Motion,this,THREAD_PRIORITY_NORMAL);
}

void CMRobot3DemoDlg::OnPause() 
{
	RobotE_haltAll( m_DeviceID );	
}

unsigned int CMRobot3DemoDlg::Motion(LPVOID pParam)
{
	CMRobot3DemoDlg *pDlg = (CMRobot3DemoDlg *)pParam;
	int iDeviceId = pDlg->m_DeviceID;

	//rotary module 1
	RobotE_setRampAcc( iDeviceId, pDlg->m_aiIdMap[0], 0.1f); //set accelaration
    RobotE_moveStep( iDeviceId, pDlg->m_aiIdMap[0], 0.8f, 6000);//module 1 positions to (0.8*180/Pi) degree during 6000ms
	Sleep(6000);

	//rotary module 2
	RobotE_setRampAcc( iDeviceId, pDlg->m_aiIdMap[1], 0.1f); //set accelaration
    RobotE_moveStep( iDeviceId, pDlg->m_aiIdMap[1], 0.8f, 6000);//module 2 positions to  (0.8*180/Pi) degree during 6000ms
	Sleep(6000);

    //rotary module 3
	RobotE_setRampAcc( iDeviceId, pDlg->m_aiIdMap[2], 0.1f); //set accelaration
    RobotE_moveStep( iDeviceId, pDlg->m_aiIdMap[2], 0.8f, 6000);//module 3 positions to  (0.8*180/Pi) degree during 6000ms
	Sleep(6000);

	//gripper
	RobotE_setRampAcc( iDeviceId, pDlg->m_aiIdMap[3], 0.1f); //set accelaration
    RobotE_moveStep( iDeviceId, pDlg->m_aiIdMap[3], 0.065f, 6000);//module 4 positions to 0.065m during 6000ms
	Sleep(6000);

	pDlg->GetDlgItem(IDC_RUN)->EnableWindow(TRUE);

	return 0;
}

void CMRobot3DemoDlg::OnYuankong() 
{
	// TODO: Add your control notification handler code here
	WORD wVersionRequested;
        WSADATA wsaData;
        char name[255];
        CString ip;
        PHOSTENT hostinfo;
        wVersionRequested = MAKEWORD( 2, 2 );

        if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
        {

              if( gethostname ( name, sizeof(name)) == 0)
              {
                    if((hostinfo = gethostbyname(name)) != NULL)
                    {
                          ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
                    }
              }
            
              WSACleanup( );
        } 
        
        GetDlgItem(IDC_IPADDRESS1)->SetWindowText(ip);
	
}

⌨️ 快捷键说明

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