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

📄 multiporttestdlg.cpp

📁 利用CSerialPort实现的双串口通信测试程序
💻 CPP
字号:
// MultiportTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiportTest.h"
#include "MultiportTestDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMultiportTestDlg dialog

CMultiportTestDlg::CMultiportTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultiportTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMultiportTestDlg)
	m_com1Receive = _T("");
	m_com1Send = _T("fe ff 12 0a 0d 1b");
	m_com1Timer = 1000;
	m_com2Receive = _T("");
	m_com2Send = _T("fe ff 12 0a 0d 1b");
	m_com2Timer = 1000;
	icom1ReceiveRadio=0;
	bcom1Send=FALSE;
	icom2ReceiveRadio=0;
	bcom2Send=FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMultiportTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMultiportTestDlg)
	DDX_Control(pDX, IDC_COM2MANUALSEND, m_btnCom2Manualsend);
	DDX_Control(pDX, IDC_COM1MANUALSEND, m_btnCom1Manualsend);
	DDX_Control(pDX, IDC_COM2ASCSEND, m_com2ASCsend);
	DDX_Control(pDX, IDC_COM2ASCDISP, m_com2ASCdisp);
	DDX_Control(pDX, IDC_COM2HEXSEND, m_com2HEXsend);
	DDX_Control(pDX, IDC_COM2HEXDISP, m_com2HEXdisp);
	DDX_Control(pDX, IDC_COM1HEXSEND, m_com1HEXsend);
	DDX_Control(pDX, IDC_COM1HEXDISP, m_com1HEXdisp);
	DDX_Control(pDX, IDC_COM1ASCSEND, m_com1ASCsend);
	DDX_Control(pDX, IDC_COM1ASCDISP, m_com1ASCdisp);
	DDX_Control(pDX, IDC_OPENCOM2, m_btnOpencom2);
	DDX_Control(pDX, IDC_OPENCOM1, m_btnOpencom1);
	DDX_Control(pDX, IDC_COM1AUTOSEND, m_btnCom1Autosend);
	DDX_Control(pDX, IDC_COM2AUTOSEND, m_btnCom2Autosend);
	DDX_Control(pDX, IDC_CLOSECOM2, m_btnClosecom2);
	DDX_Control(pDX, IDC_CLOSECOM1, m_btnClosecom1);
	DDX_Text(pDX, IDC_COM1RECEIVE, m_com1Receive);
	DDX_Text(pDX, IDC_COM1SEND, m_com1Send);
	DDX_Text(pDX, IDC_COM1TIMER, m_com1Timer);
	DDX_Text(pDX, IDC_COM2RECEIVE, m_com2Receive);
	DDX_Text(pDX, IDC_COM2SEND, m_com2Send);
	DDX_Text(pDX, IDC_COM2TIMER, m_com2Timer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMultiportTestDlg, CDialog)
	//{{AFX_MSG_MAP(CMultiportTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_COM1HEXDISP, OnCom1HEXdisp)
	ON_BN_CLICKED(IDC_COM1ASCDISP, OnCom1ASCdisp)
	ON_BN_CLICKED(IDC_COM1HEXSEND, OnCom1HEXsend)
	ON_BN_CLICKED(IDC_COM1ASCSEND, OnCom1ASCsend)
	ON_BN_CLICKED(IDC_OPENCOM1, OnOpenCOM1)
	ON_BN_CLICKED(IDC_CLOSECOM1, OnCloseCOM1)
	ON_BN_CLICKED(IDC_REFRESHCOM1DATA, OnRefreshCOM1data)
	ON_BN_CLICKED(IDC_COM1MANUALSEND, OnCom1Manualsend)
	ON_BN_CLICKED(IDC_COM1CLEARSEND, OnCom1Clearsend)
	ON_BN_CLICKED(IDC_COM1AUTOSEND, OnCom1Autosend)
	ON_MESSAGE(WM_COMM_RXCHAR,OnCommunication)
	ON_BN_CLICKED(IDC_OPENCOM2, OnOpenCOM2)
	ON_BN_CLICKED(IDC_REFRESHCOM2DATA, OnRefreshCOM2data)
	ON_BN_CLICKED(IDC_CLOSECOM2, OnCloseCOM2)
	ON_BN_CLICKED(IDC_COM2MANUALSEND, OnCom2Manualsend)
	ON_BN_CLICKED(IDC_COM2CLEARSEND, OnCom2Clearsend)
	ON_BN_CLICKED(IDC_COM2AUTOSEND, OnCom2Autosend)
	ON_BN_CLICKED(IDC_COM2HEXDISP, OnCom2HEXdisp)
	ON_BN_CLICKED(IDC_COM2ASCDISP, OnCom2ASCdisp)
	ON_BN_CLICKED(IDC_COM2HEXSEND, OnCom2HEXsend)
	ON_BN_CLICKED(IDC_COM2ASCSEND, OnCom2ASCsend)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultiportTestDlg message handlers

