关于USB BULK 读写问题
寒竹凌冬 2010-12-17 11:57:23 目前,小弟用VC做了一个小程序,读写USB设备,然遇到了以下问题,高手知道的,指点一下:
本人使用SetupDiGetClassDevs、SetupDiEnumDeviceInterfaces、SetupDiGetDeviceInterfaceDetail几个函数,可以查询到USB设备的路径,设备路径是:\\?\usb#vid_096e&pid_0403#5&3029f02&0&2#{a5dcbf10-6530-11d2-901f-00c04fb91ed} 。
但是网上说,针对USB BULK的设备,需要添加PIPE命名管道,所以我在路径后添加了“\\\\PIPE00”,使用CreateFile打开此路径,成功获得了句柄 。
因是USB BULK类型的设备,很多网上的讨论,加之我也问了一些人,读写这种设备,似乎不能用WriteFile和ReadFile,所以,我根据他们提供和网上找到的信息,使用DeviceIoControl来进行读写,可是第二个参数,IOCTL控制码,用了好几个,似乎总不对,DeviceIoControl的GetLastError返回0x32
发送DeviceIOControl的代码如下:
unsigned char databuf[512];
//组织为SCSI命令格式
memset(databuf, 0, 64);
databuf[0] = 0xAA;
databuf[1] = 0x00;
databuf[2] = 0x00;
databuf[3] = 0x00;
databuf[4] = 0x00;
databuf[5] = 0x81;
databuf[6] = 0x00;
databuf[7] = 0x00;
databuf[8] = 0x00;
databuf[9] = 0x0A;
databuf[10] = 0x00;
databuf[11] = 0x00;
ULONG nBytes;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) + sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);
connectionInfo->ConnectionIndex = 2;
success = DeviceIoControl(hCard,
IOCTL_BULKUSB_WRITE,
connectionInfo,
nBytes,
databuf,
16,
&nlRBytes,
NULL);
if (success)
{
char chTip[512];
memset(chTip,0x00,sizeof(chTip));
sprintf(chTip, "outBuffer returns: %s\n",nlRBytes);
MessageBox(NULL,chTip,"error",MB_OK);
}
else
{
char chTip[512];
memset(chTip,0x00,sizeof(chTip));
sprintf(chTip, "DeviceIoControl failed,GetLastError() returns: 0x%x\n",GetLastError());
MessageBox(NULL,chTip,"error",MB_OK);
}
各位帮忙看下,看问题到底出在哪儿 ?谢谢@@