libusb-win32使用总是遇到超时错误

咸蛋超人6456 2013-08-15 03:57:05
libusb-win32,我装完后,插上Ukey,可以读出VID,PID。我用VC写的程序,总是遇到write和read超时的错误,新手,第一次写USB程序,希望高人帮忙指点一下,谢谢了!
下面是我写的代码:
#define MY_VID 0x1234
#define MY_PID 0x5678

// Device configuration and interface id.
#define MY_CONFIG 1
#define MY_INTF 0

// Device endpoint(s)
#define EP_IN 0x81
#define EP_OUT 0x01

// Device of bytes to transfer.
#define BUF_SIZE 64

static int transfer_bulk_async(usb_dev_handle *dev,
int ep,
char *bytes,
int size,
int timeout);

usb_dev_handle *open_dev(void)
{
struct usb_bus *bus;
struct usb_device *dev;

for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == MY_VID && dev->descriptor.idProduct == MY_PID)
{
return usb_open(dev);
}
}
}
return NULL;
}







typedef struct tagusb_cbw_package{
UINT32 dCBWSignature; //byte3-0
UINT32 dCBWTag; //byte 7-4
UINT32 dCBWDataTransferLength;//byte 11-8
BYTE bmCBWFlags; //byte 12
BYTE bCBWLUN; //byte 13
BYTE bCBWCBLength; //byte 14
BYTE CBWCB[16];//byte 31-15
}usb_cbw_package;

typedef struct tagusb_csw_package{
UINT32 dCSWSignature;//byte 3-0
UINT32 dCSWTag;//byte 7-4
UINT32 dCSWDataResidue;//byte 11-8
BYTE bCSWStatus;//byte 12
}usb_csw_package;

usb_cbw_package CBW;
usb_csw_package CSW;

int main()
{

//////////////////////////////////////////////////////////////////////////////



usb_dev_handle *dev = NULL; /* the device handle */
char tmp[BUF_SIZE];
char readdata[40];
memset(readdata,0,40);
int ret;
void* async_read_context = NULL;
void* async_write_context = NULL;

usb_init(); /* initialize the library */
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */


if (!( dev = open_dev()))
{
printf("error opening device: \n%s\n", usb_strerror());
//return 0;
}
else
{
printf("success: device %04X:%04X opened\n", MY_VID, MY_PID);
}
CBW.CBWCB[0]=0xFE;
CBW.CBWCB[1]=0x01;
CBW.dCBWDataTransferLength=0x10000;
CBW.dCBWSignature=0x43425355;
CBW.dCBWTag=0x01000000;
CBW.bmCBWFlags=0x00;
CBW.bCBWLUN=0x00;

//#ifdef TEST_SET_CONFIGURATION
if (usb_set_configuration(dev, MY_CONFIG) < 0)
{
printf("error setting config #%d: %s\n", MY_CONFIG, usb_strerror());
usb_close(dev);
return 0;
}
else
{
printf("success: set configuration #%d\n", MY_CONFIG);
}
//#endif

//#ifdef TEST_CLAIM_INTERFACE
if (usb_claim_interface(dev, 0) < 0)
{
printf("error claiming interface #%d:\n%s\n", MY_INTF, usb_strerror());
usb_close(dev);
return 0;
}
else
{
printf("success: claim_interface #%d\n", MY_INTF);
}
//#endif

//#ifdef TEST_BULK_WRITE


//#ifdef BENCHMARK_DEVICE
ret = usb_control_msg(dev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
14, /* set/get test */
2, /* test type */
MY_INTF, /* interface id */
(char *)CBW.CBWCB, 512, 1000);
ret = transfer_bulk_async(dev, EP_OUT, (char *)CBW.CBWCB, sizeof(CBW.CBWCB), 1000);


ret = usb_bulk_write(dev, EP_OUT, (char *)(CBW.CBWCB), sizeof(CBW.CBWCB), 1000);

if (ret < 0)
{
printf("error writing:\n%s\n", usb_strerror());
}
else
{
printf("success: bulk write %d bytes\n", ret);
}

//ret = usb_get_string_simple(hdev, dev->descriptor.iProduct, string, sizeof(string));




/*CBW.CBWCB[0]=0xFE;
CBW.CBWCB[1]=0x01;
CBW.dCBWDataTransferLength=0x10000;
CBW.dCBWSignature=0x43425355;
CBW.dCBWTag=0x01000000;
CBW.bmCBWFlags=0x01;
CBW.bCBWLUN=0x00;*/

ret = usb_control_msg(dev, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
14, /* set/get test */
1, /* test type */
MY_INTF, /* interface id */
readdata, 512, 1000);

ret = transfer_bulk_async(dev, EP_IN, readdata, sizeof(readdata), 1000);


ret = usb_bulk_read(dev, EP_IN, readdata, sizeof(readdata), 1000);

if (ret < 0)
{
printf("error reading:\n%s\n", usb_strerror());
}
else
{
printf("success: bulk read %d bytes\n", ret);
}
for (int i=0;i<ret;i++)
printf("the miwen is %2x \n",readdata[i]);

usb_release_interface(dev, 0);
system("pause");
if (dev)
{
usb_close(dev);
}
printf("Done.\n");

return 0;
system("pause");
}
static int transfer_bulk_async(usb_dev_handle *dev,
int ep,
char *bytes,
int size,
int timeout)
{
// Each async transfer requires it's own context. A transfer
// context can be re-used. When no longer needed they must be
// freed with usb_free_async().
//
void* async_context = NULL;
int ret;

// Setup the async transfer. This only needs to be done once
// for multiple submit/reaps. (more below)
//
ret = usb_bulk_setup_async(dev, &async_context, ep);
if (ret < 0)
{
printf("error usb_bulk_setup_async:\n%s\n", usb_strerror());
goto Done;
}

// Submit this transfer. This function returns immediately and the
// transfer is on it's way to the device.
//
ret = usb_submit_async(async_context, bytes, size);
if (ret < 0)
{
printf("error usb_submit_async:\n%s\n", usb_strerror());
usb_free_async(&async_context);
goto Done;
}

// Wait for the transfer to complete. If it doesn't complete in the
// specified time it is cancelled. see also usb_reap_async_nocancel().
//
ret = usb_reap_async(async_context, timeout);

// Free the context.
usb_free_async(&async_context);

Done:
return ret;
}
...全文
793 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
beyond696 2015-01-21
  • 打赏
  • 举报
回复 1
上位机调用的读数据函数,但是下位机没正常返回数据,所以出现超时错误
chaome71283 2015-01-06
  • 打赏
  • 举报
回复
read不了,求解
wushuang443 2014-12-25
  • 打赏
  • 举报
回复
引用 3 楼 wushuang443 的回复:
引用 2 楼 tbyyym3 的回复:
楼主问题解决了吗?求指点
这个朋友,我也遇到过这个问题,请问你的解决了么?你是如何解决的呢?麻烦告知下,谢谢
哥们啊,我用libusb开发也遇到过-116,超时这个错误,跟usb设备通信的时候时不时来超时这个错误,请问原因找到了么?是啥问题呢?能否告知下,谢谢啊
wushuang443 2014-09-09
  • 打赏
  • 举报
回复
引用 2 楼 tbyyym3 的回复:
楼主问题解决了吗?求指点
这个朋友,我也遇到过这个问题,请问你的解决了么?你是如何解决的呢?麻烦告知下,谢谢
tbyyym3 2014-09-01
  • 打赏
  • 举报
回复
楼主问题解决了吗?求指点
sparrow429 2014-05-13
  • 打赏
  • 举报
回复
你这是哪个版本的usb-win32啊?

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