?? comtestdlg.cpp
字號:
// ComTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ComTest.h"
#include "cnComm.h"
#include "ComTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SAVEPATH _T("C:\\COMDATA")
#define OPENPORT _T("打開串口")
#define CLOSEPORT _T("關(guān)閉串口")
#define STOPVIEW _T("停止顯示")
#define CONVIEW _T("繼續(xù)顯示")
#define SUFFIX _T("\\*.txt")
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CComTestDlg dialog
CComTestDlg::CComTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CComTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CComTestDlg)
m_16in = FALSE;
m_savePath = _T("");
m_clearIn = FALSE;
m_cycle = 0;
m_out = _T("");
m_autoSend = FALSE;
m_RxCount = 0;
m_TxCount = 0;
m_16out = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CComTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CComTestDlg)
DDX_Control(pDX, IDC_EDIT_SENDPAHT, m_sendPath);
DDX_Control(pDX, IDC_EDIT_IN, m_in);
DDX_Control(pDX, IDC_BUTTON_STOPVIEW, m_view);
DDX_Control(pDX, IDC_BUTTON_PORT, m_portSet);
DDX_Control(pDX, IDC_COMBO_STOPBITS, m_stopBits);
DDX_Control(pDX, IDC_COMBO_PORT, m_port);
DDX_Control(pDX, IDC_COMBO_BYTESIZE, m_byetSize);
DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_baudRate);
DDX_Control(pDX, IDC_COMBO_PARITY, m_parity);
DDX_Check(pDX, IDC_CHECK_16IN, m_16in);
DDX_Text(pDX, IDC_EDIT_SAVEPATH, m_savePath);
DDX_Check(pDX, IDC_CHECK_CLEARIN, m_clearIn);
DDX_Text(pDX, IDC_EDIT_CYCLE, m_cycle);
DDX_Text(pDX, IDC_EDIT_OUT, m_out);
DDX_Check(pDX, IDC_CHECK_AUTOSEND, m_autoSend);
DDX_Check(pDX, IDC_CHECK_16OUT, m_16out);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CComTestDlg, CDialog)
//{{AFX_MSG_MAP(CComTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_STOPVIEW, OnButtonStopview)
ON_MESSAGE(WM_COM_RECEIVE, OnComRecv)
ON_BN_CLICKED(IDC_BUTTON_PORT, OnButtonPort)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_SAVEPATH, OnButtonSavepath)
ON_BN_CLICKED(IDC_BUTTON_CLEAROUT, OnButtonClearout)
ON_BN_CLICKED(IDC_CHECK_AUTOSEND, OnCheckAutosend)
ON_EN_CHANGE(IDC_EDIT_OUT, OnChangeEditOut)
ON_EN_CHANGE(IDC_EDIT_CYCLE, OnChangeEditCycle)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_SELECT, OnButtonSelect)
ON_BN_CLICKED(IDC_BUTTON_SENDFILE, OnButtonSendfile)
ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComTestDlg message handlers
BOOL CComTestDlg::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_com.SetWnd(this->m_hWnd);
if ( !m_com.Open(1) )
::AfxMessageBox( _T("串口打開失敗") );
m_portSet.SetWindowText( CLOSEPORT );
// 初始化狀態(tài)欄.
m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW, CRect(0,0,0,0), this, AFX_IDW_STATUS_BAR);
int strPartDim[3]= {400, 550, 800}; //分割數(shù)量
m_StatusBar.SetParts(3, strPartDim);
//設(shè)置狀態(tài)欄文本
m_StatusBar.SetText("STATUS: COM1 OPENED, 9600, N, 8, 1", 0, 0);
m_StatusBar.SetText("RX: 0", 1, 0);
m_StatusBar.SetText("TX: 0", 2, 0);
m_bflag = TRUE;
// 初始化各Combo控件
m_port.SetCurSel(0); // "COM1".
m_baudRate.SetCurSel(1); // "9600".
m_parity.SetCurSel(0); // 無校驗位.
m_byetSize.SetCurSel(0); // 數(shù)據(jù)位為8
m_stopBits.SetCurSel(0); // 停止位為1
// 初始化 check 控件.
m_clearIn = TRUE;
// 初始化 Edit 控件.
m_savePath = SAVEPATH;
m_cycle = 1000;
UpdateData(FALSE);
// 創(chuàng)建串口發(fā)送線程并掛起.
m_hThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadFunc, this, CREATE_SUSPENDED, &m_threadID);
return TRUE; // return TRUE unless you set the focus to a control
}
void CComTestDlg::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 CComTestDlg::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 CComTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LRESULT CComTestDlg::OnComRecv(WPARAM wParam, LPARAM lParam)
{
ASSERT( m_RxCount != -1);
char buf[1024];
if (m_bflag)
{
m_in.SetSel(-1, -1);
UpdateData(); // 實現(xiàn)自動滾屏.
if (m_clearIn && m_RxCount > 512)
{
m_in.SetSel(0, -1);
m_in.Clear(); // 清空接收區(qū).
m_RxCount = 0; // 計數(shù)器清零.
}
int nCount = m_com.Read(buf,1024); // 讀取數(shù)據(jù).
buf[nCount] = _T('\0');
m_RxCount += nCount; // 得到總的接收個數(shù).
CString strTemp = _T("");
CString str = _T("");
if (m_16in)
{
for (int i = 0; i < nCount; i++) // 把收到的字節(jié)轉(zhuǎn)換
{ // 為16進(jìn)制的形式.
strTemp.Format(_T("%02X"), buf[i]);
str += strTemp + _T(' ');
}
m_in.ReplaceSel("\n\r");
}
else
str = buf;
m_in.ReplaceSel(str);
str.Format("%d", m_RxCount);
m_StatusBar.SetText( _T("RX: ") + str, 1, 0);
}
return 0;
}
void CComTestDlg::OnButtonStopview()
{
// TODO: Add your control notification handler code here
if (m_bflag)
{
m_bflag = FALSE;
m_view.SetWindowText( CONVIEW ); // 改變按紐控件名.
}
else
{
m_bflag = TRUE;
m_view.SetWindowText( STOPVIEW ); // 改變按紐控件名.
}
}
void CComTestDlg::OnButtonPort()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if ( m_com.IsOpen() )
{ // 如果串口打開的話關(guān)閉串口.
m_com.Close();
m_portSet.SetWindowText( OPENPORT ); // 改變按紐控件名.
// 重置狀態(tài)欄.
m_StatusBar.SetText( _T("COM CLOSED"), 0, 0 );
m_StatusBar.SetText( _T("RX: 0"), 1, 0 );
m_StatusBar.SetText( _T("TX: 0"), 2, 0 );
m_RxCount = 0; // 重置計數(shù)器.
m_TxCount = 0; // 重置計數(shù)器.
}
else
{
int port = m_port.GetCurSel();
if ( !m_com.Open(++port) )
{ // 打開串口.
::AfxMessageBox( _T("串口打開失敗.\n") );
return;
}
TCHAR* s[] = { _T("COM1"), _T("COM2"), _T("COM3"), _T("COM4"), };
DWORD baudRate[] = { 4800, 9600, 19200, 115200, };
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -