📄 com232testdlg.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_EN_CHANGE(IDC_EDIT_TXDATA, OnChangeEditTxdata)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
ON_BN_CLICKED(IDC_BUTTONA, OnButtona)
ON_BN_CLICKED(IDC_BUTTONB, OnButtonb)
ON_BN_CLICKED(IDC_BUTTONC, OnButtonc)
ON_BN_CLICKED(IDC_BUTTOND, OnButtond)
ON_BN_CLICKED(IDC_BUTTONE, OnButtone)
ON_BN_CLICKED(IDC_BUTTONF, OnButtonf)
ON_BN_CLICKED(IDC_BUTTON0, OnButton0)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER1, OnCustomdrawSlider1)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER2, OnCustomdrawSlider2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCom232TestDlg message handlers
BOOL CCom232TestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
m_Motor.SetRange(1,127);
m_Servo.SetRange(128,255);
if(m_comm.GetPortOpen())
m_comm.SetPortOpen(FALSE);
m_comm.SetCommPort(3); //选择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;
}
void CCom232TestDlg::OnChangeEditTxdata()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
UpdateData(TRUE); //读取编辑框内容
m_comm.SetOutput(COleVariant(m_strTXData));//发送数据
}
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;
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()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("\x3f"));//发送数据
}
void CCom232TestDlg::OnButton2()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("\xbf\xbf"));//发送数据
Sleep(20);
}
void CCom232TestDlg::OnButton3()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("3"));//发送数据
}
void CCom232TestDlg::OnButton4()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("4"));//发送数据
}
void CCom232TestDlg::OnButton5()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("5"));//发送数据
}
void CCom232TestDlg::OnButton6()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("6"));//发送数据
}
void CCom232TestDlg::OnButton7()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("7"));//发送数据
}
void CCom232TestDlg::OnButton8()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("8"));//发送数据
}
void CCom232TestDlg::OnButton9()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("9"));//发送数据
}
void CCom232TestDlg::OnButtona()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("A"));//发送数据
}
void CCom232TestDlg::OnButtonb()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("B"));//发送数据
}
void CCom232TestDlg::OnButtonc()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("C"));//发送数据
}
void CCom232TestDlg::OnButtond()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("D"));//发送数据e
}
void CCom232TestDlg::OnButtone()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("E"));//发送数据
}
void CCom232TestDlg::OnButtonf()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("F"));//发送数据
}
void CCom232TestDlg::OnButton0()
{
if( !m_comm.GetPortOpen())
m_comm.SetPortOpen(TRUE);//打开串口
m_comm.SetOutput(COleVariant("0"));//发送数据
}
//DEL void CCom232TestDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult)
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL if( !m_comm.GetPortOpen())
//DEL m_comm.SetPortOpen(TRUE);//打开串口
//DEL UpdateData();
//DEL CString str1 = "";
//DEL str1.Format("马达ascii=%d",m_motor);
//DEL m_motor_ascii=str1;
//DEL char a;
//DEL a = m_motor;
//DEL CString str = "";
//DEL str +=a;
//DEL m_comm.SetOutput(COleVariant(str));//发送数据
//DEL UpdateData(FALSE); //更新编辑框内容
//DEL *pResult = 0;
//DEL }
//DEL void CCom232TestDlg::OnReleasedcaptureSlider2(NMHDR* pNMHDR, LRESULT* pResult)
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL if( !m_comm.GetPortOpen())
//DEL m_comm.SetPortOpen(TRUE);//打开串口
//DEL UpdateData();
//DEL CString str1 = "";
//DEL str1.Format("舵机ascii=%d",m_servo);
//DEL m_servo_ascii=str1;
//DEL char a;
//DEL a = m_servo;
//DEL CString str = "";
//DEL str +=a;
//DEL str +=a;
//DEL m_comm.SetOutput(COleVariant(str));//发送数据
//DEL UpdateData(FALSE); //更新编辑框内容
//DEL *pResult = 0;
//DEL }
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;
char a;
a = m_motor;
CString str = "";
str +=a;
m_comm.SetOutput(COleVariant(str));//发送数据
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;
char a;
a = m_servo;
CString str = "";
str +=a;
str +=a;
m_comm.SetOutput(COleVariant(str));//发送数据
UpdateData(FALSE); //更新编辑框内容
*pResult = 0;
}
//DEL void CCom232TestDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL
//DEL CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
//DEL }
//DEL void CCom232TestDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL if((nChar == VK_UP)||(nChar == VK_DOWN))
//DEL MessageBox("wo ai ni");
//DEL CDialog::OnChar(nChar, nRepCnt, nFlags);
//DEL }
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);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -