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

📄 firstdlg.cpp

📁 WINCE操作系统下
💻 CPP
字号:
// FirstDlg.cpp : implementation file
//

#include "stdafx.h"
#include "First.h"
#include "FirstDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFirstDlg dialog

CFirstDlg::CFirstDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFirstDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFirstDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFirstDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFirstDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFirstDlg, CDialog)
	//{{AFX_MSG_MAP(CFirstDlg)
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	ON_BN_CLICKED(IDC_BTN_OPEN_COM, OnBtnOpenCom)
	ON_BN_CLICKED(IDC_BTN_CLOSE_COM, OnBtnCloseCom)
	ON_MESSAGE(WM_RECV_SERIAL_DATA,OnRecvSerialData)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFirstDlg message handlers

BOOL CFirstDlg::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_pPort1 = NULL;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFirstDlg::OnBtnSend() 
{
	// TODO: Add your control notification handler code here
	char * buf = NULL;
	DWORD dwBufLen = 0;
	CString strSend = L"";

	CEdit * pEdtSendMsg = (CEdit*)GetDlgItem(IDC_EDIT_SEND);
	ASSERT(pEdtSendMsg != NULL);

	if(m_pPort1 == NULL)
	{
		AfxMessageBox(_T("COM1 has not been Opened."));
		return;
	}

	pEdtSendMsg->GetWindowTextW(strSend);

	int len=strSend.GetLength();
	//将待发送的字符串转换成单字节
	buf = new char[len*2+1];
	ZeroMemory(buf,len*2+1);
	WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,strSend.GetBuffer(len),len,buf,len*2,NULL,NULL);
	dwBufLen=strlen(buf)+1;
	
	//进行发送
	m_pPort1->WriteSyncPort((BYTE*)buf,dwBufLen);

	//释放内存
	delete[] buf;
	buf=NULL;
}

void CALLBACK CFirstDlg::OnSerialRead(void * pOwner,BYTE* buf,DWORD bufLen)
{
	BYTE* pRecvBuf = NULL;
	CFirstDlg* pThis = (CFirstDlg*)pOwner;

	pRecvBuf = new BYTE[bufLen];
	CopyMemory(pRecvBuf,buf,bufLen);

	pThis->PostMessage(WM_RECV_SERIAL_DATA,WPARAM(pRecvBuf),bufLen);
}

LONG CFirstDlg::OnRecvSerialData(WPARAM wParam,LPARAM lParam)
{
	CString strOldRecv = L"";
	CString strRecv = L"";
	//串口接收到的BUF
	CHAR * pBuf = (CHAR*)wParam;
	//串口接收到的BUF长度
	DWORD dwBufLen = lParam;

	//接收框
	CEdit *pEdtRecvMsg = (CEdit*)GetDlgItem(IDC_EDIT_RECV);
	ASSERT(pEdtRecvMsg!=NULL);

	pEdtRecvMsg->GetWindowTextW(strOldRecv);
	strRecv=CString(pBuf);
	if(strOldRecv.GetLength()<1)
	{
		strOldRecv = strRecv;
	}
	else
		strOldRecv = strOldRecv+L"\r\n"+strRecv;

	pEdtRecvMsg->SetWindowTextW(strOldRecv);

	//m_pPort1->WriteSyncPort((BYTE*)pBuf,dwBufLen);

	//释放内存
	delete[] pBuf;
	pBuf = NULL;
	return 0;
}

void CFirstDlg::OnBtnOpenCom() 
{
	// 判断串口是否已经打开,如果已经打开,则关掉它
	if(m_pPort1 != NULL)
	{
		m_pPort1->ClosePort();
		delete m_pPort1;
		m_pPort1 = NULL;
	}

	//打开串口
	m_pPort1 = new CCESeries();	
	m_pPort1->m_OnSeriesRead = OnSerialRead;//设置回调函数

	if(m_pPort1->OpenPort(this,1,9600,NOPARITY,8,1))
	{
		AfxMessageBox(_T("Open Come OK"));
	}
	else
	{
		AfxMessageBox(_T("Open Come Error"));
	}
}

void CFirstDlg::OnBtnCloseCom() 
{
	if(m_pPort1 != NULL)
	{
		m_pPort1->ClosePort();

		delete m_pPort1;
		m_pPort1 = NULL;
	}	
}

⌨️ 快捷键说明

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