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

📄 comtestdlg.cpp

📁 串口RS232与PIC16F877A单片机通信.用于控制PIC单片机产生音乐,附完整报告.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ComTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ComTest.h"
#include "ComTestDlg.h"

#include "mscomm.h"
#include"afx.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)
	virtual void OnOK();
	//}}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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComTestDlg dialog

CComTestDlg::CComTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CComTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CComTestDlg)
	m_fasong = _T("");
	m_jieshou = _T("");
	m_save = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CComTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComTestDlg)
	DDX_Control(pDX, IDC_BUTTON24, m_button24);
	DDX_Control(pDX, IDC_BUTTON23, m_button23);
	DDX_Control(pDX, IDC_BUTTON22, m_button22);
	DDX_Control(pDX, IDC_EDIT3, m_edit3);
	DDX_Control(pDX, IDC_BUTTON21, m_button21);
	DDX_Control(pDX, IDC_BUTTON20, m_button20);
	DDX_Control(pDX, IDC_BUTTON19, m_button19);
	DDX_Control(pDX, IDC_BUTTON9, m_button9);
	DDX_Control(pDX, IDC_BUTTON18, m_button18);
	DDX_Control(pDX, IDC_BUTTON17, m_button7);
	DDX_Control(pDX, IDC_BUTTON16, m_button6);
	DDX_Control(pDX, IDC_BUTTON15, m_button5);
	DDX_Control(pDX, IDC_BUTTON14, m_button4);
	DDX_Control(pDX, IDC_BUTTON13, m_button3);
	DDX_Control(pDX, IDC_BUTTON12, m_button2);
	DDX_Control(pDX, IDC_BUTTON11, m_button1);
	DDX_Control(pDX, IDC_BUTTON10, m_button0);
	DDX_Text(pDX, IDC_EDIT1, m_fasong);
	DDX_Text(pDX, IDC_EDIT2, m_jieshou);
	DDX_Control(pDX, IDC_MSCOMM, m_comport);
	DDX_Text(pDX, IDC_EDIT3, m_save);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CComTestDlg, CDialog)
	//{{AFX_MSG_MAP(CComTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_CHAR()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON5, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON6, OnButtonAbout)
	ON_BN_CLICKED(IDC_BUTTON7, OnButtonClr)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON8, OnButtonWrite)
	ON_BN_CLICKED(IDC_BUTTON4, OnButtonSaveEdit)
	ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
	ON_BN_CLICKED(IDC_BUTTON9, OnButtonBBMusic)
	ON_BN_CLICKED(IDC_BUTTON18, OnButtonBBRead)
	ON_BN_CLICKED(IDC_BUTTON19, OnButtonBB1)
	ON_BN_CLICKED(IDC_BUTTON20, OnButton20)
	ON_BN_CLICKED(IDC_BUTTON21, OnButton21)
	ON_BN_CLICKED(IDC_BUTTON22, OnButtonRead1)
	ON_BN_CLICKED(IDC_BUTTON23, OnButtonRead2)
	ON_BN_CLICKED(IDC_BUTTON24, OnButtonRead3)
	ON_BN_CLICKED(IDC_BUTTON25, OnButton25)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComTestDlg message handlers

BOOL CComTestDlg::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
	flag_music=TRUE;
	// TODO: Add extra initialization here
	m_comport.SetCommPort(1); 
	if(!m_comport.GetPortOpen())
	{
		m_comport.SetPortOpen(TRUE);
	}	
	m_comport.SetHandshaking(0);
	m_comport.SetInputMode(1);		//1010
	m_comport.SetSettings("19200,n,8,1"); 
	m_comport.SetInBufferCount(0);
	m_comport.SetOutBufferCount(0);
	m_comport.SetRThreshold(1); //every letter to receive
	m_comport.SetSThreshold(0); //send not to oncommn
	m_comport.SetInputLen(1); 	//one letter to read once	
	m_button9.EnableWindow(TRUE);
	m_button18.EnableWindow(FALSE);
	m_button19.EnableWindow(FALSE);
	m_button20.EnableWindow(FALSE);
	m_button21.EnableWindow(FALSE);
	m_button22.EnableWindow(FALSE);
	m_button23.EnableWindow(FALSE);
	m_button24.EnableWindow(FALSE);
	m_edit3.EnableWindow(FALSE);
	flag_ele=0;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

BEGIN_EVENTSINK_MAP(CComTestDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CComTestDlg)
	ON_EVENT(CComTestDlg, IDC_MSCOMM, 1 /* OnComm */, OnOnCommMscomm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

void CComTestDlg::OnOnCommMscomm() //change m_jieshou
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_comport.GetCommEvent()==2)
	{
		VARIANT	input1;
		char *str,*str1;
		input1=m_comport.GetInput();
		str=(char*)(unsigned char*)input1.parray->pvData;
		str1=str;
		str1++;
		*str1='\0';
		m_jieshou+=(const char*)str;
		UpdateData(FALSE);
	}
}
void CComTestDlg::OnButton1() ///send
{
	// TODO: Add your control notification handler code here
		UpdateData(TRUE);
	if(m_fasong.GetLength() > 30)
	{
		AfxMessageBox("ALARM! input must <30");		
	}
	else
	{
	if(!m_comport.GetPortOpen())
		m_comport.SetPortOpen(TRUE);
	m_comport.SetOutput(COleVariant(m_fasong));
	m_fasong="";
	UpdateData(FALSE);

	BYTE am=0x0a;
	fasong_temp.RemoveAll();
	fasong_temp.SetSize(1);
	fasong_temp.SetAt(0,am);		//fasong_temp get last letter	
	m_comport.SetOutput(COleVariant(fasong_temp));   
	}

}

void CComTestDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	BYTE am=nChar;
	
	if(flag_music==TRUE)
	{
		UpdateData(1);  
		fasong_temp.RemoveAll();
		//int m=0;						//commute string m	
		fasong_temp.SetSize(1);
		fasong_temp.SetAt(0,am);		//fasong_temp get last letter	
		m_comport.SetOutput(COleVariant(fasong_temp));   
		UpdateData(0);
	

/////////////////////////////////////
	switch(am)
	{
	case '0':
	    	m_button0.EnableWindow(FALSE);
			m_button1.EnableWindow(TRUE);
			m_button2.EnableWindow(TRUE);
			m_button3.EnableWindow(TRUE);
			m_button4.EnableWindow(TRUE);
			m_button5.EnableWindow(TRUE);
			m_button6.EnableWindow(TRUE);
			m_button7.EnableWindow(TRUE);
			break;
	case '1':
			m_button0.EnableWindow(TRUE);
			m_button1.EnableWindow(FALSE);
			m_button2.EnableWindow(TRUE);
			m_button3.EnableWindow(TRUE);
			m_button4.EnableWindow(TRUE);
			m_button5.EnableWindow(TRUE);
			m_button6.EnableWindow(TRUE);
			m_button7.EnableWindow(TRUE);
			break;
	case '2':
			m_button0.EnableWindow(TRUE);
			m_button1.EnableWindow(TRUE);
			m_button2.EnableWindow(FALSE);
			m_button3.EnableWindow(TRUE);
			m_button4.EnableWindow(TRUE);
			m_button5.EnableWindow(TRUE);
			m_button6.EnableWindow(TRUE);
			m_button7.EnableWindow(TRUE);
			break;
	case '3':
			m_button0.EnableWindow(TRUE);
			m_button1.EnableWindow(TRUE);
			m_button2.EnableWindow(TRUE);
			m_button3.EnableWindow(FALSE);
			m_button4.EnableWindow(TRUE);
			m_button5.EnableWindow(TRUE);
			m_button6.EnableWindow(TRUE);
			m_button7.EnableWindow(TRUE);
			break;
	case '4':	 
			m_button0.EnableWindow(TRUE);
			m_button1.EnableWindow(TRUE);
			m_button2.EnableWindow(TRUE);
			m_button3.EnableWindow(TRUE);
			m_button4.EnableWindow(FALSE);
			m_button5.EnableWindow(TRUE);
			m_button6.EnableWindow(TRUE);

⌨️ 快捷键说明

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