I'm writing a program which needs to read data from usb device. I looked up msdn, that i know Readfile function can do this,so I used this, but it never works. The program can write data to USB device but can't read and when I add parameter 'GENERIC_READ' in the function createfile ,the program can detect the USB device. As I debug I found that the errorcode is 5. Please help. This is 911. Thanks a lot.
bool CUSB::InitUSB()
{
VendorID=0X040B;
ProductID=0X6515;
GUID guidHID;
HidD_GetHidGuid(&guidHID);
HDEVINFO hDevInfo;
hDevInfo = SetupDiGetClassDevs(&guidHID,
NULL,
0,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if(hDevInfo == INVALID_HANDLE_VALUE)
{
return false;
}
SP_DEVICE_INTERFACE_DATA strtInterfaceData;
strtInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
DWORD index = 0;
CString temp;
BOOL findFlag = false;
int i = 0;
while(1)
{
if(!SetupDiEnumDeviceInterfaces(hDevInfo,
NULL,
&guidHID,
i,
&strtInterfaceData))
{
break;
}
i++;
if(strtInterfaceData.Flags == SPINT_ACTIVE )
{
DWORD strSize = 0, requiesize = 0;
SetupDiGetDeviceInterfaceDetail(hDevInfo,
&strtInterfaceData,
NULL,
0,
&strSize,
NULL);
requiesize = strSize;
PSP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;
strtDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiesize);
strtDetailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
SP_DEVINFO_DATA infodata;
infodata.cbSize = sizeof(SP_DEVINFO_DATA);
if(!SetupDiGetDeviceInterfaceDetail(hDevInfo,
&strtInterfaceData,
strtDetailData,
strSize,
&requiesize,
NULL))
{
free(strtDetailData);
break;
}
CString str = strtDetailData -> DevicePath;
hDevice = CreateFile(strtDetailData->DevicePath,
GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
DWORD dwError = GetLastError();
CString str1;
str1.Format(_T("Error Code :[%d]"), dwError);
AfxMessageBox(str1);
if(hDevice == INVALID_HANDLE_VALUE)
{
free(strtDetailData);
break;
}
HIDD_ATTRIBUTES strtAttrib;
strtAttrib.Size = sizeof(HIDD_ATTRIBUTES);
if(!HidD_GetAttributes(hDevice, &strtAttrib))
{
free(strtDetailData);
break;
}
if(strtAttrib.VendorID == VendorID)
{
if(strtAttrib.ProductID ==ProductID)
{
PHIDP_PREPARSED_DATA preparsedData;
HidD_GetPreparsedData(hDevice, &preparsedData);
HIDP_CAPS capabilities;
HidP_GetCaps(preparsedData, &capabilities);
if(capabilities.OutputReportByteLength == 9)
{
findFlag = true;
free(strtDetailData);
break;
}
}
}
free(strtDetailData);
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
if(findFlag == false)
{
return false;
}
else
{
return true;
}
}
bool CUSB::WriteUSB(char *Data)
{
if(hDevice==INVALID_HANDLE_VALUE)
return false;
PHIDP_PREPARSED_DATA preparsedData;
HidD_GetPreparsedData(hDevice, &preparsedData);
HIDP_CAPS capabilities;
HidP_GetCaps(preparsedData, &capabilities);
memset(SendDataBuffer, 0, 9);
SendDataBuffer[4]=*Data;
bool succ =false;
unsigned long nWriteBytes;
succ = WriteFile(hDevice,
SendDataBuffer,
capabilities.OutputReportByteLength,
&nWriteBytes,
NULL);
return succ;
}
bool CUSB::ReadUSB(char RecieveDataBuffer[])
{
unsigned long length;
if(hDevice==INVALID_HANDLE_VALUE)
return false;
char readBuffer[32];
length = 32;
memset(&readBuffer,0,32);
memset(&RecieveDataBuffer,0,32);
PHIDP_PREPARSED_DATA preparsedData;
HidD_GetPreparsedData(hDevice,&preparsedData);
HIDP_CAPS capabilities;
HidP_GetCaps(preparsedData,&capabilities);
memset(RecieveDataBuffer, 0, 9);
bool succ = false;
unsigned long nWriteBytes;
succ = ReadFile(hDevice,RecieveDataBuffer,capabilities.OutputReportByteLength,&nWriteBytes,NULL);
DWORD dwError = GetLastError();
CString str1;
str1.Format(_T("Error Code :[%d]"), dwError);
AfxMessageBox(str1);
memcpy(&RecieveDataBuffer,&readBuffer,32);
return succ;
}