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

📄 s.cpp

📁 刚学RS232时,仿串口测试工具界面,做的一个串口测试小软件.
💻 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("关闭串口")
#define STOPVIEW _T("停止显示")
#define CONVIEW _T("继续显示")

/////////////////////////////////////////////////////////////////////////////
// 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_count = 0;
	//}}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_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);
	//}}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)
	//}}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_bflag = TRUE;
	// init 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

	// init check.
	m_clearIn = TRUE;

	// init Edit.
	m_savePath = SAVEPATH;
	m_cycle = 1000;

	// Set com;
	m_com.SetWnd(this->m_hWnd);		
	if ( !m_com.Open(1) )
		::AfxMessageBox( _T("串口打开失败") );
	m_com.SetState();				// Set default com connect.

	if ( !m_com.GetThread() )
		::AfxMessageBox( _T("线程打开失败") );
	if (m_com.IsThreadRunning() )
		::AfxMessageBox( _T("aaaa") );

	m_portSet.SetWindowText( CLOSEPORT );
	// init the StatusBar.
	m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW, CRect(0,0,0,0), this, 0);
	int strPartDim[3]= {300, 400, 500}; //分割数量
	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);

	UpdateData(FALSE);
	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)
{
	        m_in.SetSel(-1, -1);       //自动滚屏(其中m_Edit是EDIT筐所关联的EDIT控制对象)
 

	UpdateData();
	ASSERT(m_count != -1);
	char buf[1024];
	if (m_bflag)
	{
		if (m_clearIn && m_count > 512)
		{
			m_in.Clear();
			m_count = 0;
		}

		int nCount = m_com.Read(buf,1024);  // read the data.
		buf[nCount] = _T('\0');	
		m_count += nCount;					// add the count.

		CString strTemp = _T("");



		if (m_16in)
		{
			for (int i = 0; i < nCount; i++)
			{
				strTemp.Format(_T("%02X"), buf[i]);
				strTemp += _T(' ');
//				if (!(i % 120))
//					m_in += _T("\r\n");

			}
		}
		else
			strTemp = buf;
		m_in.ReplaceSel(strTemp+"\r\n");    //自动换行


		strTemp.Format("%d", m_count);
//		m_StatusBar.SetText( _T("RX: ") + strTemp, 1, 0);
		UpdateData(false);
	}
	return 0;
}

void CComTestDlg::OnButtonStopview() 
{
	// TODO: Add your control notification handler code here
	if (m_bflag)
	{
		m_bflag = FALSE;
		m_view.SetWindowText( CONVIEW );	// Set button text.
	}
	else
	{
		m_bflag = TRUE;
		m_view.SetWindowText( STOPVIEW );	// Set button text.
	}
}

void CComTestDlg::OnButtonPort() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if ( m_com.IsOpen() )
	{	// close com
		m_com.Close();
		m_portSet.SetWindowText( OPENPORT );	// Set button text.
		// Reset StatusBar.
		m_StatusBar.SetText( _T("COM CLOSED"), 0, 0 );
		m_StatusBar.SetText( _T("RX: 0"), 1, 0 );
		m_StatusBar.SetText( _T("TX: 0"), 2, 0 );
	}
	else
	{
		int port = m_port.GetCurSel();
		if ( !m_com.Open(++port) )
		{	// Open com port.
			::AfxMessageBox( _T("串口打开失败.\n") );
			return;
		}
		
		TCHAR* s[] = { _T("COM1"), _T("COM2"), _T("COM3"), _T("COM4"), };
		DWORD baudRate[] = { 4800, 9600, 19200, 115200, };
		DWORD byetSize[] = { 8, 7, 6, };
		DWORD parity[] = { NOPARITY, ODDPARITY, EVENPARITY, };
		TCHAR* szParity[] = { _T("N"), _T("O"), _T("E"), };
		DWORD stopBits[] = { ONESTOPBIT, TWOSTOPBITS, };

		if ( !m_com.SetState(baudRate[m_baudRate.GetCurSel()],
							byetSize[m_byetSize.GetCurSel()],
							parity[m_parity.GetCurSel()],
							stopBits[m_stopBits.GetCurSel()]) )
		{
			::AfxMessageBox( _T(" 串口设置失败") );
			return;
		}
		m_portSet.SetWindowText( CLOSEPORT );	// Set button text.

		// Reset StatusBar.
		CString str = _T("STATUS: ");
		str += s[m_port.GetCurSel()];
		str += _T(" OPENED, ");

		CString strTemp;
		strTemp.Format( _T("%d, "), baudRate[m_baudRate.GetCurSel()]);
		str += strTemp;
		str += szParity[m_parity.GetCurSel()];
		strTemp.Format( _T(", %d, "), byetSize[m_byetSize.GetCurSel()]);
		str += strTemp;
		strTemp.Format( _T("%d"), stopBits[m_stopBits.GetCurSel()]);
		str += strTemp; // make "STATUS: COM1 OPENED, 9600, N, 8, 1"

		m_StatusBar.SetText(str, 0, 0);
		m_StatusBar.SetText(_T("RX: 0"), 1, 0);
		m_StatusBar.SetText(_T("TX: 0"), 2, 0);
	}
}

void CComTestDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_in.Clear();
	UpdateData(FALSE);
}

void CComTestDlg::OnButtonSave() 
{
	// TODO: Add your control notification handler code here
	
}

void CComTestDlg::OnButtonSavepath() 
{
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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