📄 serialportdlg.cpp
字号:
// SerialPortDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SerialPort.h"
#include "SerialPortDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortDlg dialog
CSerialPortDlg::CSerialPortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialPortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialPortDlg)
m_strRecDisp = _T("");
m_strSendEdit = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerialPortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialPortDlg)
DDX_Control(pDX, IDC_CLOSE_COM, m_ButClose);
DDX_Control(pDX, IDC_OPEN_COM, m_ButOpen);
DDX_Control(pDX, IDC_PARITY, m_ComboParity);
DDX_Control(pDX, IDC_STOP, m_ComboStop);
DDX_Control(pDX, IDC_DATA, m_ComboData);
DDX_Control(pDX, IDC_PORT, m_ComboPort);
DDX_Control(pDX, IDC_BAUD, m_ComboBaud);
DDX_Text(pDX, IDC_REC_DISP, m_strRecDisp);
DDX_Text(pDX, IDC_SEND_EDIT, m_strSendEdit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialPortDlg, CDialog)
//{{AFX_MSG_MAP(CSerialPortDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_OPEN_COM, OnOpenCom)
ON_BN_CLICKED(IDC_CLOSE_COM, OnCloseCom)
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_CLEAR_SEND, OnClearSend)
ON_BN_CLICKED(IDC_CLEAR_REC, OnClearRec)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialPortDlg message handlers
BOOL CSerialPortDlg::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_ComboBaud.SetCurSel(0); /* 默认波特率为: 115200 */
m_ComboData.SetCurSel(1); /* 默认数据位为: 8位 */
m_ComboParity.SetCurSel(0); /* 默认校验为: 无 */
m_ComboPort.SetCurSel(0); /* 默认端口为: COM1 */
m_ComboStop.SetCurSel(0); /* 默认停止位为: 1位 */
m_ButClose.EnableWindow(FALSE); /* "关闭端口"按键无效 */
m_hComm = INVALID_HANDLE_VALUE; /* 串口操作句柄无效 */
m_ExitThreadEvent = NULL; /* 串口接收线程退出事件无效 */
m_strRecDisp = _T(""); /* 接收显示字符为空 */
UpdateData(FALSE);
return TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialPortDlg::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 CSerialPortDlg::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 CSerialPortDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerialPortDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if (m_ExitThreadEvent != NULL)
{
SetEvent(m_ExitThreadEvent); /* 通知串口接收线程退出 */
Sleep(1000);
CloseHandle(m_ExitThreadEvent); /* 关闭接收线程退出事件句柄 */
m_ExitThreadEvent = NULL;
}
ClosePort(); /* 关闭串口 */
}
/****************************************************************
实 验 代 码
*****************************************************************/
/*******************************************************************************************
函数名称: CSerialPortDlg::OpenPort
描 述: 打开串口
输入参数: LPCTSTR Port: 串口名,如"COM0:","COM1:"
int BaudRate: 波特率
int DataBits: 数据位, 取值为7或8
int StopBits: 停止位
int Parity : 奇偶校验位
输出参数: 无
返 回: FALSE: 失败; TRUE: 成功
********************************************************************************************/
BOOL CSerialPortDlg::OpenPort(LPCTSTR Port, int BaudRate, int DataBits, int StopBits, int Parity)
{
COMMTIMEOUTS CommTimeOuts;
// 打开串口
m_hComm = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(m_hComm == INVALID_HANDLE_VALUE)
{
MessageBox(_T("无法打开端口或端口已打开!请检查是否已被占用."));
return FALSE;
}
GetCommState(m_hComm, &dcb); /* 读取串口的DCB */
dcb.BaudRate = BaudRate;
dcb.ByteSize = DataBits;
dcb.Parity = Parity;
dcb.StopBits = StopBits;
dcb.fParity = FALSE; /* 禁止奇偶校验 */
dcb.fBinary = TRUE;
dcb.fDtrControl = 0; /* 禁止流量控制 */
dcb.fRtsControl = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.fTXContinueOnXoff = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -