📄 serial_communicationdlg.cpp
字号:
// Serial_CommunicationDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Serial_Communication.h"
#include "Serial_CommunicationDlg.h"
//#pragma comment( lib, "Ws2_32.lib" );
#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()
/////////////////////////////////////////////////////////////////////////////
// CSerial_CommunicationDlg dialog
CSerial_CommunicationDlg::CSerial_CommunicationDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerial_CommunicationDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerial_CommunicationDlg)
m_Check_Send_Hex = FALSE;
m_Check_Send_Auto = FALSE;
m_Check_Receive_Hex = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSerial_CommunicationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerial_CommunicationDlg)
DDX_Control(pDX, IDC_BUTTON_Switch, m_Button_Switch);
DDX_Control(pDX, IDC_COMBO_Check, m_Combo_Check);
DDX_Control(pDX, IDC_COMBO_Stop, m_Combo_Stop);
DDX_Control(pDX, IDC_COMBO_Data, m_Combo_Data);
DDX_Control(pDX, IDC_COMBO_Baud, m_Combo_Baud);
DDX_Control(pDX, IDC_COMBO_Port, m_Combo_Port);
DDX_Control(pDX, IDC_EDIT_Send_Time, m_Edit_Send_Time);
DDX_Control(pDX, IDC_EDIT_Receive, m_Edit_Receive);
DDX_Control(pDX, IDC_EDIT_Send, m_Edit_Send);
DDX_Check(pDX, IDC_CHECK_Send_Hex, m_Check_Send_Hex);
DDX_Check(pDX, IDC_CHECK_Send_Auto, m_Check_Send_Auto);
DDX_Check(pDX, IDC_CHECK_Receive_Hex, m_Check_Receive_Hex);
DDX_Control(pDX, IDC_MSCOMM1, m_CMSComm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerial_CommunicationDlg, CDialog)
//{{AFX_MSG_MAP(CSerial_CommunicationDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_End, OnBUTTONEnd)
ON_BN_CLICKED(IDC_BUTTON_Send_Clear, OnBUTTONSendClear)
ON_BN_CLICKED(IDC_BUTTON_Reveive_Clear, OnBUTTONReveiveClear)
ON_BN_CLICKED(IDC_CHECK_Send_Hex, OnCHECKSendHex)
ON_BN_CLICKED(IDC_CHECK_Send_Auto, OnCHECKSendAuto)
ON_BN_CLICKED(IDC_CHECK_Receive_Hex, OnCHECKReceiveHex)
ON_BN_CLICKED(IDC_BUTTON_Switch, OnBUTTONSwitch)
ON_BN_CLICKED(IDC_BUTTON_Save_Data, OnBUTTONSaveData)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerial_CommunicationDlg message handlers
BOOL CSerial_CommunicationDlg::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
//////////
//My code starts here
//////////
m_Combo_Port.SelectString(-1,"COM1");
m_Combo_Baud.SelectString(-1,"1200");
m_Combo_Data.SelectString(-1,"8");
m_Combo_Stop.SelectString(-1,"1");
m_Combo_Check.SelectString(-1,"NONE");
UpdateData(FALSE);
//////////
//My code ends here
/////////
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerial_CommunicationDlg::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 CSerial_CommunicationDlg::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 CSerial_CommunicationDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerial_CommunicationDlg::OnBUTTONEnd()
{
// TODO: Add your control notification handler code here
//////////
//My code starts here
//////////
OnOK();
//////////
//My code ends here
//////////
}
void CSerial_CommunicationDlg::OnBUTTONSendClear()
{
// TODO: Add your control notification handler code here
//////////
//My code starts here
//////////
m_Edit_Send.SetSel(0,-1);
m_Edit_Send.ReplaceSel("");
//////////
//My code ends here
//////////
}
void CSerial_CommunicationDlg::OnBUTTONReveiveClear()
{
// TODO: Add your control notification handler code here
//////////
//My code starts here
//////////
m_Edit_Receive.SetSel(0,-1);
m_Edit_Receive.ReplaceSel("");
//////////
//My code ends here
//////////
}
void CSerial_CommunicationDlg::OnCHECKSendHex()
{
// TODO: Add your control notification handler code here
}
void CSerial_CommunicationDlg::OnCHECKSendAuto()
{
// TODO: Add your control notification handler code here
///////////
//My code starts here
//////////
UpdateData(TRUE);
if(m_Check_Send_Auto==TRUE)
GetDlgItem(IDC_EDIT_Send_Time)->EnableWindow(TRUE);
else
GetDlgItem(IDC_EDIT_Send_Time)->EnableWindow(FALSE);
//////////
//My code ends here
//////////
}
void CSerial_CommunicationDlg::OnCHECKReceiveHex()
{
// TODO: Add your control notification handler code here
}
void CSerial_CommunicationDlg::OnBUTTONSwitch()
{
// TODO: Add your control notification handler code here
//////////
//My code starts here
//////////
CString theName;
GetDlgItemText(IDC_BUTTON_Switch, theName);
if(theName=="打开串口")
{
theName="关闭串口";
HANDLE hCom; //打开串口,调用CreateFile()函数
hCom=CreateFile("COM1",
GENERIC_READ|GENERIC_WRITE,//读写
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, //异步方式
NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("打开串口失败!");
//return FALSE;
}
else
{
AfxMessageBox("端口COM1打开!");
}
//return TRUE;
}
else
{
theName="打开串口";
//CloseHandle(hCom);
}
SetDlgItemText(IDC_BUTTON_Switch, theName);
//////////
//My code ends here
//////////
}
void CSerial_CommunicationDlg::OnBUTTONSaveData()
{
// TODO: Add your control notification handler code here
//////////
//My code starts here
//////////
UpdateData(TRUE);
CFile f;
f.Open("C:\\Downloads\\filedata.txt",CFile::modeCreate|CFile::modeWrite);
CArchive ar(&f,CArchive::store);
CString Receive;
GetDlgItemText(IDC_EDIT_Send,Receive);//还需要改,改为接收区的数据保存
ar<<Receive;
ar.Close();
f.Close();
//////////
//My code ends here
//////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -