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

📄 mcu2pcdlg.cpp

📁 基于VC++6.0应用MSCOMM控件的串口通信上位机程序,实现与51单片机的通信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// mcu2pcDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mcu2pc.h"
#include "mcu2pcDlg.h"
#include "mscomm.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()

/////////////////////////////////////////////////////////////////////////////
// CMcu2pcDlg dialog

CMcu2pcDlg::CMcu2pcDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMcu2pcDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMcu2pcDlg)
	m_strreceive = _T("");
	m_id = _T("");
	m_money = 0.0f;
	m_money2 = 0.0f;
	m_rrr = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMcu2pcDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMcu2pcDlg)
	DDX_Control(pDX, IDC_BTN_STOPDIS, m_ctrlStopDis);
	DDX_Control(pDX, IDC_STATICS, m_status);
	DDX_Control(pDX, IDC_OPENCOM, m_OpenCom);
	DDX_Control(pDX, IDC_STATIC_ICON, m_SetIcon);
	DDX_Control(pDX, IDC_COMBO_CHECK, m_check);
	DDX_Control(pDX, IDC_COMBO_STOPBITS, m_stopbits);
	DDX_Control(pDX, IDC_COMBO_SPEED, m_speed);
	DDX_Control(pDX, IDC_COMBO_DATABITS, m_databits);
	DDX_Control(pDX, IDC_COMBO_COMSEL, m_comsel);
	DDX_Text(pDX, IDC_EDIT_RECEIVE, m_strreceive);
	DDX_Text(pDX, IDC_ID, m_id);
	DDX_Text(pDX, IDC_MONEY, m_money);
	DDX_Text(pDX, IDC_ADD_DEC, m_money2);
	DDX_Text(pDX, IDC_EDIT1, m_rrr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMcu2pcDlg, CDialog)
	//{{AFX_MSG_MAP(CMcu2pcDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELENDOK(IDC_COMBO_COMSEL, OnSelendokComboComsel)
	ON_CBN_SELENDOK(IDC_COMBO_CHECK, OnSelendokComboCheck)
	ON_CBN_SELENDOK(IDC_COMBO_DATABITS, OnSelendokComboDatabits)
	ON_CBN_SELENDOK(IDC_COMBO_SPEED, OnSelendokComboSpeed)
	ON_CBN_SELENDOK(IDC_COMBO_STOPBITS, OnSelendokComboStopbits)
	ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
	ON_BN_CLICKED(IDC_BTN_CLR_REC, OnBtnClrRec)
	ON_BN_CLICKED(IDC_BTN_STOPDIS, OnBtnStopdis)
	ON_BN_CLICKED(IDC_BTN_CLR_SND, OnBtnClrSnd)
	ON_BN_CLICKED(IDC_RD, OnRd)
	ON_BN_CLICKED(IDC_WR, OnWr)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DEC, OnDec)
	ON_EN_CHANGE(IDC_ADD_DEC, OnChangeAddDec)
	ON_EN_CHANGE(IDC_ID, OnChangeId)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMcu2pcDlg message handlers

BOOL CMcu2pcDlg::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


	m_comsel.SetCurSel(0);		//下拉选框
	m_check.SetCurSel(0);
	m_speed.SetCurSel(5);
	m_databits.SetCurSel(0);
	m_stopbits.SetCurSel(0);

	m_hIconRed  = AfxGetApp()->LoadIcon(IDI_ICON_RED);//COM开关指示图标
	m_hIconOff	= AfxGetApp()->LoadIcon(IDI_ICON_OFF);
	
	
	m_nCom=m_comsel.GetCurSel()+1;
	strBaud=_T("9600");	
	strCheck=_T("N");
	strDatabits=_T("8");;
	strStopbits=_T("1");
	strSettings=strBaud+','+strCheck+','+strDatabits+','+strStopbits;


	DWORD style=WS_VISIBLE | WS_CHILD;
	if(!m_Port.Create(NULL,style,CRect(0,0,0,0),this,IDC_MSCOMM))
	{
		TRACE0("Failed to create OLE Communications Control\n");
		return -1;
	}

	
	m_Port.SetCommPort(1);//选择串口号
	m_Port.SetInBufferSize(14);//输入缓冲
	m_Port.SetOutBufferSize(14);//输出缓冲
	
	if(!m_Port.GetPortOpen())//打开串口
	m_Port.SetPortOpen(TRUE);
	m_Port.SetInputMode(1);//设置输入方式为二进制方式 
	m_Port.SetSettings(strSettings);//参数
	m_Port.SetRThreshold(14);//14个字符引发一个事件
	m_Port.SetInputLen(0);



	UpdateStatusEdit();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


void CMcu2pcDlg::OnSelendokComboComsel() //COM选择
{
	// TODO: Add your control notification handler code here
	m_nCom=m_comsel.GetCurSel()+1;//获取串口号
		if(m_Port.GetPortOpen())//关闭串口
	m_Port.SetPortOpen(FALSE);

	
	m_Port.SetCommPort(m_nCom);//选择串口号	

if(m_bOpenCom)
{
	if(!m_Port.GetPortOpen())//打开串口
	m_Port.SetPortOpen(TRUE);
}
	UpdateComCFG();

}

void CMcu2pcDlg::OnSelendokComboCheck() 
{
	// TODO: Add your control notification handler code here
	int i;
	i=m_check.GetCurSel();
	switch(i)
	{
		case 0:	strCheck=_T("N");		break;
		case 1:	strCheck=_T("E");		break;
		case 2:	strCheck=_T("O");		break;
		default:break;
	}
	//UpdateData(TRUE);
//	strSettings=strBaud+','+strCheck+','+strDatabits+','+strStopbits;
	UpdateComCFG();
}

void CMcu2pcDlg::OnSelendokComboDatabits() 
{
	// TODO: Add your control notification handler code here
	int i;
	i=m_databits.GetCurSel();
	switch(i)
	{
		case 0:	strDatabits=_T("8");		break;
		case 1:	strDatabits=_T("7");		break;
		case 2:	strDatabits=_T("6");		break;
		default:break;
	}
//	strSettings=strBaud+','+strCheck+','+strDatabits+','+strStopbits;
	UpdateComCFG();
}

void CMcu2pcDlg::OnSelendokComboSpeed() 
{
	// TODO: Add your control notification handler code here
	int i;
	i=m_speed.GetCurSel();
	switch(i)
	{
		case 0:	strBaud=_T("300");		break;
		case 1:	strBaud=_T("600");		break;
		case 2:	strBaud=_T("1200");		break;
		case 3:	strBaud=_T("2400");		break;
		case 4:	strBaud=_T("4800");		break;
		case 5:	strBaud=_T("9600");		break;
		case 6:	strBaud=_T("14400");		break;
		case 7: strBaud=_T("19200");		break;
		case 8:strBaud=_T("28800");		break;
		case 9:strBaud=_T("38400");		break;
		case 10:strBaud=_T("56000");		break;
		case 11:strBaud=_T("57600");		break;
		case 12:strBaud=_T("115200");		break;
	
		default:break;
	}
//	strSettings=strBaud+','+strCheck+','+strDatabits+','+strStopbits;
	UpdateComCFG();
}

void CMcu2pcDlg::OnSelendokComboStopbits() 
{
	// TODO: Add your control notification handler code here
	int i;
	i=m_stopbits.GetCurSel();
	switch(i)
	{
		case 0:	strStopbits=_T("1");		break;
		case 1:	strStopbits=_T("2");		break;		
		default:break;
	}
	
	UpdateComCFG();
}

void CMcu2pcDlg::UpdateComCFG()
{
	strSettings=strBaud+','+strCheck+','+strDatabits+','+strStopbits;	
	m_Port.SetInputMode(1);//设置输入方式为二进制方式 
	m_Port.SetSettings(strSettings);//参数
	m_Port.SetRThreshold(1);//一个字符引发一个事件
	m_Port.SetInputLen(0);
	UpdateStatusEdit();	
//	UpdateData(FALSE);

}

void CMcu2pcDlg::UpdateStatusEdit()
{
	switch(m_nCom)
	{
		case 1:strCom=_T("COM1"); break;
		case 2:strCom=_T("COM2"); break;
		case 3:strCom=_T("COM3"); break;
		case 4:strCom=_T("COM4"); break;
		default:break;
	}
	m_comstatus=strSettings+','+strCom;
	if(m_bOpenCom)
	{
		m_status.SetWindowText(m_comstatus);
	}else m_status.SetWindowText("串口已关闭");
	//UpdateData(FALSE);
}

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

void CMcu2pcDlg::OnCommMscomm() 
{
	// TODO: Add your control notification handler code here


	UpdateData(TRUE);
	VARIANT variant_inp;//接收缓冲暂存
	COleSafeArray safearray_inp;
	LONG len,k;
	
	CString strtemp;
	m_getstr="";
	if(m_Port.GetCommEvent()==2)//事收件值为2表示接缓冲区内有字符
	{
		variant_inp=m_Port.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++)

⌨️ 快捷键说明

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