知了博客

集天地之精华,吸日月之灵气

« WinCE串口驱动分析wince串口驱动相关 »

WINCE下如何使用串口!

1.创建线程

////////////////////////////////////////////////////////////////////////////

DWORD CApplicationDlg::CommRecvTread(LPVOID lparam)
{
DWORD dwLength;
char *recvBuf = new char[1024];
CBuletoothApplicationDlg *pDlg = (CBuletoothApplicationDlg*)lparam;

while(TRUE)
{
if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;

if (pDlg->m_hComm != INVALID_HANDLE_VALUE)
{
BOOL fReadState = ReadFile(pDlg->m_hComm, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
//MessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength != 0)
OnCommRecv(pDlg, recvBuf, dwLength);
}
}
}

delete[] recvBuf;
return 0;
}

///////////////////////////////////////////////////////////////////////////////

2.打开串口,创建串口接收线程

//////////////////////////////////////////////////////////////////////////////

void CApplicationDlg::OnButtonPair()
{
// TODO: Add your control notification handler code here
////////////////////////////////////////////////////////
Sleep(100);
////////////////////////////////////////////////////////
DWORD IDThread;
HANDLE hRecvThread;
UpdateData(TRUE);

CString strPort = PorTbl[m_ComboPort.GetCurSel()];
DWORD baud = BaudTbl[m_ComboBaud.GetCurSel()];
DWORD databit = DataBitTbl[1];
BYTE stopbit = StopBitTbl[0];
BYTE parity = ParityTbl[0];

BOOL ret = OpenPort(strPort, baud, databit, stopbit, parity);
if (ret == FALSE)
return;

m_ExitThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// 创建串口接收线程
hRecvThread = CreateThread(0, 0, CommRecvTread, this, 0, &IDThread);
if (hRecvThread == NULL)
{
MessageBox(_T("创建接收线程失败!"));
return;
}
CloseHandle(hRecvThread);

m_ButOpen.EnableWindow(FALSE);
m_ButClose.EnableWindow(TRUE);
MessageBox(_T("打开") + strPort + _T("成功!"));
}
/////////////////////////////////////////////////////////////////////////////////

3.串口接收线程

////////////////////////////////////////////////////////////////////////////////

DWORD CApplicationDlg::CommRecvTread(LPVOID lparam)
{

DWORD dwLength;
char *recvBuf = new char[1024];
CApplicationDlg *pDlg = (CApplicationDlg*)lparam;

while(TRUE)
{
if (WaitForSingleObject(pDlg->m_ExitThreadEvent, 0) == WAIT_OBJECT_0)
break;

if (pDlg->m_hComm != INVALID_HANDLE_VALUE)
{
BOOL fReadState = ReadFile(pDlg->m_hComm, recvBuf, 1024, &dwLength, NULL);
if(!fReadState)
{
//MessageBox(_T("无法从串口读取数据!"));
}
else
{
if(dwLength != 0)
OnCommRecv(pDlg, recvBuf, dwLength);
}
}
}

delete[] recvBuf;
return 0;

}

/////////////////////////////////////////////////////////////////////////////////

4.串口接收信息处理

////////////////////////////////////////////////////////////////////////////////

void CALLBACK CApplicationDlg::OnCommRecv(CWnd* pWnd, char *buf, int buflen)

{

//此处代码可以自己写了

//接收到的串口信息处理部分

}

//////////////////////////////////////////////////////////////////////////////

5.关闭串口

/////////////////////////////////////////////////////////////////////////////////

BOOL CApplicationDlg::ClosePort(void)
{
if(m_hComm != INVALID_HANDLE_VALUE)
{
SetCommMask(m_hComm, 0);
PurgeComm(m_hComm, PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
return TRUE;
}

return FALSE;
}

/////////////////////////////////////////////////////////////////////////////////

来源:http://hi.baidu.com/sudshine/blog/item/435135b26c5535aad9335ae4.html

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Arwen Build 90619 Code detection by Codefense  theme by BokeZhuti

Copyright know blog. Some Rights Reserved.站长(msn):webmaster#webgou.info(#换成@) 粤ICP备09183716号