读写HID设备为什么造成USB鼠标响应缓慢!
有一个USB设备,属于HID类型,在读写一段时间后造成USB鼠标响应缓慢,拨出后重新插入鼠标设备又恢复正常,造成这个的原因是什么,读写USB的代码如下:
int CUsb::ReadUSB(unsigned char * recbuffer,int len,int waittime)
{
DWORD index = 0;
HIDD_ATTRIBUTES deviceAttributes;
BOOL matched = FALSE;
PHIDP_PREPARSED_DATA PreparsedData;
PHIDP_CAPS Capabilities = new HIDP_CAPS[1];
NTSTATUS status;
//读取使用的参数
int ErrorCode;
BOOL readStatus;
DWORD bytesRead;
while (1)
{
ErrorCode=0;
Disconnect();
ErrorCode = USBConnectToIthDevice(index);
if (ErrorCode==1){
delete Capabilities;
return -1;
}
if (m_handle!=INVALID_HANDLE_VALUE){
if (!HidD_GetAttributes (m_handle, &deviceAttributes)){
}
else{
if (deviceAttributes.VendorID==VendorID && deviceAttributes.ProductID == ProductID ){
if (HidD_GetPreparsedData(m_handle,&PreparsedData)){
status=HidP_GetCaps(PreparsedData,Capabilities);
if (status==HIDP_STATUS_SUCCESS){
if ( Capabilities-> UsagePage == UsagePage && Capabilities-> Usage == Usage_Read){
//找到我们要的装置,再使用GENERIC_READ | GENERIC_WRITE去开启装置
CloseHandle(m_handle);
m_handle = CreateFile (m_szDevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
FILE_FLAG_OVERLAPPED,
NULL); // No template file
if ( m_handle == INVALID_HANDLE_VALUE){
cout < < "Found HID Device and Interface you specified=> ReadDevice OpenFile Failed!\n " < <endl;
}
m_Received=false;
ZeroMemory(m_ucB,256);
OVERLAPPED overlap;
memset(&overlap, 0, sizeof(OVERLAPPED));
HANDLE completionEvent;
completionEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
overlap.hEvent = completionEvent;
overlap.Offset=overlap.OffsetHigh=0;
DWORD BytesWritten=0;
DWORD NumberOfBytesWritten = 0;
readStatus = ReadFile ( m_handle,m_ucB,
Capabilities-> InputReportByteLength,
&bytesRead,
&overlap);
if (readStatus){
//若是马上读成功,马上把Event唤醒
SetEvent(completionEvent);
memcpy(recbuffer,&m_ucB[1],bytesRead-1);
HidD_FreePreparsedData(PreparsedData);
CloseHandle(completionEvent);
delete Capabilities;
return bytesRead-1;
}
DWORDwaitStatus = WaitForSingleObject (completionEvent, 500 );
if ( WAIT_OBJECT_0 == waitStatus){
readStatus=GetOverlappedResult(m_handle,
&overlap,
&bytesRead,
FALSE);
if(readStatus)
{
m_Received=true;
memcpy(recbuffer,&m_ucB[1],bytesRead-1);
}
}
HidD_FreePreparsedData(PreparsedData);
CloseHandle(completionEvent);
delete Capabilities;
return bytesRead-1;
}
//Get_Caps,成功Usage不同
HidD_FreePreparsedData(PreparsedData);
}
else{
//Get_Caps没成功
HidD_FreePreparsedData(PreparsedData);
}
}
}//end eviceAttributes.VendorID==vendorID && deviceAttributes.ProductID == productID
}// end !HidD_GetAttributes (m_handle, &deviceAttributes
}//end m_handle!=INVALID_HANDLE_VALUE
index=index+1;
}//end while
//HidD_FreePreparsedData(PreparsedData);
//delete Capabilities;
return -1;
}
int CUsb::USBConnectToIthDevice(DWORD deviceIndex)
{
m_ErrorCode=0;
GUID hidGUID;
HDEVINFO hardwareDeviceInfoSet;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
ULONG requiredSize;
DWORD result;
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
//Get the HID GUID value - used as mask to get list of devices
HidD_GetHidGuid (&hidGUID);
//Get a list of devices matching the criteria (hid interface, present)
hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
//Go through the list and get the interface data
result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
NULL, //infoData,
&hidGUID, //interfaceClassGuid,
deviceIndex,
&deviceInterfaceData);
/* Failed to get a device - possibly the index is larger than the number of devices */
if (result == FALSE)
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
return 1;
}
//Get the details with null values to get the required size of the buffer
SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
NULL, //interfaceDetail,
0, //interfaceDetailSize,
&requiredSize,
0); //infoData))
//Allocate the buffer
deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);
deviceDetail-> cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
//Fill the buffer with the device details
if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
deviceDetail,
requiredSize,
&requiredSize,
NULL))
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
return 2;
}
//Open file on the device
//用0去开,条列所有的Device出来。
strcpy(m_szDevicePath,(LPCSTR)deviceDetail-> DevicePath);
m_handle = CreateFile (deviceDetail-> DevicePath,
0,//GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
FILE_FLAG_OVERLAPPED,
NULL); // No template file
if ( m_handle == INVALID_HANDLE_VALUE)
{
cout < < "Device List=> CreateFile open error!\n " < <endl;
}
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
return 0;
}
[解决办法]
好像你一直在读HID设备
这样USB Mouse会一直很少被windows访问吧
[解决办法]
USB鼠标应该是属于HID设备。
[解决办法]
不是由于读USB的时候不断的去查找设备造成的!
再查查其它问题。程序都是这么做的,不停的查找...
[解决办法]
我也是有用VC写HID通讯的 好象鼠标键盘只能被系统直接控制,记得在哪看过 想不起来了
有空交流下:QQ:284375203