最近使用VS2005 SDK开发一个USB-HID的应用程序,现在对于hid设备能识别出来,creatfile也没问题,但是继续writefile的时候出问题,使用getlasterror返回值一直是0x57 代码和网上给的例程都是一致的,现在使用BusHound软件能够和hid设备通信,能观察到下发和上传的数据,这样能证明底层USB驱动没问题么? 我现在定位不了错误,不知道怎么继续解决了,希望大牛能帮我分析下
/* 1) Get the HID Globally Unique ID from the OS */ HidD_GetHidGuid(&HidGuid); /* 2) Get an array of structures containing information about all attached and enumerated HIDs */ HidDevInfo = SetupDiGetClassDevs( &HidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); /* 3) Step through the attached device list 1 by 1 and examine each of the attached devices. When there are no more entries in the array of structures, the function will return FALSE. */ Index = 0; /* init to first index of array */ devInfoData.cbSize = sizeof(devInfoData); /* set to the size of the structure that will contain the device info data */ do { /* Get information about the HID device with the 'Index' array entry */ Result = SetupDiEnumDeviceInterfaces( HidDevInfo, 0, &HidGuid, Index, &devInfoData); /* If we run into this condition, then there are no more entries to examine, we might as well return FALSE at point */ if(Result == FALSE) { /* free the memory allocated for DetailData */ if(detailData != NULL) free(detailData); /* free HID device info list resources */ SetupDiDestroyDeviceInfoList(HidDevInfo); return FALSE; } if(GotRequiredSize == FALSE) { /* 3) Get the size of the DEVICE_INTERFACE_DETAIL_DATA structure. The first call will return an error condition, but we'll get the size of the strucure */ DIDResult = SetupDiGetDeviceInterfaceDetail( HidDevInfo, &devInfoData, NULL, 0, &DataSize, NULL); GotRequiredSize = TRUE; /* allocate memory for the HidDevInfo structure */ detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(DataSize); /* set the size parameter of the structure */ detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); } /* 4) Now call the function with the correct size parameter. This function will return data from one of the array members that Step #2 pointed to. This way we can start to identify the attributes of particular HID devices. */ DIDResult = SetupDiGetDeviceInterfaceDetail( HidDevInfo, &devInfoData, detailData, DataSize, &RequiredSize, NULL); /* 5) Open a file handle to the device. Make sure the attibutes specify overlapped transactions or the IN transaction may block the input thread. */ *HidDevHandle = CreateFile( detailData->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, NULL, NULL); /* 6) Get the Device VID & PID to see if it's the device we want */ if(HidDevHandle != INVALID_HANDLE_VALUE) { HIDAttrib.Size = sizeof(HIDAttrib); HidD_GetAttributes( *HidDevHandle, &HIDAttrib); if((HIDAttrib.VendorID == VID) && (HIDAttrib.ProductID == PID)) { hDev = CreateFile( detailData->DevicePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, 0, //for write // FILE_FLAG_OVERLAPPED, //for read NULL); if(hDev == INVALID_HANDLE_VALUE){ DWORD err = GetLastError(); }else{ HIDP_CAPS Capabilities; PHIDP_PREPARSED_DATA HidParsedData; OVERLAPPED HidOverlapped; HANDLE ReportEvent; unsigned char* outbuffer[7] = 0; unsigned char inbuffer[33] = {0}; DWORD numBytesReturned; outbuffer[0] = 0x05; outbuffer[1] = 0x07; outbuffer[2] = 0x11; outbuffer[3] = 0x22; outbuffer[4] = 0x33; outbuffer[5] = 0x44; outbuffer[6] = 0x55; HidD_GetPreparsedData(hDev, &HidParsedData); /* extract the capabilities info */ HidP_GetCaps( HidParsedData ,&Capabilities); /* Free the memory allocated when getting the preparsed data */ HidD_FreePreparsedData(HidParsedData); if(!WriteFile(hDev, outbuffer, Capabilities.OutputReportByteLength, &numBytesReturned, NULL)){ DWORD err = GetLastError(); }
Capabilities.OutputReportByteLength的值是多少 这个值应该比你的下位机读取长度大1
Capabilities.OutputReportByteLength = 0x21 下面是下位机中的USB相关描述符
unsigned char code Config_Descriptor[0x0029] = { /* 1 single Config_Descriptor */ 0x09, //bLength 0x02, //bDescriptorType W2B(0x0029), //wTotalLength(09+09+36+07+07) 0x01, //bNumInterfaces _BCFG_VALUE, //bConfigurationValue 0x00, //iConfiguration 0x00, //bmAttributes 0x32, //bMaxPower /* 2 Interface Descriptor */ 0x09, //bLength 0x04, //bDescriptorType 0x00, //bInterfaceNumber 0x00, //bAlternateSetting 0x02, //bNumEndpoints 0x03, //bInterfaceClass 0x00, //bInterfaceSubClass 0xff, //bInterfacePortocol 0x00, //iInterface /* 3 HID Descriptor */ 0x09, //HID DESCR 0x21, 0x00, 0x01, 0x21, 0x01, 0x22, 0x21, 0x00, /* 4 Bulk-OUT Endpoint Descriptor */ 0x07, //bLength 0x05, //bDescriptorType 0x82, //bEndpointAddress 0x03, //bmAttributes W2B(0x0020), //wMaxPacketSize 0x01, //bInterval /* 5 Bulk-IN Endpoint Descriptor */ 0x07, //bLength 0x05, //bDescriptorType 0x02, //bEndpointAddress 0x03, //bmAttributes W2B(0x0020), //wMaxPacketSize 0x01 //bInterval }; unsigned char code HID_Descriptor[0x21] = { 0x06, 0x00, 0xFF, 0x09, 0x01, 0xA1, 0x01, 0x19, 0x01, 0x29, 0x08, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x20, 0x81, 0x02, 0x19, 0x01, 0x29, 0x08, 0x75, 0x08, 0x95, 0x20, 0x91, 0x02, 0xC0 };
|