使用ReadFile读取usb设备的问题?
目前碰到一奇怪问题,通过guid获得了该usb设备,并通过CreateFile打开了usb设备通信端口文件,没有使用OVERLAPPED方式的。有时会出现ReadFile从该设备读数据会死等现象, 一直不会返回.然后通过windows设备管理器可以看到该usb设备还是正常状态的,也能通过guid信息调用SetDiGetDves函数成功,可以获得各usb 通信端口,但是一ReadFile就会出现死等现象.
ReadFile使用的阻塞方式.请问和usb设备进行通信时怎么避免该问题的产生,以及怎么使用重叠方式来避免死等,谢谢!
路过
[解决办法]
不要用阻塞方式读
[解决办法]
这个应该是报告描述符有关,我也遇到这个问题,现在还没解决
[解决办法]
没数据上来他当然在死等啊~
[解决办法]
想避免死等使用异步模式的等待超时,
CreateFile的时候加 FILE_FLAG_OVERLAPPED 标志
网上搜搜GetOverlappedResult 介绍的很多,
//overlapped read
{
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.
return FALSE;
}
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);
}
}
if (fWaitingOnRead)
{
#define READ_TIMEOUT 500 // milliseconds
DWORD dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
switch(dwRes)
{
// Read completed.
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, &osReader, &dwRead, FALSE))
{
// Error in communications; report it.
}
else
{
// Read completed successfully.
HandleASuccessfulRead(lpBuf, dwRead);
}
// Reset flag so that another opertion can be issued.
fWaitingOnRead = FALSE;
break;
case WAIT_TIMEOUT:
// Operation isn't complete yet. fWaitingOnRead flag isn't
// changed since I'll loop back around, and I don't want
// to issue another read until the first one finishes.
//
// This is a good time to do some background work.
break;
default:
// Error in the WaitForSingleObject; abort.
// This indicates a problem with the OVERLAPPED structure's
// event handle.
break;
}
}
}
//overlapped write
{
OVERLAPPED osWrite = {0};
DWORD dwWritten;
BOOL fRes;
// Create this writes OVERLAPPED structure 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 it isn't delayed. Report error and abort.
fRes = FALSE;
}
else
{
// Write is pending.
#define WRITE_TIMEOUT 500 //write timeout
DWORD dwRes = WaitForSingleObject(osWrite.hEvent, WRITE_TIMEOUT);
switch(dwRes)
{
// Read completed.
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
fRes = FALSE;
else
// Write operation completed successfully.
fRes = TRUE;
case WAIT_TIMEOUT:
// This is a good time to do some background work.
break;
default:
// Error in the WaitForSingleObject; abort.
// This indicates a problem with the OVERLAPPED structure's
// event handle.
break;
}
}
}
else
// WriteFile completed immediately.
fRes = TRUE;
CloseHandle(osWrite.hEvent);
return fRes;
}
[解决办法]
楼主,这个问题解决了么,我也碰到了同样的问题,改成用WaitForSingleObject在多次读写后,仍然会卡死。
[解决办法]
大哥,我用类似的代码,在多次读写USB后,会出现Read不到数据的现象,这是为什么?
[解决办法]
可能下位机没有发数据上来,或者是你们没有协调好
用 "Bus Hound" 监控一下收发的数据