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

📄 comtestdlg.cpp

📁 基于三星2440串口的简单测试程序,可以进行接收和发送的检测
💻 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

/////////////////////////////////////////////////////////////////////////////
// CComtestDlg dialog
UINT CommThread(LPVOID lpParam);
UINT CommThread(LPVOID lpParam)
{
	CComtestDlg* p=(CComtestDlg*)lpParam;
	OVERLAPPED ros;
	DWORD dwByteToRead=0;
	DWORD dwError,dwEvMask=0;
	BOOL  bRet;
	COMSTAT comstat;
	char ReadBuff[256];
	while(p->bConnect)
	{
		bRet=WaitCommEvent(p->hComm,&dwEvMask,&p->os);
		if(!bRet)
		{
		}
		else
		{
			ClearCommError(p->hComm,&dwError,&comstat);
			if(comstat.cbInQue==0)	continue;
		}
		WaitForSingleObject(p->os.hEvent,INFINITE);
		ResetEvent(p->os.hEvent);
		GetCommMask(p->hComm,&dwEvMask);
		if(dwEvMask & EV_RXCHAR==EV_RXCHAR)
		{
			ClearCommError(p->hComm,&dwError,&comstat);
			if(comstat.cbInQue==0)	continue;
			memset(&ros,0,sizeof(ros));
			memset(ReadBuff,0,sizeof(ReadBuff));
			ros.hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
			if(!ReadFile(p->hComm,ReadBuff,min(256,comstat.cbInQue),&dwByteToRead,&ros))
			{
				if(ERROR_IO_PENDING==GetLastError())
				{
					//while(!GetOverlappedResult(p->hComm,&ros,&dwByteToRead,TRUE))
					{
						if(ERROR_IO_INCOMPLETE==GetLastError())
						{
							continue;
						}
					}
				}
				else
				{
					dwByteToRead=0;
				}
			}
			CloseHandle(ros.hEvent);
			CString tmp;
			p->GetDlgItemText(IDC_EDIT1,tmp);
			tmp+=ReadBuff;
			p->SetDlgItemText(IDC_EDIT1,tmp);
		}
	}
	return 0L;
}
CComtestDlg::CComtestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CComtestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CComtestDlg)
	m_portno = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	hComm=hThread=NULL;
	memset(&os,0,sizeof(os));
	bConnect=FALSE;
}

void CComtestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComtestDlg)
	DDX_CBIndex(pDX, IDC_COMBO1, m_portno);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CComtestDlg, CDialog)
	//{{AFX_MSG_MAP(CComtestDlg)
	ON_BN_CLICKED(IDC_INIT, OnInitialize)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_DESTROY()
	//}}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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CComtestDlg::OnInitialize() 

{
	// TODO: Add your control notification handler code here
	CString sPort;
	UpdateData();
	sPort.Format(TEXT("COM%d:"),m_portno+1);
	hComm=CreateFile(sPort,GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
	DWORD dwErr=GetLastError();
	
	if(INVALID_HANDLE_VALUE==hComm)
	{
		MessageBox(L"Serial port open failed!",L"Error",MB_ICONSTOP);
		hComm=NULL;
		return;
	}
	
	DCB dcb;
	GetCommState(hComm,&dcb);
	dcb.BaudRate=115200;
	SetCommState(hComm,&dcb);
	PurgeComm(hComm,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_TXABORT);
	SetupComm(hComm,1024,1024);
	os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
	hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)CommThread,this,CREATE_SUSPENDED,&dwThreadID);
	bConnect=TRUE;
	ResumeThread(hThread);
}

void CComtestDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	OVERLAPPED wos;
	DWORD dwByteToWrite;
	TCHAR buff[256]={0};
	memset(&wos,0,sizeof(wos));
	GetDlgItemText(IDC_EDIT1,buff,256);
	wos.hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
	if(!WriteFile(hComm,buff,_tcslen(buff),&dwByteToWrite,&wos))
	{
		if(ERROR_IO_PENDING==GetLastError())
		{
// 			while(!GetOverlappedResult(hComm,&wos,&dwByteToWrite,TRUE))
// 			{
// 				if(ERROR_IO_INCOMPLETE==GetLastError())
// 				{
// 					continue;
// 				}
// 			}
		}
	}
	CloseHandle(wos.hEvent);
}

void CComtestDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	bConnect=FALSE;
	CloseHandle(hThread);
	hThread=NULL;
	CloseHandle(os.hEvent);
	os.hEvent=NULL;
	PurgeComm(hComm,PURGE_TXABORT|PURGE_TXCLEAR|PURGE_RXABORT|PURGE_RXCLEAR);
	CloseHandle(hComm);
	hComm=NULL;
}

⌨️ 快捷键说明

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