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

📄 comsetup.cpp

📁 采用文档类方法实现的一种串口通讯协议。可以借鉴参考
💻 CPP
字号:
// ComSetup.cpp : implementation file
//

#include "stdafx.h"
#include "OperationTool.h"
#include "ComSetup.h"

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

/////////////////////////////////////////////////////////////////////////////
// CComSetup dialog


CComSetup::CComSetup(CWnd* pParent /*=NULL*/)
	: CDialog(CComSetup::IDD, pParent)
{
	//{{AFX_DATA_INIT(CComSetup)
	m_strComBaud = _T("");
	m_strComCheck = _T("");
	m_strComData = _T("");
	m_strComFlow = _T("");
	m_strComStop = _T("");
	m_strComPort = _T("");
	//}}AFX_DATA_INIT	
}


CComSetup::CComSetup(CWnd* pParent , DCB dcb)
	: CDialog(CComSetup::IDD, pParent)
{
	m_dcb = dcb;
}


void CComSetup::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComSetup)
	DDX_Control(pDX, IDC_ComPort, m_cbComPort);
	DDX_Control(pDX, IDC_ComStop, m_cbComStop);
	DDX_Control(pDX, IDC_ComFlow, m_cbComFlow);
	DDX_Control(pDX, IDC_ComData, m_cbComData);
	DDX_Control(pDX, IDC_ComCheck, m_cbComCheck);
	DDX_Control(pDX, IDC_ComBaud, m_cbComBaud);
	DDX_CBString(pDX, IDC_ComBaud, m_strComBaud);
	DDX_CBString(pDX, IDC_ComCheck, m_strComCheck);
	DDX_CBString(pDX, IDC_ComData, m_strComData);
	DDX_CBString(pDX, IDC_ComFlow, m_strComFlow);
	DDX_CBString(pDX, IDC_ComStop, m_strComStop);
	DDX_CBString(pDX, IDC_ComPort, m_strComPort);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CComSetup, CDialog)
	//{{AFX_MSG_MAP(CComSetup)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComSetup message handlers

BOOL CComSetup::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	mf_CountValidComm();

	m_cbComBaud.AddString("2400");		//add baudrate
	m_cbComBaud.AddString("4800");
	m_cbComBaud.AddString("9600");
	m_cbComBaud.AddString("19200");
	m_cbComBaud.AddString("38400");
	m_cbComBaud.AddString("57600");
	m_cbComBaud.AddString("115200");

	m_cbComData.AddString("4");			//add COM data bits
	m_cbComData.AddString("5");
	m_cbComData.AddString("6");
	m_cbComData.AddString("7");
	m_cbComData.AddString("8");

	m_cbComStop.AddString("1");			//add COM stop bits
	m_cbComStop.AddString("1.5");
	m_cbComStop.AddString("2");

	m_cbComCheck.AddString("None");		//add COM parity
	m_cbComCheck.AddString("Odd");
	m_cbComCheck.AddString("Even");

	m_cbComFlow.AddString("None");		//add COM flow control
	m_cbComFlow.AddString("Hardware");
	m_cbComFlow.AddString("Xon/Xoff");

	CString strTemp;
		
	strTemp.Format("%d", m_dcb.BaudRate);
	m_cbComBaud.SelectString(0, strTemp);	//select using baud rate

	m_cbComCheck.SetCurSel(m_dcb.Parity);	//select using parity

	m_cbComStop.SetCurSel(m_dcb.StopBits);	//select using stop bits

	strTemp.Format("%d", m_dcb.ByteSize);	
	m_cbComData.SelectString(0, strTemp);	//select using Data bits

	m_cbComFlow.SetCurSel(0);				//not used

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CComSetup::mf_CountValidComm()
{		
	const int MAX_COMPORT_NUM = 16;

	CString strComm;
	CString	strComPort;
	HANDLE	hComPort = INVALID_HANDLE_VALUE;

	m_cbComPort.ResetContent();
	for (int xyz = 1; xyz < MAX_COMPORT_NUM; xyz++)
	{			
		strComm.Format("COM%d", xyz);
		strComPort.Format("%d", xyz);

		if (strComPort == m_strComPort)
		{			
			m_cbComPort.AddString(strComPort);
			m_cbComPort.SelectString(0, m_strComPort);
			continue;
		}

		hComPort = CreateFile(strComm,						// communication port string (COMX)
							GENERIC_READ | GENERIC_WRITE,	// read/write types
							0,								// comm devices must be opened with exclusive access
							NULL,							// no security attributes
							OPEN_EXISTING,					// comm devices must use OPEN_EXISTING
							FILE_FLAG_OVERLAPPED,			// Async I/O
							0);								// template must be 0 for comm devices

		if (hComPort != INVALID_HANDLE_VALUE)		
		{
			m_cbComPort.AddString(strComPort);
		}

		CloseHandle(hComPort);
		hComPort = INVALID_HANDLE_VALUE;
	}
}

⌨️ 快捷键说明

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