www.gusucode.com > VC实现简单的串口收发功能,结构简单,方便初学者学习使用 > VC实现简单的串口收发功能,结构简单,方便初学者学习使用/SerialComm/SerialCommDlg.cpp

    // SerialCommDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SerialComm.h"
#include "SerialCommDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSerialCommDlg dialog

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

void CSerialCommDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialCommDlg)
	DDX_Control(pDX, IDC_LIST1, m_receive);
	DDX_Control(pDX, IDC_COMBO_PORT, m_combo_port);
	DDX_Control(pDX, IDC_CHECK1, m_ctrlHexDisplay);
	DDX_Control(pDX, IDC_MSCOMM1, m_mscomm1);
	DDX_Text(pDX, IDC_EDIT_SEND, m_send);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSerialCommDlg, CDialog)
	//{{AFX_MSG_MAP(CSerialCommDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_CBN_SELCHANGE(IDC_COMBO_PORT, OnSelchangeComboPort)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialCommDlg message handlers

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

	// 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

	m_brush.CreateSolidBrush(RGB(46,68,89));	
	// TODO: Add extra initialization here
	if(m_mscomm1.GetPortOpen())
		m_mscomm1.SetPortOpen(FALSE);
	
	m_mscomm1.SetCommPort(4); //选择com1
	if( !m_mscomm1.GetPortOpen())
		m_mscomm1.SetPortOpen(TRUE);//打开串口
	else
		AfxMessageBox("cannot open serial port");
	
	m_mscomm1.SetSettings("9600,n,8,1"); //波特率9600,标记 (Mark),8个数据位,1个停止位 
	
	m_mscomm1.SetInputMode(1); //1:表示以二进制方式检取数据;为什么不是ASCII码?
	//m_ctrlComm.SetInputModel(0);
	m_mscomm1.SetRThreshold(50); 
	//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
	m_mscomm1.SetInputLen(0); //设置当前接收区数据长度为0
    m_mscomm1.GetInput();//先预读缓冲区以清除残留数据

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

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

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

void CSerialCommDlg::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
	VARIANT variant_inp;
	COleSafeArray safearray_inp;
	
	LONG len,k;
	BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
	CString strtemp,str;
	if(m_mscomm1.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
	{
		str="";
		variant_inp=m_mscomm1.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); //字符型
			if(m_ctrlHexDisplay.GetCheck())
				strtemp.Format("%02X ",bt); //将字符以十六进制方式送入临时变量strtemp存放,注意这里加入一个空隔
			else 
				strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
		   str+=strtemp;
		
		//	m_receive+=strtemp; //加入接收编辑框对应字符串 
		}
		m_receive.AddString(str);
		UpdateData(false);
 }
}

void CSerialCommDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	
	CByteArray sendArr; 
	WORD wLength;
	
	wLength = m_send.GetLength();
	sendArr.SetSize(wLength);
	for(int i =0; i<wLength; i++)
	{
		sendArr.SetAt(i, m_send.GetAt(i));
	}
	 m_mscomm1.SetOutput(COleVariant(sendArr));
}

void CSerialCommDlg::OnSelchangeComboPort() 
{
	// TODO: Add your control notification handler code here
	CString str;
	m_combo_port.GetLBText(m_combo_port.GetCurSel(),str);
	int i;
	i=atoi(str); 
	if(m_mscomm1.GetPortOpen()) //打开串口
	{
		m_mscomm1.SetPortOpen(FALSE); 
	}
	m_mscomm1.SetCommPort(i); //串口1
	if(!m_mscomm1.GetPortOpen()) //打开串口
	{
		m_mscomm1.SetPortOpen(true); 
	}
	m_mscomm1.SetSettings("9600,n,8,1"); //设置波特率等参数 
	m_mscomm1.SetInputMode(1); //设置输入方式为二进制方式 
	m_mscomm1.SetRThreshold(1); //为1表示有一个字符即引发事件 
	m_mscomm1.SetInputLen(0);
	m_mscomm1.GetInput();//先预读缓冲区以清除残留数据
}

HBRUSH CSerialCommDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if( nCtlColor==CTLCOLOR_STATIC ) //设置static文本颜色透明
	{ 
		pDC->SetTextColor(RGB(255,255,255)); 
		pDC->SetBkMode(TRANSPARENT); //设置透明 
	}
	// TODO: Return a different brush if the default is not desired
	return m_brush;
}