BOOL CMultiportTestDlg::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_serialCom1.InitPort(this,1,9600,'N',8,1);//初始化串口1
	m_serialCom1.StartMonitoring();
	m_serialCom2.InitPort(this,2,9600,'N',8,1);//初始化串口2
	m_serialCom2.StartMonitoring();

	m_btnOpencom1.EnableWindow(false);
	m_com1HEXdisp.SetCheck(1);
	m_com1HEXsend.SetCheck(1);
	m_btnOpencom2.EnableWindow(false);
	m_com2HEXdisp.SetCheck(1);
	m_com2HEXsend.SetCheck(1);

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

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

void CMultiportTestDlg::OnCom1HEXdisp() 
{
	// TODO: Add your control notification handler code here
	icom1ReceiveRadio=0;
	m_com1ASCdisp.SetCheck(0);
}

void CMultiportTestDlg::OnCom1ASCdisp() 
{
	// TODO: Add your control notification handler code here
	icom1ReceiveRadio=1;
	m_com1HEXdisp.SetCheck(0);
}

void CMultiportTestDlg::OnCom1HEXsend() 
{
	// TODO: Add your control notification handler code here
	icom1SendRadio=0;
	m_com1ASCsend.SetCheck(0);
	m_com1Send=_T("fe ff 12 0a 0d 1b");
	UpdateData(false);
}

void CMultiportTestDlg::OnCom1ASCsend() 
{
	// TODO: Add your control notification handler code here
	icom1SendRadio=1;
	m_com1HEXsend.SetCheck(0);
	m_com1Send=_T("0123456789abcdefg");
	UpdateData(false);
}

void CMultiportTestDlg::OnOpenCOM1() 
{
	// TODO: Add your control notification handler code here
	m_serialCom1.StartMonitoring();
	m_btnOpencom1.EnableWindow(false);
	m_btnClosecom1.EnableWindow(true);
}

void CMultiportTestDlg::OnCloseCOM1() 
{
	// TODO: Add your control notification handler code here
	m_serialCom1.StopMonitoring();
	m_btnOpencom1.EnableWindow(true);
	m_btnClosecom1.EnableWindow(false);
}

void CMultiportTestDlg::OnRefreshCOM1data() 
{
	// TODO: Add your control notification handler code here
	m_com1Receive=_T("");
	UpdateData(false);
}

void CMultiportTestDlg::OnCom1Manualsend() 
{
	// TODO: Add your control notification handler code here
	char *string1;
	UpdateData(true);
	string1=(LPSTR)(LPCTSTR)m_com1Send;
	m_serialCom1.WriteToPort(string1);
}

void CMultiportTestDlg::OnCom1Clearsend() 
{
	// TODO: Add your control notification handler code here
	m_com1Send=_T("");
	UpdateData(false);
}

