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

📄 com232testdlg.cpp

📁 机器人控制的串口通信调试程序,包含角度
💻 CPP
字号:
// Com232TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Com232Test.h"
#include "Com232TestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCom232TestDlg dialog

CCom232TestDlg::CCom232TestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCom232TestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCom232TestDlg)
	m_strRXData = _T("");
	m_strTXData = _T("");
	m_motor = 0;
	m_servo = 0;
	m_motor_ascii = _T("");
	m_servo_ascii = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCom232TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCom232TestDlg)
	DDX_Control(pDX, IDC_SLIDER2, m_Servo);
	DDX_Control(pDX, IDC_SLIDER1, m_Motor);
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Text(pDX, IDC_EDIT_RXDATA, m_strRXData);
	DDX_Text(pDX, IDC_EDIT_TXDATA, m_strTXData);
	DDX_Slider(pDX, IDC_SLIDER1, m_motor);
	DDX_Slider(pDX, IDC_SLIDER2, m_servo);
	DDX_Text(pDX, IDC_STATIC_MOTOR, m_motor_ascii);
	DDX_Text(pDX, IDC_STATIC_SERVO, m_servo_ascii);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCom232TestDlg, CDialog)
	//{{AFX_MSG_MAP(CCom232TestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnCustomdrawSlider2)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCom232TestDlg message handlers

BOOL CCom232TestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.
	m_Servo.SetRange(0,127);
	m_Servo.SetTic(64);
	m_Servo.SetPos(64);
	
	m_Motor.SetRange(128,255);
	m_Motor.SetTic(192);
	m_Motor.SetPos(192);

	if(m_comm.GetPortOpen())
		m_comm.SetPortOpen(FALSE);


	m_comm.SetCommPort(1);                                 //选择com1
	if( !m_comm.GetPortOpen())
		m_comm.SetPortOpen(TRUE);                          //打开串口
	else
		AfxMessageBox("cannot open serial port");


	m_comm.SetSettings("9600,n,8,1");                        //波特率9600,无校验,8个数据位,1个停止位 

	m_comm. SetInputMode(1);                          //1:表示以二进制方式检取数据
	m_comm.SetRThreshold(1); 
                                                      //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
	m_comm.SetInputLen(0);                            //设置当前接收区数据长度为0

	m_comm.GetInput();                              //先预读缓冲区以清除残留数据

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



BEGIN_EVENTSINK_MAP(CCom232TestDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CCom232TestDlg)
	ON_EVENT(CCom232TestDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()




void CCom232TestDlg::OnComm() 
{
VARIANT variant_inp;
COleSafeArray safearray_inp;
    LONG len,k;
    BYTE rxdata[2048];                                     //设置BYTE数组 An 8-bit integerthat is not signed.
    CString strtemp;

	m_strRXData = "";
    if(m_comm.GetCommEvent()==2)                           //事件值为2表示接收缓冲区内有字符
    {                                                     ////////以下你可以根据自己的通信协议加入处理代码
        variant_inp=m_comm.GetInput();                     //读缓冲区
        safearray_inp=variant_inp;                        //VARIANT型变量转换为ColeSafeArray型变量
        len=safearray_inp.GetOneDimSize();                 //得到有效数据长度
        for(k=0;k<len;k++)
            safearray_inp.GetElement(&k,rxdata+k);         //转换为BYTE型数组


        for(k=0;k<len;k++)                                 //将数组转换为Cstring型变量
        {
            BYTE bt=*(char*)(rxdata+k);                      //字符型
            strtemp.Format("%c",bt);                      //将字符送入临时变量strtemp存放
            m_strRXData+=strtemp;                          //加入接收编辑框对应字符串 			
		}
    }
    

    UpdateData(FALSE);                                      //更新编辑框内容
}





void CCom232TestDlg::OnButton1() 
{
	CByteArray binData;
	binData.RemoveAll();
	
	binData.Add('\xc0');
	COleVariant var(binData);
	m_comm.SetOutput(COleVariant(var));                 //发送数据192
	
	m_Motor.SetPos(192);

}

void CCom232TestDlg::OnButton2() 
{

	m_comm.SetOutput(COleVariant("\x40"));               //发送数据64
	
	m_Servo.SetPos(64);
	
}

void CCom232TestDlg::OnButton3() 
{
	if( !m_comm.GetPortOpen())
		m_comm.SetPortOpen(TRUE);                             //打开串口

	int m_int;
	unsigned char m_char;
	
	UpdateData(TRUE);                                     //读取编辑框内容	
	
	m_int = atoi(m_strTXData);
	m_char = m_int;
	
	CByteArray binData;
	binData.RemoveAll();
	binData.Add(m_char);
	COleVariant var(binData);	
	m_comm.SetOutput(COleVariant(var)) ;         //发送数据
	
	UpdateData(FALSE);
	
}

void CCom232TestDlg::OnButton4() 
{
	if( !m_comm.GetPortOpen())
		m_comm.SetPortOpen(TRUE);                          //打开串口

	m_strRXData = "";
	UpdateData(FALSE);
	
}


void CCom232TestDlg::OnCustomdrawSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if( !m_comm.GetPortOpen())
		m_comm.SetPortOpen(TRUE);                  //打开串口

	UpdateData();
	CString str1 = "";
	str1.Format("马达ascii=%d",m_motor);
    m_motor_ascii=str1;

	unsigned char a;
	a = m_motor;

	CByteArray binData;
	binData.RemoveAll();
	binData.Add(a);
	COleVariant var(binData);

	m_comm.SetOutput(COleVariant(var));              //发送数据
	UpdateData(FALSE);                            //更新编辑框内容

	*pResult = 0;
}

void CCom232TestDlg::OnCustomdrawSlider2(NMHDR* pNMHDR, LRESULT* pResult) 
{
	if( !m_comm.GetPortOpen())
		m_comm.SetPortOpen(TRUE);                         //打开串口

	UpdateData();
	CString str1 = "";
	str1.Format("舵机ascii=%d",m_servo);
    m_servo_ascii=str1;

    unsigned char a;
	a = m_servo;

	CByteArray binData;
	binData.RemoveAll();
	binData.Add(a);
	COleVariant var(binData);

	m_comm.SetOutput(COleVariant(var));                   //发送数据
	UpdateData(FALSE);                                    //更新编辑框内容
	
	*pResult = 0;
}



BOOL CCom232TestDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch   (pMsg->message)   
	{   
	case   WM_KEYDOWN:   
		switch   (pMsg->wParam)   
		{   
		case   VK_UP:
		case   VK_DOWN: 
			m_Motor.SetFocus();
			break;
		case   VK_LEFT:   
		case   VK_RIGHT:  
			m_Servo.SetFocus();
			break;   
		}
		break;
	}

	return CDialog::PreTranslateMessage(pMsg);
}



void CCom232TestDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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