win7下ReadFile进行读串口数据 接收到的总是0 ,急
海木漄 2012-08-08 03:12:49 void LComm::_read_data(LComm* comm) // 读数据
{
EnterCriticalSection(&comm->m_Cs);
BOOL bResult;
DWORD BytesRead = 0, dwError = 0;
COMSTAT cs;
unsigned char RXBuff;
// ;
// BYTE* LPBuffer;
// unsigned char rBuf1[MAX_PATH], rBuf2[MAX_PATH];
int ret = 0;
for (; ;) {
ClearCommError(comm->m_hComm, &dwError, &cs);
if (cs.cbInQue == 0)
break; // Exit for
ret = ReadFile(comm->m_hComm, &RXBuff, sizeof(RXBuff), &BytesRead,
&comm->m_ov);
if (!ret) {
ret = GetLastError();
Frmain->Memo1->Lines->Add(IntToStr(ret));
Frmain->Memo1->Lines->Add("Read Error " + IntToStr((int)BytesRead) +
"Bytes");
switch (dwError = GetLastError()) {
case ERROR_IO_PENDING:
bResult = GetOverlappedResult(comm->m_hComm,
// Handl to COMM port
&comm->m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
true);
// Read OverLapped Error
if (!bResult) {
comm->RecError(ERR_READ_OL, comm->m_SendData[2]);
}
break;
default:
// Read Other Error
comm->RecError(ERR_READ_OTHERERR, comm->m_SendData[2]);
break;
}
}
else { // Read Successful
comm->RecByte(RXBuff);
}
} // end for
LeaveCriticalSection(&comm->m_Cs);
}