📄 mscommdlg.cpp
字号:
// MSCommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MSComm.h"
#include "MSCommDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMSCommDlg dialog
CMSCommDlg::CMSCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMSCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMSCommDlg)
m_Port = 0;
m_Receive = _T("");
m_Send = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMSCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMSCommDlg)
DDX_CBIndex(pDX, IDC_COMBOLIST, m_Port);
DDX_Text(pDX, IDC_RECEIVEDATA, m_Receive);
DDX_Text(pDX, IDC_SENDDATA, m_Send);
DDX_Control(pDX, IDC_MSCOMM, m_Comm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMSCommDlg, CDialog)
//{{AFX_MSG_MAP(CMSCommDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEND, OnSend)
ON_BN_CLICKED(IDC_OPENPORT, OnOpenport)
ON_CBN_SELCHANGE(IDC_COMBOLIST, OnSelchangeCombolist)
ON_BN_CLICKED(IDC_CLOSTPORT, OnClostport)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMSCommDlg message handlers
BOOL CMSCommDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CMSCommDlg::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 CMSCommDlg::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 CMSCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMSCommDlg::InitComm()
{
//设置串口
m_Comm.SetCommPort(m_Port+1);
// 设置数据读取格式为二进制方式
m_Comm.SetInputMode(1);
// 设置传输参数
m_Comm.SetSettings("9600,n,8,1");
//缓冲区内有一个字符就可以接收
m_Comm.SetRThreshold(1);
// 指定接收缓冲区大小
m_Comm.SetInBufferSize(1024);
//指定接收缓冲区大小
m_Comm.SetOutBufferSize(1024);
// 清空接收缓冲区
m_Comm.SetInBufferCount(0);
// 打开串口
if(!m_Comm.GetPortOpen())
{
m_Comm.SetPortOpen(TRUE);
}
}
BEGIN_EVENTSINK_MAP(CMSCommDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMSCommDlg)
ON_EVENT(CMSCommDlg, IDC_MSCOMM, 1 /* OnComm */, OnOnCommMscomm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMSCommDlg::OnOnCommMscomm()
{
// TODO: Add your control notification handler code here
int nEvent;
VARIANT m_input;
char *str, *str1;
int k,i;
CString str2;
//得到事件
nEvent = m_Comm.GetCommEvent();
switch(nEvent)
{
//只对接收数据事件进行处理
case 2:
// 接收缓冲区的字符数目
k = m_Comm.GetInBufferCount();
if(k > 0)
{
//得到接收数据
m_input = m_Comm.GetInput();
str = (char*)(unsigned char*)m_input.parray->pvData;
}
//清除字符串中的不必要字符
i = 0;
str1 = str;
while(i < k)
{
i++;
str1++;
}
*str1 ='\0';
str2 = (const char*)str;
//得到最终数据
m_Receive += (LPCTSTR)str2;
default:
break;
}
// 显示数据
UpdateData(FALSE);
}
void CMSCommDlg::OnSend()
{
// TODO: Add your control notification handler code here
//得到发送Edit控件的数据
UpdateData(TRUE);
int Count=m_Send.GetLength();
//将发送数据传给m_Array
CByteArray m_Array;
m_Array.RemoveAll();
m_Array.SetSize(Count);
for(int i=0;i<Count;i++)
m_Array.SetAt(i,m_Send[i]);
//发送
m_Comm.SetOutput(COleVariant(m_Array));
m_Send = "";
UpdateData(FALSE);
}
void CMSCommDlg::OnOpenport()
{
// TODO: Add your control notification handler code here
InitComm();
}
void CMSCommDlg::OnSelchangeCombolist()
{
// TODO: Add your control notification handler code here
CString m_str;
//得到下拉列表内容
GetDlgItemText(IDC_COMBOLIST, m_str);
//得到串口号
m_Port = atoi(LPCTSTR(m_str))-1;
}
void CMSCommDlg::OnClostport()
{
// TODO: Add your control notification handler code here
if( m_Comm.GetPortOpen() )
{
// 关闭串口
m_Comm.SetPortOpen(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -