📄 swjdlg.cpp
字号:
// SWJDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SWJ.h"
#include "SWJDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSWJDlg dialog
CSWJDlg::CSWJDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSWJDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSWJDlg)
m_strDataBackMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
temp=350;
counter=0;
counter1=0;
flag=0;
}
void CSWJDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSWJDlg)
DDX_Control(pDX, IDC_COMBO_comport, m_ctrlComboComPort);
DDX_Text(pDX, IDC_EDIT_DataBack, m_strDataBackMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSWJDlg, CDialog)
//{{AFX_MSG_MAP(CSWJDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_Stop, OnBUTTONStop)
ON_BN_CLICKED(IDC_BUTTON_Open, OnBUTTONOpen)
ON_BN_CLICKED(IDC_BUTTON_Close, OnBUTTONClose)
ON_BN_CLICKED(IDC_BUTTON_Go, OnBUTTONGo)
ON_MESSAGE(WM_COMM_RXCHAR,OnComm)
ON_BN_CLICKED(IDC_BUTTON_Back, OnBUTTONBack)
ON_BN_CLICKED(IDC_BUTTON_TurnLeft, OnBUTTONTurnLeft)
ON_BN_CLICKED(IDC_BUTTON_TurnRight, OnBUTTONTurnRight)
ON_BN_CLICKED(IDC_BUTTON_Rise, OnBUTTONRise)
ON_BN_CLICKED(IDC_BUTTON_Down, OnBUTTONDown)
ON_BN_CLICKED(IDC_BUTTON_Add, OnBUTTONAdd)
ON_BN_CLICKED(IDC_BUTTON_Reduce, OnBUTTONReduce)
ON_BN_CLICKED(IDC_BUTTON_Reset, OnBUTTONReset)
ON_BN_CLICKED(IDC_BUTTON_Add_1, OnBUTTONAdd1)
ON_BN_CLICKED(IDC_BUTTON_Reduce_1, OnBUTTONReduce1)
ON_BN_CLICKED(IDC_BUTTON_StartDraw, OnBUTTONStartDraw)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_StopDraw, OnBUTTONStopDraw)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSWJDlg message handlers
BOOL CSWJDlg::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);
m_ctrlComboComPort.SetCurSel(0);
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 CSWJDlg::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 CSWJDlg::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();
}
Draw_Frame();
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSWJDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSWJDlg::OnBUTTONOpen()
{
// TODO: Add your control notification handler code here
int nPort=m_ctrlComboComPort.GetCurSel()+1; //得到串口号,想想为什么要加1
if(m_SerialPort.InitPort(this, nPort, 9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
{
m_SerialPort.StartMonitoring();
m_bSerialPortOpened=TRUE;
}
else
{
AfxMessageBox("没有发现此串口或被占用");
m_bSerialPortOpened=FALSE;
}
GetDlgItem(IDC_BUTTON_Open)->EnableWindow(!m_bSerialPortOpened);
GetDlgItem(IDC_BUTTON_Close)->EnableWindow(m_bSerialPortOpened);
}
void CSWJDlg::OnBUTTONClose()
{
// TODO: Add your control notification handler code here
m_SerialPort.ClosePort();//关闭串口
m_bSerialPortOpened=FALSE;
GetDlgItem(IDC_BUTTON_Open)->EnableWindow(!m_bSerialPortOpened);
GetDlgItem(IDC_BUTTON_Close)->EnableWindow(m_bSerialPortOpened);
}
void CSWJDlg::OnBUTTONGo()
{
// TODO: Add your control notification handler code here
CString data_go;
data_go='a';
m_SerialPort.WriteToPort(data_go);
}
void CSWJDlg::OnBUTTONStop()
{
// TODO: Add your control notification handler code here
CString data_stop;
data_stop='0';
m_SerialPort.WriteToPort(data_stop);
}
LONG CSWJDlg::OnComm(WPARAM ch,LPARAM port)
{
counter++;
CString Rx_Data;
switch(ch)
{
case 'a':
Rx_Data="前进";
break;
case 'b':
Rx_Data="后退";
break;
case 'c':
Rx_Data="左转";
break;
case 'd':
Rx_Data="右转";
break;
case '0':
Rx_Data="停止";
break;
case 'e':
Rx_Data="上浮";
break;
case 'f':
Rx_Data="下沉";
break;
case 'r':
Rx_Data="复位";
break;
case 'x':
Rx_Data="水平加速";
break;
case 'y':
Rx_Data="水平减速";
break;
case 'm':
Rx_Data="垂直加速";
break;
case 'n':
Rx_Data="垂直减速";
break;
}
m_strDataBackMsg+=Rx_Data;
m_strDataBackMsg+=' ';
if(counter==13)
{
m_strDataBackMsg+="\r\n";
counter=0;
}
UpdateData(FALSE);
return 0;
}
void CSWJDlg::OnBUTTONBack()
{
// TODO: Add your control notification handler code here
CString data_back;
data_back='b';
m_SerialPort.WriteToPort(data_back);
}
void CSWJDlg::OnBUTTONTurnLeft()
{
// TODO: Add your control notification handler code here
CString data_turnleft;
data_turnleft='c';
m_SerialPort.WriteToPort(data_turnleft);
}
void CSWJDlg::OnBUTTONTurnRight()
{
// TODO: Add your control notification handler code here
CString data_turnright;
data_turnright='d';
m_SerialPort.WriteToPort(data_turnright);
}
void CSWJDlg::OnBUTTONRise()
{
// TODO: Add your control notification handler code here
CString data_rise;
data_rise='e';
m_SerialPort.WriteToPort(data_rise);
}
void CSWJDlg::OnBUTTONDown()
{
// TODO: Add your control notification handler code here
CString data_down;
data_down='f';
m_SerialPort.WriteToPort(data_down);
}
void CSWJDlg::OnBUTTONAdd()
{
// TODO: Add your control notification handler code here
CString data_add;
data_add='x';
m_SerialPort.WriteToPort(data_add);
}
void CSWJDlg::OnBUTTONReduce()
{
// TODO: Add your control notification handler code here
CString data_reduce;
data_reduce='y';
m_SerialPort.WriteToPort(data_reduce);
}
void CSWJDlg::OnBUTTONReset()
{
// TODO: Add your control notification handler code here
CString data_reset;
data_reset='r';
m_SerialPort.WriteToPort(data_reset);
}
void CSWJDlg::OnBUTTONAdd1()
{
// TODO: Add your control notification handler code here
CString data_add1;
data_add1='m';
m_SerialPort.WriteToPort(data_add1);
}
void CSWJDlg::OnBUTTONReduce1()
{
// TODO: Add your control notification handler code here
CString data_reduce;
data_reduce='n';
m_SerialPort.WriteToPort(data_reduce);
}
void CSWJDlg::Init_Rx_Data(int x)
{
int a;
double b;
for(int i=0;i<100;++i)
{
a=rand()%100;
if(a>70)
Rx_Data[i]=x+0.5;
else if(a<40)
Rx_Data[i]=x;
else
Rx_Data[i]=x-0.5;
}
}
void CSWJDlg::Draw_Frame()
{
CDC *pDC=GetDC();
pDC->MoveTo(350,350); //边框
pDC->LineTo(350,50);
pDC->LineTo(800,50);
pDC->LineTo(800,350);
pDC->LineTo(350,350);
for(int i=0;i<10;++i)
{
pDC->MoveTo(350,350-i*30); //左y轴
pDC->LineTo(350+10,350-i*30);
pDC->MoveTo(800,350-i*30); //右y轴
pDC->LineTo(800-10,350-i*30);
pDC->MoveTo(350+i*45,50); //上x轴
pDC->LineTo(350+i*45,50+10);
pDC->MoveTo(350+i*45,350); //下x轴
pDC->LineTo(350+i*45,350-10);
for(int j=0;j<11;++j)
{
if(j==5)
{
pDC->MoveTo(350,350-i*30-j*3); //左y轴
pDC->LineTo(350+7.5,350-i*30-j*3);
pDC->MoveTo(800,350-i*30-j*3); //右y轴
pDC->LineTo(800-7.5,350-i*30-j*3);
pDC->MoveTo(350+i*45+j*4.5,50); //上x轴
pDC->LineTo(350+i*45+j*4.5,50+7.5);
pDC->MoveTo(350+i*45+j*4.5,350); //下x轴
pDC->LineTo(350+i*45+j*4.5,350-7.5);
}
else
{
pDC->MoveTo(350,350-i*30-j*3); //左y轴
pDC->LineTo(350+5,350-i*30-j*3);
pDC->MoveTo(800,350-i*30-j*3); //右y轴
pDC->LineTo(800-5,350-i*30-j*3);
pDC->MoveTo(350+i*45+j*4.5,50); //上x轴
pDC->LineTo(350+i*45+j*4.5,50+5);
pDC->MoveTo(350+i*45+j*4.5,350); //下x轴
pDC->LineTo(350+i*45+j*4.5,350-5);
}
}
}
}
void CSWJDlg::Draw()
{
CDC *pDC=GetDC();
if(flag)
{
temp-=4.5;
--counter1;
}
pDC->MoveTo(temp,350-Rx_Data[counter1]*3);
flag=1;
temp+=4.5;
++counter1;
pDC->LineTo(temp,350-Rx_Data[counter1]*3);
flag=0;
}
void CSWJDlg::OnBUTTONStartDraw()
{
// TODO: Add your control notification handler code here
Init_Rx_Data(20);
SetTimer(1,1000,NULL);
}
void CSWJDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Draw();
CDialog::OnTimer(nIDEvent);
}
void CSWJDlg::OnBUTTONStopDraw()
{
// TODO: Add your control notification handler code here
KillTimer(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -