15,471
社区成员
发帖
与我相关
我的任务
分享
int SendData(BYTE* pdata, int len)
{
int result=0,nErr=0;
while ( 1 )
{
pSerialCom->WritePort(pdata,len);
bool flags;
flags = WaitMyEvent(m_hAckEvent,40);//等待40ms
if ( flags )
{
ResetEvent(m_hAckEvent);
return TRUE;
}
else
{
nErr++;
if ( nErr >= 3)
{
nErr = 0;
return FALSE;
}
}
}
}
BOOL WaitMyEvent(HANDLE hObject,int timeOut)
{
while (TRUE)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// If it's a quit message, we're out of here.
if (msg.message == WM_QUIT)
//return FALSE;
// Otherwise, dispatch the message.
TranslateMessage(&msg);
DispatchMessage(&msg);
} // End of PeekMessage while loop.
// Wait for any message sent or posted to this queue
// or for one of the passed handles be set to signaled.
DWORD result = MsgWaitForMultipleObjects(1, &hObject,
TRUE, timeOut, QS_ALLINPUT);
switch ( result)
{
case WAIT_OBJECT_0:
return TRUE;
break;
case WAIT_OBJECT_0+1:
PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
//TranslateMessage(&msg);
DispatchMessage(&msg);
break;
case WAIT_TIMEOUT:
return FALSE;
break;
default:
break;
}
}
}
ReadFile(ceSeries->m_hComm, readBuf, willReadLen, &actualReadLen,0);
//如果读取的数据大于0,
if (actualReadLen>0)
{
LeaveCriticalSection(&ceSeries->m_csCommunicationSync);
//触发读取回调函数
::SendMessage(ceSeries->m_pPortOwner,WM_NEW_DATA_ARRIVE,(WPARAM)readBuf,(LPARAM)actualReadLen);
}