?? rs232dlg.cpp
字號:
// RS232Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "RS232.h"
#include "RS232Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_RECEIVE WM_USER + 121
/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg dialog
CRS232Dlg::CRS232Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CRS232Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRS232Dlg)
// 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 CRS232Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRS232Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRS232Dlg, CDialog)
//{{AFX_MSG_MAP(CRS232Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_RECEIVE, OnReceiveData)
ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
ON_BN_CLICKED(IDC_BTNCLOSE, OnBtnclose)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BTNSEND, OnBtnsend)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg message handlers
BOOL CRS232Dlg::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
// TODO: Add extra initialization here
CComboBox *cb = (CComboBox *)GetDlgItem(IDC_CBCOMNAME);
cb->SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CRS232Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
// 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 CRS232Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRS232Dlg::OnBtnopen()
{
if (m_Comm.IsActive())
{
m_Comm.Close();
}
CString ComName(_T(""));
CComboBox *cb = (CComboBox *)GetDlgItem(IDC_CBCOMNAME);
int nCom = cb->GetCurSel() + 1;
if (!m_Comm.Open(nCom))
{
CString serr;
serr.Format(_T("Open comm fail : %d"), m_Comm.m_Error);
GetDlgItem(IDC_COMSTAT)->SetWindowText(serr);
return;
}
ComName.Format(_T("COM%d: opened"), nCom);
GetDlgItem(IDC_COMSTAT)->SetWindowText(ComName);
m_Comm.m_OnReceive = OnReceive;
if (!m_Comm.StartAutoRead())
{
AfxMessageBox(_T("Comm autoread fail"));
}
}
void CRS232Dlg::OnReceive(CSerialComm *theCom)
{
int len = theCom->GetInBufferLen();
if (len > 0)
{
char *buf = new char[len];
if (theCom->ReadBuf(buf, len) > 0)
{
CString sRecv(_T("")), stmp(_T(""));
for (int i = 0; i < len; i++)
{
stmp.Format(_T("%02x "), (BYTE)buf[i]);
sRecv += stmp;
stmp = _T("");
}
theCom->SendBuf(buf, len);
::SendMessage(theApp.m_pMainWnd->m_hWnd, WM_RECEIVE, 0, (LPARAM)sRecv.GetBuffer(1));
}
delete []buf;
}
}
HRESULT CRS232Dlg::OnReceiveData(WPARAM wParam, LPARAM lParam)
{
GetDlgItem(IDC_RECV)->SetWindowText((TCHAR *)lParam);
return 0;
}
void CRS232Dlg::OnBtnclose()
{
m_Comm.Close();
}
void CRS232Dlg::OnButton4()
{
EndDialog(IDOK);
}
void CRS232Dlg::OnBtnsend()
{
CString strSnd(_T(""));
GetDlgItem(IDC_SEND)->GetWindowText(strSnd);
int len = strSnd.GetLength();
char *buf = new char[len + 1];
BOOL bUse = FALSE;
WideCharToMultiByte(0, 0, strSnd.GetBuffer(1), len, buf, len, NULL, &bUse);
if (!m_Comm.SendBuf(buf, len))
{
AfxMessageBox(_T("Send fail"));
}
delete []buf;
}
void CRS232Dlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -