⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comtestdlg.cpp

📁 刚学RS232时,仿串口测试工具界面,做的一个串口测试小软件.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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("关闭串口")
#define STOPVIEW _T("停止显示")
#define CONVIEW _T("继续显示")
#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 );
	// 初始化状态栏.
	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}; //分割数量
	m_StatusBar.SetParts(3, strPartDim);
	//设置状态栏文本
	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);		// 数据位为8
	m_stopBits.SetCurSel(0);		// 停止位为1

	// 初始化 check 控件.
	m_clearIn = TRUE;
	// 初始化 Edit 控件.
	m_savePath = SAVEPATH;
	m_cycle = 1000;
	UpdateData(FALSE);
	// 创建串口发送线程并挂起.
	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();						// 实现自动滚屏.

		if (m_clearIn && m_RxCount > 512)
		{
			m_in.SetSel(0, -1);
			m_in.Clear();					// 清空接收区.
			m_RxCount = 0;					// 计数器清零.
		}

		int nCount = m_com.Read(buf,1024);  // 读取数据.
		buf[nCount] = _T('\0');	
		m_RxCount += nCount;				// 得到总的接收个数.

		CString strTemp = _T("");
		CString str = _T("");

		if (m_16in)
		{		
			for (int i = 0; i < nCount; i++)			// 把收到的字节转换
			{											// 为16进制的形式.
				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() )
	{	// 如果串口打开的话关闭串口.
		m_com.Close();
		m_portSet.SetWindowText( OPENPORT );	// 改变按纽控件名.
		// 重置状态栏.
		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;				// 重置计数器.
		m_TxCount = 0;				// 重置计数器.
	}
	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, };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -