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

📄 comtestdlg.cpp

📁 ce4.2下测试通过。 evc写的一个串口收发软件。 用于测试串口通讯能力。 功能有待完善
💻 CPP
字号:
// COMTESTDlg.cpp : implementation file
//

#include "stdafx.h"
#include "COMTEST.h"
#include "COMTESTDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ID_TIMER 1 

/////////////////////////////////////////////////////////////////////////////
// CCOMTESTDlg dialog

CCOMTESTDlg::CCOMTESTDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCOMTESTDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCOMTESTDlg)
	m_outnum = 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_BUTTON1, m_stop);
	DDX_Control(pDX, IDC_SEND, m_send);
	DDX_Control(pDX, IDC_OPEN, m_open);
	DDX_Control(pDX, IDC_CLOSE, m_close);
	DDX_Control(pDX, IDC_EDIT_T, m_edit_t);
	DDX_Control(pDX, IDC_COMBO2, m_baud);
	DDX_Control(pDX, IDC_COMBO1, m_comm);
	DDX_Control(pDX, IDC_SPIN1, m_LSpin);
	DDX_Text(pDX, IDC_OUTNUM, m_outnum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCOMTESTDlg, CDialog)
	//{{AFX_MSG_MAP(CCOMTESTDlg)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_EN_CHANGE(IDC_OUTNUM, OnChangeOutnum)
	ON_WM_PAINT()
	ON_EN_UPDATE(IDC_OUTNUM, OnUpdateOutnum)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCOMTESTDlg message handlers

BOOL CCOMTESTDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	m_LSpin.SetRange(0,60);
	SetDlgItemText(IDC_EDIT_OUT, _T("123HELLO"));

    m_comm.SetCurSel(0);
	m_baud.SetCurSel(0);
	SetDlgItemText(IDC_EDIT_T, _T("0"));

	m_close.EnableWindow(FALSE);
	m_send.EnableWindow(FALSE);
	m_stop.EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CCOMTESTDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	BOOL berr;
	DWORD dwThreadID;

	CString temp_com;
	CString temp_baud;


	m_comm.GetLBText(m_comm.GetCurSel(),temp_com);
	m_baud.GetLBText(m_baud.GetCurSel(),temp_baud);
	m_time = GetDlgItemInt(IDC_EDIT_T, &berr, FALSE); 

	m_hComm = CreateFile(	temp_com,
							GENERIC_READ | GENERIC_WRITE,
							0,
							NULL,
							OPEN_EXISTING,
							0,
							NULL
							);

	if (m_hComm == INVALID_HANDLE_VALUE)
	{
		AfxMessageBox(_T("Open COM failed!"));
		m_close.EnableWindow(FALSE);
		return;
	}

	// get comm state
	GetCommState(m_hComm, &m_dcb);

	// then set comm state
	m_dcb.DCBlength = sizeof(DCB);
	m_dcb.BaudRate = _ttol(temp_baud);         //CBR_9600;			// baud rate: 9600
	m_dcb.fBinary = TRUE;
	m_dcb.fParity = FALSE;				// no parity
	m_dcb.fOutxCtsFlow = FALSE;
	m_dcb.fOutxDsrFlow = FALSE;
	m_dcb.fDtrControl = DTR_CONTROL_DISABLE;
	m_dcb.fDsrSensitivity = FALSE;
	m_dcb.fTXContinueOnXoff = TRUE;
	m_dcb.fOutX = FALSE;
	m_dcb.fInX = FALSE;
	m_dcb.fErrorChar = FALSE;					// disable error char replace
	m_dcb.fNull = FALSE;						// disable NULL stipping
	m_dcb.fRtsControl = RTS_CONTROL_DISABLE;
	m_dcb.fAbortOnError = FALSE;				// diable Abort-On-Error
	m_dcb.ByteSize = 8;
	m_dcb.Parity = NOPARITY;
	m_dcb.StopBits = ONESTOPBIT;
	
	if (!SetCommState(m_hComm, &m_dcb))
	{
		AfxMessageBox(_T("set COM paramter failed!"));
		m_close.EnableWindow(FALSE);
		return;
	}

	if (!PurgeComm(m_hComm, PURGE_TXCLEAR | PURGE_RXCLEAR))
	{
		AfxMessageBox(_T("purge in and out buffer of COM failed!"));
		m_close.EnableWindow(FALSE);
		return;
	}
	CreateThread(NULL,0,PortReadThread,this,0,&dwThreadID);
	

	m_open.EnableWindow(FALSE);
	m_close.EnableWindow(TRUE);
	m_edit_t.EnableWindow(FALSE);
	m_comm.EnableWindow(FALSE);
	m_baud.EnableWindow(FALSE);
	m_send.EnableWindow(TRUE);
	m_stop.EnableWindow(FALSE);
}

void CCOMTESTDlg::OnClose() 
{
	// TODO: Add your control notification handler code here
	if (!CloseHandle(m_hComm))
	{
		AfxMessageBox(_T("close COM failed!"));
	}

	m_open.EnableWindow(TRUE);
	m_close.EnableWindow(FALSE);
	m_send.EnableWindow(FALSE);
	m_edit_t.EnableWindow(TRUE);
	m_comm.EnableWindow(TRUE);
	m_baud.EnableWindow(TRUE);
	m_stop.EnableWindow(FALSE);
}

void CCOMTESTDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	CHAR outbox[1024];
	unsigned long n;
	unsigned long nWR;

	// clear in and out box
	for (n=0; n<10; n++)
	{
		outbox[n]	= 0x55;
	}

	if(m_time==0)
	{
		if (n != 0)
		{
			WriteFile(m_hComm, outbox, n, &nWR, NULL);
			ASSERT(n == nWR);
		}
	}
	else
	{
		m_time=m_time*1000;
		SetTimer(1,m_time,NULL);
		m_send.EnableWindow(FALSE);
		m_stop.EnableWindow(TRUE);
	}
	
}

void CCOMTESTDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	/*CHAR outbox[1024];
	unsigned long n;
	unsigned long nWR;

	// clear in and out box
	for (n=0; n<10; n++)
	{
		outbox[n]	= 0x55;
	}
	if (n != 0)
	{
		WriteFile(m_hComm, outbox, n, &nWR, NULL);
		ASSERT(n == nWR);
	}*/
	UpdateData (false);
	CDialog::OnTimer(nIDEvent);
}

void CCOMTESTDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1); 
	m_send.EnableWindow(TRUE);
}


	


void CCOMTESTDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData (false);
}

void CCOMTESTDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	m_outnum = 0;
	UpdateData (false);
}


void CCOMTESTDlg::OnChangeOutnum() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData (false);UpdateData (true);
}

void CCOMTESTDlg::OnUpdateOutnum() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	UpdateData (false);UpdateData (true);
}

⌨️ 快捷键说明

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