读写串口函数(多线程),为何总是得不到想要的从串口读来字符(请看下面程序),求助
「已注销」 2006-04-27 09:36:57 void __fastcall TCbwSerialPort::ReceiveChar(TCbwSerialPort* port, COMSTAT comstat)
{
bool bRead = true;
bool bResult = true;
DWORD dwError = 0;
DWORD BytesRead = 0;
unsigned char RXBuff;
for (;;)
{
EnterCriticalSection(&port->m_csCommunicationSync);
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);
LeaveCriticalSection(&port->m_csCommunicationSync);
if (comstat.cbInQue == 0)
break;
EnterCriticalSection(&port->m_csCommunicationSync);
if (bRead)
{
bResult = ReadFile(port->m_hComm,
&RXBuff,
1,
&BytesRead,
&port->m_ov);
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
bRead = false;
break;
default:
port->ProcessErrorMessage("ReadFile()");
break;
}
}
else
{
bRead = true;
}
}
if (!bRead)
{
bRead = true;
bResult = GetOverlappedResult(port->m_hComm,
&port->m_ov,
&BytesRead,
true);
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults(in ReadFile()");
}
}
LeaveCriticalSection(&port->m_csCommunicationSync);
::SendMessage((port->m_pOwner)->Handle, WM_COMM_RXCHAR, (WPARAM)RXBuff, (LPARAM) port->m_nPortNr);
}//for
v3b.ReadFromPort(RXBuff);//明明读过来的是0x4F,为何这里调用后的RXBuff数据不是0x4F
}