方法不对。请问你用的是NT,还是WINDOWS 9X?在NT下,必须使用读事件,否则返回错误。具体的方法,请看下面的读串口的原码: COMSTAT ComStat ; DWORD dwErrorFlags; DWORD dwLength; OVERLAPPED os_read; os_read.hEvent = CreateEvent( NULL, // no security TRUE, // explicit reset req FALSE, // initial event reset NULL ) ; // no name if (os_read.hEvent == NULL) return -1; // only try to read number of bytes in queue ClearCommError( hCom, &dwErrorFlags, &ComStat ) ; if(!ReadFile( hCom, lpszBlock, nMaxLength, &dwLength, &os_read)) { if (GetLastError() == ERROR_IO_PENDING) { // We have to wait for read to complete. // This function will timeout according to the // CommTimeOuts.ReadTotalTimeoutConstant variable // Every time it times out, check for port errors while(!GetOverlappedResult( hCom, &os_read, &dwLength, TRUE )) { if(GetLastError() != ERROR_IO_INCOMPLETE) { ClearCommError( hCom, &dwErrorFlags, &ComStat ) ; break; } } } else { // some other error occurred dwLength = -1 ; ClearCommError( hCom, &dwErrorFlags, &ComStat ) ; } } CloseHandle(os_read.hEvent); return ( dwLength ) ;
Reading: DWORD dwRead; BOOL fWaitingOnRead = FALSE; OVERLAPPED osReader = {0};// Create the overlapped event. Must be closed before exiting // to avoid a handle leak. osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);if (osReader.hEvent == NULL) // Error creating overlapped event; abort.if (!fWaitingOnRead) { // Issue read operation. if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) { if (GetLastError() != ERROR_IO_PENDING) // read not delayed? // Error in communications; report it. else fWaitingOnRead = TRUE; } else { // read completed immediately HandleASuccessfulRead(lpBuf, dwRead); } } Writing: OVERLAPPED osWrite = {0}; DWORD dwWritten; DWORD dwRes; BOOL fRes; // Create this write operation's OVERLAPPED structure's hEvent. osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (osWrite.hEvent == NULL) // error creating overlapped event handle return FALSE; // Issue write. if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) { if (GetLastError() != ERROR_IO_PENDING) { // WriteFile failed, but isn't delayed. Report error and abort. fRes = FALSE; } else // Write is pending. dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE); switch(dwRes) { // OVERLAPPED structure's event has been signaled. case WAIT_OBJECT_0: if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE)) fRes = FALSE; else // Write operation completed successfully. fRes = TRUE; break;
default: // An error has occurred in WaitForSingleObject. // This usually indicates a problem with the // OVERLAPPED structure's event handle. fRes = FALSE; break; } } } else // WriteFile completed immediately. fRes = TRUE; CloseHandle(osWrite.hEvent); return fRes;
另外,你可以用GetLastError(...)看看时什么错误,将错误号码传入FormatMessage(...)可以得到出了什么错。
这时ReadFile正确返回。但却返回了我刚发送的字符,如我发送"ADZA",则返回"ADZA",却没有收到所期待的字符。另外,我将CreateFile()的第6个参数改为FILE_FLAG_OVERLAPPED,
WaitCommEvent()和ReadFile()的最后一个参数改为 &OverlappedRead .
ReadFile()第3个参数改为 10.
添加变量 OverlappedRead ,并初始化。
OVERLAPPED OverlappedRead ;
OverlappedRead.hEvent=CreateEvent( NULL, TRUE, FALSE, NULL );
这时程序停在WaitCommEvent()中,死掉。
第二个问题是由于设置成异步访问,WriteFile()还没完成就返回,所以必死无疑。
调用FlushFileBuffers(hCom)后,还是一样,没有变化。to xiaozuo:
我设置串行口的COMMTIMEOUTS如下:
timeout.ReadIntervalTimeout=MAXDWORD;
timeout.ReadTotalTimeoutConstant=5000;
timeout.ReadTotalTimeoutMultiplier=0;
timeout.WriteTotalTimeoutConstant=5000;
timeout.WriteTotalTimeoutMultiplier=0;
SetCommTimeouts(hCom,&timeout);我使用GetOverlappedResult如下,程序仍是死掉,不知道写错没有,请指正!if(GetOverlappedResult(hCom,&o,&dwBytesRead,true)){
ReadFile(hCom,read,10,&dwBytesRead,&o);
}
else{
AfxMessageBox("ERROR2");
}
DWORD dwErrorFlags;
DWORD dwLength;
OVERLAPPED os_read; os_read.hEvent = CreateEvent( NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event reset
NULL ) ; // no name
if (os_read.hEvent == NULL) return -1; // only try to read number of bytes in queue
ClearCommError( hCom, &dwErrorFlags, &ComStat ) ; if(!ReadFile( hCom, lpszBlock, nMaxLength, &dwLength, &os_read)) {
if (GetLastError() == ERROR_IO_PENDING) {
// We have to wait for read to complete.
// This function will timeout according to the
// CommTimeOuts.ReadTotalTimeoutConstant variable
// Every time it times out, check for port errors
while(!GetOverlappedResult( hCom, &os_read, &dwLength, TRUE )) {
if(GetLastError() != ERROR_IO_INCOMPLETE) {
ClearCommError( hCom, &dwErrorFlags, &ComStat ) ;
break;
}
}
}
else {
// some other error occurred
dwLength = -1 ;
ClearCommError( hCom, &dwErrorFlags, &ComStat ) ;
}
}
CloseHandle(os_read.hEvent);
return ( dwLength ) ;
Reading:
DWORD dwRead;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);if (osReader.hEvent == NULL)
// Error creating overlapped event; abort.if (!fWaitingOnRead) {
// Issue read operation.
if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) {
if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
// Error in communications; report it.
else
fWaitingOnRead = TRUE;
}
else {
// read completed immediately
HandleASuccessfulRead(lpBuf, dwRead);
}
}
Writing: OVERLAPPED osWrite = {0};
DWORD dwWritten;
DWORD dwRes;
BOOL fRes; // Create this write operation's OVERLAPPED structure's hEvent.
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osWrite.hEvent == NULL)
// error creating overlapped event handle
return FALSE; // Issue write.
if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) {
if (GetLastError() != ERROR_IO_PENDING) {
// WriteFile failed, but isn't delayed. Report error and abort.
fRes = FALSE;
}
else
// Write is pending.
dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
switch(dwRes)
{
// OVERLAPPED structure's event has been signaled.
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE))
fRes = FALSE;
else
// Write operation completed successfully.
fRes = TRUE;
break;
default:
// An error has occurred in WaitForSingleObject.
// This usually indicates a problem with the
// OVERLAPPED structure's event handle.
fRes = FALSE;
break;
}
}
}
else
// WriteFile completed immediately.
fRes = TRUE; CloseHandle(osWrite.hEvent);
return fRes;
另外timeout.ReadTotalTimeoutMultiplier和timeout.WriteTotalTimeoutMultiplier不应该设置成0,从你的设置看最多只能读和写5秒时间,在多个数据读写数时可能发生超时错误。应该根据你的波特率设置timeout.ReadTotalTimeoutMultiplier和timeout.WriteTotalTimeoutMultiplier的数值,timeout.ReadTotalTimeoutConstant也可以设置的小一些。