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

📄 mscomm32dlg.cpp

📁 串口调试程序
💻 CPP
字号:
// mscomm32Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "mscomm32.h"
#include "mscomm32Dlg.h"
#include "DlgProxy.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()

/////////////////////////////////////////////////////////////////////////////
// CMscomm32Dlg dialog

IMPLEMENT_DYNAMIC(CMscomm32Dlg, CDialog);

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

CMscomm32Dlg::~CMscomm32Dlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CMscomm32Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMscomm32Dlg)
	DDX_Control(pDX, IDC_TAB1, m_Tab);
	DDX_Text(pDX, IDC_EDIT_RXDATA, m_strEditRXData);
	DDX_Text(pDX, IDC_EDIT_TXDATA, m_strEditTXData);
	DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
	DDX_Control(pDX, IDC_SBARCTRL1, m_StatusBar);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMscomm32Dlg, CDialog)
	//{{AFX_MSG_MAP(CMscomm32Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_MANUALSEND, OnButtonManualsend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMscomm32Dlg message handlers

BOOL CMscomm32Dlg::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_ctrlComm.SetCommPort(7);//选择comm1口
	m_ctrlComm.SetInputMode(1);//二进制输入方式
	m_ctrlComm.SetInBufferSize(1024);//设置输入缓冲区大小为1024
	m_ctrlComm.SetOutBufferSize(512);
	m_ctrlComm.SetSettings("9600,n,8,1");
	//if(m_ctrlComm.GetPortOpen())
		m_ctrlComm.SetPortOpen(TRUE);
	m_ctrlComm.SetRThreshold(1);
	m_ctrlComm.SetInputLen(0);
	m_ctrlComm.GetInput();//读取缓冲区,并清空之,为开始正式通讯做准备
	
	//BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMscomm32Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CMscomm32Dlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

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

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CMscomm32Dlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CMscomm32Dlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CMscomm32Dlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CMscomm32Dlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

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

void CMscomm32Dlg::OnComm() 
{
	// TODO: Add your control notification handler code here
	VARIANT variant_inp;//Variant 是一种特殊的数据类型,除了定长 String 数据及用户定义类型外,可以包含任何种类的数据
	COleSafeArray safearray_inp;//COleSafeArray类是用于处理任意类型和维数的数组的类
	LONG len,k;
	BYTE rxdata[2048];
	CString strtemp="",str;
	if (m_ctrlComm.GetCommEvent()==2)//2表示接收缓冲区内有数据
	{
		//rxdata=m_ctrlComm.GetInput();
		variant_inp=m_ctrlComm.GetInput();//读取接收缓冲区内数据
		safearray_inp=variant_inp;//不确定型变量variant_inp转换为任意数组型变量
		len=safearray_inp.GetOneDimSize();//得到数组长度,即读取的字节数
		for(k=0;k<len;k++)
			safearray_inp.GetElement(&k,rxdata+k);
		for(k=0;k<len;k++)
		{
			BYTE bt,bt_2;			
			bt=*(char*)(rxdata+k);//(rxdata+k)为收到数据流中某个字节的地址,其值=&rxdata[k],即第K个字节的地址
			bt_2=int(rxdata[k]);			
			//MessageBox(str,   "Caption",   MB_OK+MB_ICONINFORMATION);			
			strtemp.Format("%x",bt_2);
			m_strEditRXData+=strtemp;
		}

	}
	UpdateData(FALSE);
	
}

void CMscomm32Dlg::OnButtonManualsend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_ctrlComm.SetOutput(COleVariant(m_strEditTXData));//发送数据
	
}

⌨️ 快捷键说明

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