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

📄 comchatdlg.cpp

📁 WinCE串口通讯
💻 CPP
字号:
// ComChatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ComOper.h"
#include "ComChat.h"
#include "ComChatDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CComChatDlg dialog

CComChatDlg::CComChatDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CComChatDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CComChatDlg)
	com_Msg = _T("");
	com_hCom = NULL;
	com_Send = _T("");
	com_hThread = NULL;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CComChatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComChatDlg)
	DDX_Text(pDX, IDC_EDIT_MSG, com_Msg);
	DDX_Text(pDX, IDC_EDIT_SEND, com_Send);
	DDV_MaxChars(pDX, com_Send, 200);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CComChatDlg, CDialog)
	//{{AFX_MSG_MAP(CComChatDlg)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_QUIT, OnButtonQuit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComChatDlg message handlers

BOOL CComChatDlg::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

	// 初始化串口设备
	com_hCom = OpenPort();
	if(com_hCom == NULL)
	{
		MessageBox(_T("Com can't initialized"),_T("Com Error"),MB_OK|MB_ICONERROR);
		OnButtonQuit();
		return TRUE;
	}
	if(com_hCom == INVALID_HANDLE_VALUE)
	{
		CString c;
		c.Format(_T("Com can't open error = %d"),GetLastError());
		MessageBox(c.LockBuffer(),_T("Com Error"),MB_OK|MB_ICONERROR);
		c.UnlockBuffer();
		OnButtonQuit();
		return TRUE;
	}
	//创建接收线程
	com_hThread = CreateThread(NULL, 0, ReadThreadPort, this, 0, NULL);
	if(com_hThread == NULL)//创建线程失败
	{
		MessageBox(_T("Thread failed"),_T("Com Error"),MB_OK|MB_ICONERROR);
		OnButtonQuit();
		return TRUE;
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}
/*****************************************************************
*	函数名 	:OnButtonSend
*	功能  	:响应按钮Send操作,通过串口发送数据
*	返回值	: 无
******************************************************************/
void CComChatDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	DWORD operSize;
	this->UpdateData();//读取编辑框的内容
	com_Send.Insert(com_Send.GetLength(),_T("\r\n"));//添加换行符号
	wcscpy(com_SendChar,com_Send.LockBuffer());
	com_Send.UnlockBuffer();
	//通过串口发送数据
	if(!WriteFile(com_hCom,com_SendChar,(com_Send.GetLength()+1)*sizeof(WCHAR),	&operSize,0))
	{
		com_Send.Format(_T("Sending error %d"),GetLastError());
	}
	else
	{
		com_Send.Format(_T("%d charactors of %d charactors has been sent\r\n"),operSize/2,com_Send.GetLength());
		com_Msg.Insert(0,com_Send.LockBuffer());
		com_Send.UnlockBuffer();
		com_Send.Empty();
	}
	this->UpdateData(FALSE);
}
/*****************************************************************
*	函数名 	:OnButtonQuit
*	功能  	:响应按钮Quit操作,关闭串口,退出窗口程序
*	返回值	: 无
******************************************************************/
void CComChatDlg::OnButtonQuit() 
{
	// TODO: Add your control notification handler code here
   	if(com_hThread)
	{
		TerminateThread(com_hThread,0);//结束线程,该函数使用的时候请慎重
		CloseHandle(com_hThread);
	}
	if(com_hCom)
	{
		ShutPort(com_hCom); //取消串口操作
	}
	CDialog::OnCancel();
}

void CComChatDlg::OnOK() 
{
	// TODO: Add extra validation here
	OnButtonSend();

}

void CComChatDlg::OnCancel() 
{
	// TODO: Add extra cleanup here	
	OnButtonQuit(); 
}
/*****************************************************************
*	函数名 	:ReadThreadPort
*	功能  	: 串口接收线程函数
*	输入参数: 
*		Param1:  lpvoid 该指针指向创建该线程的对话框类的实例
*	返回值	: 0
******************************************************************/
DWORD WINAPI ReadThreadPort(LPVOID lpvoid)//线程函数实现
{
	CComChatDlg* pDlg = (CComChatDlg*)lpvoid;
	BOOL fReadState;//读串口返回值
	DWORD dwCommModemStatus;
	DWORD dwLength;//未读字节个数
	COMSTAT ComStat;//串口状态结构
	DWORD dwErrorFlags;//错误码		
	DWORD dwBytesRead;//读字节个数
	while (pDlg->com_hCom!=INVALID_HANDLE_VALUE&&pDlg->com_hCom!=NULL) 
	{
		WaitCommEvent (pDlg->com_hCom,&dwCommModemStatus, NULL);//等待串口事件
		if (dwCommModemStatus & EV_RXCHAR) //如果为数据到达事件
		{
			ClearCommError(pDlg->com_hCom,&dwErrorFlags,&ComStat);//读取串口状态并清除状态标志
			dwLength=ComStat.cbInQue;//读缓冲区中的未读字节个数
			if(dwLength>0&&dwLength<(500*sizeof(WCHAR)))
			{	//读串口
				memset(pDlg->com_ReadChar,0,sizeof(WCHAR)*500);
				fReadState=ReadFile(pDlg->com_hCom,pDlg->com_ReadChar,dwLength,&dwBytesRead,NULL);
				if(!fReadState)//读取失败不理会
				{
					
				}			
				else//读取成功
				{
					//插入读入的字符串
					pDlg->com_Msg.Insert(0,pDlg->com_ReadChar);
					pDlg->UpdateData(FALSE);
				}
		 	}		
		    //返回串口状态
			GetCommModemStatus (pDlg->com_hCom,&dwCommModemStatus);
		}	
	}
		return 0;
}

⌨️ 快捷键说明

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