////////////////////////////////////////////////////////////////////////////
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;
}
/////////////////////////////////////////////////////////////////////////////////