USB应用程序开发Word文档下载推荐.docx
《USB应用程序开发Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《USB应用程序开发Word文档下载推荐.docx(17页珍藏版)》请在冰点文库上搜索。
hid.lib"
comctl32.lib"
#ifndefBULKUSBH_INC
#defineBULKUSBH_INC
#defineBULKUSB_IOCTL_INDEX
0x0000
#defineIOCTL_BULKUSB_GET_CONFIG_DESCRIPTOR
CTL_CODE(FILE_DEVICE_UNKNOWN,
/
BULKUSB_IOCTL_INDEX,/
METHOD_BUFFERED,
FILE_ANY_ACCESS)
#defineIOCTL_BULKUSB_RESET_DEVICE
BULKUSB_IOCTL_INDEX+1,/
FILE_ANY_ACCESS)
#defineIOCTL_BULKUSB_RESET_PIPE
CTL_CODE(FILE_DEVICE_UNKNOWN,
BULKUSB_IOCTL_INDEX+2,/
FILE_ANY_ACCESS)
externHANDLE
open_file(char*filename);
externint
GetUsbPath(char*path);
WriteUsb(HANDLEhUsb,char*Outbuff,intlen);
ReadUsb(HANDLEhUsb,BYTEinbuff[],DWORD&
nBytesRead,intnToRead);
3.2源文件usbport.cpp
#include"
usbport.h"
//8a3bf75d-83c7-440e-8276-5ae3f3ea6e77
DEFINE_GUID(GUID_CLASS_I82930_BULK,0x8a3bf75d,0x83c7,0x440e,0x82,0x76,0x5a,0xe3,0xf3,0xea,0x6e,0x77);
BOOL
GetUsbDeviceFileName(LPGUID
pGuid,char*outNameBuf);
HANDLE
OpenUsbDevice(LPGUID
OpenOneDevice(HDEVINFO
HardwareDeviceInfo,PSP_INTERFACE_DEVICE_DATA
DeviceInfoData,char*devName);
int
ReadUsb(HANDLEhUsb,BYTEinbuff[],DWORD&
/*名称:
open_file
功能:
打开USB设备
参数:
filename定义为”PIPE00”pipenameforbulkinputpipeonourtestboard
”PIPE01”pipenameforbulkoutputpipeonourtestboard。
PIPE00和PIPE01 是参考src/usb/bulkusb,我实际在用时这两个效果一样,为了避免USB异常引起的死机,我文件打开采用非阻塞模式。
*/
HANDLEopen_file(char*filename)
{
intsuccess=1;
HANDLEh;
charcompleteDeviceName[256]="
"
;
//generatedfromtheGUIDregisteredbythedriveritself
if(!
GetUsbDeviceFileName((LPGUID)&
GUID_CLASS_I82930_BULK,completeDeviceName))
{
//NOISY(("
FailedtoGetUsbDeviceFileName/n"
GetLastError()));
return
INVALID_HANDLE_VALUE;
}
strcat(completeDeviceName,
"
//"
);
filename);
//printf("
completeDeviceName=(%s)/n"
completeDeviceName);
h=CreateFile(completeDeviceName,
GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,
NULL);
if(h==INVALID_HANDLE_VALUE){
Failedtoopen(%s)=%d"
completeDeviceName,GetLastError()));
success=0;
}else{
Openedsuccessfully./n"
));
}
returnh;
}
GetUsbDeviceFileName
功能:
获取USB设备路径
参数:
pGUID
返回:
outNameBufUSB设备路径
BOOLGetUsbDeviceFileName(LPGUID
pGuid,char*outNameBuf)
HANDLEhDev=OpenUsbDevice(pGuid,outNameBuf);
if(hDev!
=INVALID_HANDLE_VALUE)
CloseHandle(hDev);
returnTRUE;
returnFALSE;
OpenUsbDevice
pGUID设备GUID
HANDLEOpenUsbDevice(LPGUID
ULONGNumberDevices;
HANDLEhOut=INVALID_HANDLE_VALUE;
HDEVINFO
hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATAdeviceInfoData;
ULONG
i;
BOOLEAN
done;
PUSB_DEVICE_DESCRIPTOR
usbDeviceInst;
*UsbDevices=&
usbDeviceInst;
*UsbDevices=NULL;
NumberDevices=0;
//
//Openahandletotheplugandplaydevnode.
//SetupDiGetClassDevs()returnsadeviceinformationsetthatcontainsinfoonall
//installeddevicesofaspecifiedclass.
hardwareDeviceInfo=SetupDiGetClassDevs(
pGuid,
NULL,//Definenoenumerator(global)
NULL,//Defineno
(DIGCF_PRESENT|//OnlyDevicespresent
DIGCF_INTERFACEDEVICE));
//Functionclassdevices.
//Takeawildguessatthenumberofdeviceswehave;
//Bepreparedtoreallocandretryiftherearemorethanweguessed
NumberDevices=4;
done=FALSE;
deviceInfoData.cbSize=sizeof(SP_INTERFACE_DEVICE_DATA);
i=0;
while(!
done)
NumberDevices*=2;
if(*UsbDevices)
*UsbDevices=(PUSB_DEVICE_DESCRIPTOR)realloc(*UsbDevices,(NumberDevices*sizeof(USB_DEVICE_DESCRIPTOR)));
}
else
*UsbDevices=(PUSB_DEVICE_DESCRIPTOR)calloc(NumberDevices,sizeof(USB_DEVICE_DESCRIPTOR));
if(NULL==*UsbDevices)
//SetupDiDestroyDeviceInfoListdestroysadeviceinformationset
//andfreesallassociatedmemory.
SetupDiDestroyDeviceInfoList(hardwareDeviceInfo);
returnINVALID_HANDLE_VALUE;
usbDeviceInst=*UsbDevices+i;
for(;
i<
NumberDevices;
i++)
//SetupDiEnumDeviceInterfaces()returnsinformationaboutdeviceinterfaces
//exposedbyoneormoredevices.Eachcallreturnsinformationaboutoneinterface;
//theroutinecanbecalledrepeatedlytogetinformationaboutseveralinterfaces
//exposedbyoneormoredevices.
if(SetupDiEnumDeviceInterfaces(hardwareDeviceInfo,
0,//Wedon'
tcareaboutspecificPDOs
pGuid,
i,
&
deviceInfoData)){
hOut=OpenOneDevice(hardwareDeviceInfo,&
deviceInfoData,outNameBuf);
if(hOut!
=INVALID_HANDLE_VALUE)
done=TRUE;
break;
if(ERROR_NO_MORE_ITEMS==GetLastError())
NumberDevices=i;
//SetupDiDestroyDeviceInfoList()destroysadeviceinformationset
free(*UsbDevices);
returnhOut;
HANDLEOpenOneDevice(
IN
HardwareDeviceInfo,
PSP_INTERFACE_DEVICE_DATA
DeviceInfoData,
char*devName
)
PSP_INTERFACE_DEVICE_DETAIL_DATA
functionClassDeviceData=NULL;
predictedLength=0;
requiredLength=0;
HANDLE
hOut=INVALID_HANDLE_VALUE;
//allocateafunctionclassdevicedatastructuretoreceivethe
//goodsaboutthisparticulardevice.
SetupDiGetInterfaceDeviceDetail(
NULL,//probingsonooutputbufferyet
0,//probingsooutputbufferlengthofzero
&
requiredLength,
//notinterestedinthespecificdev-node
predictedLength=requiredLength;
//sizeof(SP_FNCLASS_DEVICE_DATA)+512;
functionClassDeviceData=(PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength);
functionClassDeviceData->
cbSize=sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
//RetrievetheinformationfromPlugandPlay.
if(!
functionClassDeviceData,
predictedLength,
NULL)){
free(functionClassDeviceData);
strcpy(devName,functionClassDeviceData->
DevicePath);
//printf("
Attemptingtoopen%s/n"
devName);
hOut=CreateFile(
DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,//noSECURITY_ATTRIBUTESstructure
OPEN_EXISTING,//No