📄 commtestdlg.cpp
字号:
/*
** FILENAME CommTestDlg.cpp
**
** PURPOSE This is the dialog that shows the comm activity.
**
** CREATION DATE 15-09-1997
** LAST MODIFICATION 12-11-1997
**
** AUTHOR Remon Spekreijse
**
*/
#include "stdafx.h"
#include "commtest.h"
#include "commtestDlg.h"
#include "configdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CCommtestDlg::CCommtestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCommtestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCommtestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCommtestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCommtestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCommtestDlg, CDialog)
//{{AFX_MSG_MAP(CCommtestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnSendButton1)
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_BN_CLICKED(IDC_BUTTON7, OnClearButton1)
ON_BN_CLICKED(IDC_CONFIGBUTTON1, OnConfigbutton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CCommtestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
m_ListBox.SubclassDlgItem(IDC_LIST1, this);
m_Edit.SubclassDlgItem(IDC_EDIT1, this);
// init the ports
if (m_Ports.InitPort(this, 1, 19200))
{ m_Ports.StartMonitoring();
Sleep(2000);
InitModem();}
else
{
// port not found
m_Edit.SetWindowText("NOT FOUND");
m_Edit.EnableWindow(FALSE);
m_ListBox.EnableWindow(FALSE);
m_ListBox.AddString("串口初始化失败");
m_ListBox.SetSel(m_ListBox.GetCount()-1, TRUE);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CCommtestDlg::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();
}
}
HCURSOR CCommtestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCommtestDlg::OnSendButton1()
{
char buf[100];
memset(&buf, 0, sizeof(buf));
GetDlgItemText(IDC_EDIT1, buf, sizeof(buf));
if (strcmp("NOT FOUND", buf) == 0)
return;
m_Ports.WriteToPort(buf);
}
LONG CCommtestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
if (port <= 0 || port > 4)
return -1;
if (ch == 13 && !m_strReceived.IsEmpty() )
{
m_ListBox.AddString(m_strReceived);
m_ListBox.SetSel(m_ListBox.GetCount()-1, TRUE);
(m_strReceived).Empty();
}
else
{
m_strReceived += (char)ch;
}
return 0;
}
void CCommtestDlg::OnClearButton1()
{
m_ListBox.ResetContent();
}
void CCommtestDlg::OnConfigbutton1()
{
CConfigDlg* dlg = new CConfigDlg(this, m_Ports.GetDCB());
dlg->m_strSendBuffer.Format("%d", m_Ports.GetWriteBufferSize());
DWORD dwCommEvents = m_Ports.GetCommEvents();
dlg->m_CommBreakDetected = (dwCommEvents & EV_BREAK) > 0 ? TRUE : FALSE;
dlg->m_CommCTSDetected = (dwCommEvents & EV_CTS) > 0 ? TRUE : FALSE;
dlg->m_CommDSRDetected = (dwCommEvents & EV_DSR) > 0 ? TRUE : FALSE;
dlg->m_CommERRDetected = (dwCommEvents & EV_ERR) > 0 ? TRUE : FALSE;
dlg->m_CommRingDetected = (dwCommEvents & EV_RING) > 0 ? TRUE : FALSE;
dlg->m_CommRLSDDetected = (dwCommEvents & EV_RLSD) > 0 ? TRUE : FALSE;
dlg->m_CommRxchar = (dwCommEvents & EV_RXCHAR) > 0 ? TRUE : FALSE;
dlg->m_CommRxcharFlag = (dwCommEvents & EV_RXFLAG) > 0 ? TRUE : FALSE;
dlg->m_CommTXEmpty = (dwCommEvents & EV_TXEMPTY) > 0 ? TRUE : FALSE;
if (dlg->DoModal() == IDOK)
{
DWORD dwCommEvents = 0;
if (dlg->m_CommBreakDetected)
dwCommEvents |= EV_BREAK;
if (dlg->m_CommCTSDetected)
dwCommEvents |= EV_CTS;
if (dlg->m_CommDSRDetected)
dwCommEvents |= EV_DSR;
if (dlg->m_CommERRDetected)
dwCommEvents |= EV_ERR;
if (dlg->m_CommRingDetected)
dwCommEvents |= EV_RING;
if (dlg->m_CommRLSDDetected)
dwCommEvents |= EV_RLSD;
if (dlg->m_CommRxchar)
dwCommEvents |= EV_RXCHAR;
if (dlg->m_CommRxcharFlag)
dwCommEvents |= EV_RXFLAG;
if (dlg->m_CommTXEmpty)
dwCommEvents |= EV_TXEMPTY;
m_Ports.InitPort(this, 1,
atoi(dlg->m_strBaudRate),
dlg->m_strParity[0],
atoi(dlg->m_strDataBits),
atoi(dlg->m_strStopBits),
dwCommEvents,
atoi(dlg->m_strSendBuffer));
m_Ports.StartMonitoring();
}
delete dlg;
}
void CCommtestDlg::InitModem()
{
m_Ports.WriteToPort("AT\r");
}
void CCommtestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
InitModem();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -