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

📄 com_ndlg.cpp

📁 本程序调用了微软自带的mscomm控件
💻 CPP
字号:
// Com_NDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Com_N.h"
#include "Com_NDlg.h"

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

#include "ConfigureDlg.h"
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCom_NDlg dialog

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

void CCom_NDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCom_NDlg)
	DDX_Control(pDX, IDC_MSCOMM, m_Comm);
	DDX_Text(pDX, IDC_EDIT_ComData, m_comdata);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCom_NDlg, CDialog)
	//{{AFX_MSG_MAP(CCom_NDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_CONFIG, OnButtonConfig)
	ON_BN_CLICKED(IDC_BUTTON_OPENCOM, OnButtonOpencom)
	ON_BN_CLICKED(IDC_BUTTON_CLOSECOM, OnButtonClosecom)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCom_NDlg message handlers

BOOL CCom_NDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

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

void CCom_NDlg::OnComm() 
{
	// TODO: Add your control notification handler code here
	COleVariant myVAr;
    COleSafeArray safearray_inp;
    LONG len,k;
    BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
    CString strtemp;

	int nEvent = m_Comm.GetCommEvent();

    if(nEvent==2) //事件值为2表示接收缓冲区内有字符
    {             ////////以下你可以根据自己的通信协议加入处理代码
	
        //variant_inp=m_Comm.GetInput(); //读缓冲区
		myVAr.Attach(m_Comm.GetInput());
        safearray_inp=myVAr; //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=*(unsigned char*)(rxdata+k); //字符型
            strtemp.Format("%c ",bt); //将字符送入临时变量strtemp存放
            m_comdata+=strtemp+"\n"; //加入接收编辑框对应字符串 
			strtemp="";
        }
	}
	UpdateData(FALSE);
}

void CCom_NDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	SendMessage(WM_CLOSE);
}

void CCom_NDlg::OnButtonConfig() 
{
	// TODO: Add your control notification handler code here
	CConfigureDlg config;

	config.m_COMPort=m_Comm.GetCommPort();
	config.m_DTREnable=m_Comm.GetDTREnable();
	config.m_HandShaking=m_Comm.GetHandshaking();
	config.m_InBufferSize=m_Comm.GetInBufferSize();
	config.m_InputMode=m_Comm.GetInputMode();
	config.m_NULLDiscard=m_Comm.GetNullDiscard();
	config.m_OutBufferSize=m_Comm.GetOutBufferSize();
	config.m_ParityReplace=m_Comm.GetParityReplace();
	config.m_RThreshold=m_Comm.GetRThreshold();
	config.m_RTSEnable=m_Comm.GetRTSEnable();
	config.m_Setting=m_Comm.GetSettings();
	config.m_SThreshold=m_Comm.GetSThreshold();

	if(config.DoModal()==IDOK)
	{
		m_Comm.SetCommPort(config.m_COMPort);
		m_Comm.SetDTREnable(config.m_DTREnable);
		m_Comm.SetHandshaking(config.m_HandShaking);
		m_Comm.SetInBufferSize(config.m_InBufferSize);
		m_Comm.SetInputMode(config.m_InputMode);
		m_Comm.SetNullDiscard(config.m_NULLDiscard);
		m_Comm.SetOutBufferSize(config.m_OutBufferSize);
		m_Comm.SetParityReplace(config.m_ParityReplace);
		m_Comm.SetRThreshold(config.m_RThreshold);
		m_Comm.SetRTSEnable(config.m_RTSEnable);
		m_Comm.SetSettings(config.m_Setting);
		m_Comm.SetSThreshold(config.m_SThreshold);
	}
	
}

void CCom_NDlg::OnButtonOpencom() 
{
	// TODO: Add your control notification handler code here
	if(m_Comm.GetPortOpen()==TRUE) //串口如果已打开
	{
		m_Comm.SetPortOpen(FALSE); 
	}
	    m_Comm.SetCommPort(1);//选择com?
        m_Comm.SetInBufferSize(1024);//设置输入缓冲区的大小
        m_Comm.SetOutBufferSize(1024);// 设置输出缓冲区的大小
        m_Comm.SetOutBufferCount(0);//清空输出缓冲区
	    m_Comm.SetInBufferCount(0);
        m_Comm.SetInputMode(1);//设置输入方式为二进制方式
	    m_Comm.SetRThreshold(1); /*每接收一个字符则激发OnComm()事件*/
	    m_Comm.SetSettings("9600,n,8,1");//设置波特率等参数
	    //打开串口
		m_Comm.SetPortOpen(TRUE);

}


void CCom_NDlg::OnButtonClosecom() 
{
	// TODO: Add your control notification handler code here
    if (m_Comm.GetPortOpen()==TRUE)
	{
		m_Comm.SetPortOpen(FALSE);
	}
	
}

⌨️ 快捷键说明

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