void CMultiportTestDlg::OnCom1Autosend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	bcom1Send=!bcom1Send;
	if(bcom1Send)
	{
		m_btnCom1Autosend.SetWindowText("停止发送");
		m_btnCom1Manualsend.EnableWindow(false);
		SetTimer(1,m_com1Timer,NULL);
	}
	else
	{
		m_btnCom1Autosend.SetWindowText("自动发送");
		m_btnCom1Manualsend.EnableWindow(true);
		KillTimer(1);
	}
}

void CMultiportTestDlg::OnOpenCOM2() 
{
	// TODO: Add your control notification handler code here
	m_serialCom2.StartMonitoring();
	m_btnOpencom2.EnableWindow(false);
	m_btnClosecom2.EnableWindow(true);
}

void CMultiportTestDlg::OnRefreshCOM2data() 
{
	// TODO: Add your control notification handler code here
	m_com2Receive=_T("");
	UpdateData(false);
}

void CMultiportTestDlg::OnCloseCOM2() 
{
	// TODO: Add your control notification handler code here
	m_serialCom2.StopMonitoring();
	m_btnOpencom2.EnableWindow(true);
	m_btnClosecom2.EnableWindow(false);
}

void CMultiportTestDlg::OnCom2Manualsend() 
{
	// TODO: Add your control notification handler code here
	char *string2;
	UpdateData(true);
	string2=(LPSTR)(LPCTSTR)m_com2Send;
	m_serialCom2.WriteToPort(string2);
}

void CMultiportTestDlg::OnCom2Clearsend() 
{
	// TODO: Add your control notification handler code here
	m_com2Send=_T("");
	UpdateData(false);
}

void CMultiportTestDlg::OnCom2Autosend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	bcom2Send=!bcom2Send;
	if(bcom2Send)
	{
		m_btnCom2Autosend.SetWindowText("停止发送");
		m_btnCom2Manualsend.EnableWindow(false);
		SetTimer(2,m_com2Timer,NULL);
	}
	else
	{
		m_btnCom2Autosend.SetWindowText("自动发送");
		m_btnCom2Manualsend.EnableWindow(true);
		KillTimer(2);
	}
}
LONG CMultiportTestDlg::OnCommunication(WPARAM ch,LPARAM port)
{
	//Add your communication code here
	CString strTemp1;
	CString strTemp2;
	switch((int)port)
	{
	case 1:
		if(icom1ReceiveRadio==0)
			strTemp1.Format("%x ",ch);
		if(icom1ReceiveRadio==1)
			strTemp1.Format("%c",ch);
		m_com1Receive+=strTemp1;
		UpdateData(false);
		break;
	case 2:
		if(icom2ReceiveRadio==0)
			strTemp2.Format("%x ",ch);
		if(icom2ReceiveRadio==1)
			strTemp2.Format("%c",ch);
		m_com2Receive+=strTemp2;
		UpdateData(false);
		break;
	}

	return 0;
}

void CMultiportTestDlg::OnCom2HEXdisp() 
{
	// TODO: Add your control notification handler code here
	icom2ReceiveRadio=1;
	m_com2ASCdisp.SetCheck(0);
}

void CMultiportTestDlg::OnCom2ASCdisp() 
{
	// TODO: Add your control notification handler code here
	icom2ReceiveRadio=1;
	m_com2HEXdisp.SetCheck(0);
}

void CMultiportTestDlg::OnCom2HEXsend() 
{
	// TODO: Add your control notification handler code here
	icom2SendRadio=0;
	m_com2ASCsend.SetCheck(0);
	m_com2Send=_T("fe ff 12 0a 0d 1b");
	UpdateData(false);
}

void CMultiportTestDlg::OnCom2ASCsend() 
{
	// TODO: Add your control notification handler code here
	icom2SendRadio=1;
	m_com2HEXsend.SetCheck(0);
	m_com2Send=_T("0123456789abcdefg");
	UpdateData(false);
}

void CMultiportTestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nIDEvent)
	{
	case 1:
		OnCom1Manualsend();
		break;
	case 2:
		OnCom2Manualsend();
		break;
	}

	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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