libusb in windows

libusb in windows

最早接触 libusb 当然是在linux下面。 原来玩2440开发板的时候,网上有人写了一个基于libusb的工具,2440工作在device 模式下,我记得命令是usb slave.

然后通过这个工具将电脑上的镜像传到板子上,非常方便。


公司的项目正好有usb boot mode.这对裸机来说非常有帮助。 于是把握手机制加入以后,居然能够成功的上传镜像。


后来,我发现libusb有windows的版本,于是就把这个工具稍作修改,并且在vs2010上编译通过,并且也能够工作。

下面是代码:



#include <stdio.h>
#include <string.h>

#include <conio.h>

//#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include "dbt.h"

#include "lusb0_usb.h"

#define DEVICE_SECBULK_IDVENDOR  0x3252
#define DEVICE_SECBULK_IDPRODUCT 0x0004


struct usb_device_a6
{
 usb_dev_handle *hdev;
 struct usb_device *dev;
 char *filebuf;
 int filelen;
 char usb_name[MAX_PATH];

};
struct usb_device_a6 a6_usb;

char buf_pr[512];

int usb_init_search()
{
 struct usb_bus *busses, *bus;
 struct usb_device *dev;
 usb_init();
 usb_find_busses();
 usb_find_devices();
 for (bus = usb_get_busses(); bus; bus = bus->next)
 {
   for (dev = bus->devices; dev; dev = dev->next)
   {
    printf("idVendor:0x%x\t,ipProduct:0x%x\n",
      dev->descriptor.idVendor, dev->descriptor.idProduct);

    if (
     ((DEVICE_SECBULK_IDVENDOR == dev->descriptor.idVendor
     && DEVICE_SECBULK_IDPRODUCT == dev->descriptor.idProduct) )
    )
    {
     a6_usb.dev = dev;
     printf("Target usb device found!\n");
     return 0;
    }

   }
 }
 printf("Target usb device not found!\n");
 return -1;
}

int usb_open_port()
{
 a6_usb.hdev = usb_open(a6_usb.dev);
 if (!a6_usb.hdev)
 {
  return -1;
 }
 else
 {
  if (0 != usb_claim_interface(a6_usb.hdev, 0))
  {
   perror("Cannot claim interface");
   usb_close(a6_usb.hdev);
   a6_usb.hdev = NULL;
   return -1;
  }
 }
 return 0;
}

char* prepare_write_buf(char *filename, unsigned int *len) {
 char *write_buf = NULL;
 int ret, ret2=0;
 FILE *pfile;
 int sz;

 pfile=fopen(filename,"rb");
 if(!pfile) {
  printf("open file err\n");
  return NULL;
 }
 fseek(pfile, 0L, SEEK_END);
 sz = ftell(pfile);
 fseek(pfile, 0L, SEEK_SET);

 write_buf = (char*) malloc(sz);
 if (NULL == write_buf) {
  perror("malloc failed");
  goto error;
 }

 ret = fread(write_buf, 1, sz, pfile);
 printf("---Reading file  %d\n", ret);
 
 while(ret > 0) {
  ret2 += ret;
  ret=fread(write_buf+ret2,1, sz, pfile);
  printf("---Reading file  %d %d\n", ret,ret2);
 }
 if (sz != ret2) {
  printf("---Reading file failed %d %d\n", sz, ret2);
  goto error;
 }

 printf("Filename : %s\n", filename);
 printf("Filesize : %d bytes\n", (int)sz);
 *len = sz;
 return write_buf;

error:
  fclose(pfile);
    if (NULL != write_buf)
     free(write_buf);
    sz = 0;
    return NULL;
}


int usb_read_device(int *len)
{
    char readbuf[32];
 unsigned int *magic=(unsigned int *)readbuf;
 int i;
 int wantread;
 memset(readbuf, 0 , sizeof(readbuf));
 wantread = usb_bulk_read(a6_usb.hdev, 0x82, readbuf, 16, 5000);

 for(i=0;i<16;i++)
  printf("%02x ", readbuf[i]);
 printf("\n0x%04x\n",*magic);

 if(*magic == 0x22c3b2a1) {
  *len=*(magic+2);
  printf("\n0x%04x\n",*(magic+2));

  return 0;
 }
 else
  return -1;


}

int usb_write_device(unsigned int writelen, char* buf )
{
 unsigned int remain = writelen;
 unsigned int towrite;
 while (remain)
 {
  towrite = remain > 256 ? 256 : remain;
  if (towrite != usb_bulk_write(a6_usb.hdev, 0x2, buf + (writelen - remain),
     towrite, 3000)) {
   perror("usb_bulk_write failed");
   break;
  }
  remain -= towrite;
  printf("\r%d%\t %d bytes     ", (writelen - remain) * 100 / writelen, writelen-remain);
  fflush(stdout);
 }

 return remain;

}

int loadfile_A6(char *f)
{
 unsigned int remain;
 unsigned int writed=0;
 unsigned int retwrite;
 int asklen = 0;
 unsigned int len;
 char *buf;
 //todo: add the logic here for getting file path and name
 a6_usb.filebuf = prepare_write_buf(f, &len);
 a6_usb.filelen = len;
 
 if(!a6_usb.filebuf)
 {
  return -1;
 }
 
 buf = a6_usb.filebuf;
 remain = a6_usb.filelen;

 for(;;) {
  Sleep(200);
  if(writed == a6_usb.filelen)
  {
   printf("Done!\n");
   return 0;
  }

  if(usb_read_device(&asklen) == 0) {
   if(asklen > 0) {
    retwrite=usb_write_device(asklen, a6_usb.filebuf+writed);
    if(retwrite > 0)
     break;
    writed += asklen;
   }
  }

 }
 return -1;
}

int scan_enable_a6()
{
 if(usb_init_search() < 0)
  return -1;
 if(usb_open_port() < 0)
  return -1;
 return 0;
}

int main(int argc, char *argv[])
{
 if(scan_enable_a6() == 0)
 loadfile_A6(argv[1]);
 
}



关于libusb-win32开发的经验(2012-11-10 11:37:50)转载▼标签: win32rs232驱动usblibusbit 分类: 外围器件接口 作为设备开发者, 一般需要让设备与上位机PC通讯, 我们往往考虑采用以下几种接口: rs232, USB, ethernet. 现在在PC机上已经很难见到rs232的接口, 而ethernet也需要做特殊的配置, USB大多成为我们的首选. 对于数据偏少的应用, 我们可以利用USB虚拟串口的方式来完成这样的任务, 虚拟串口的驱动和实例, 对于下位机来说也非常常见. 有个问题: 很多应用无法用虚拟串口的方式来得到满足, 只能按照USB的方式来解决问题. 这个时候, 我们只好针对USB进行编程. 对于复杂的驱动编程, 大多数程序员往往望而却步. 不过总有其他简单的方法解决问题. 这里, 我们介绍一个USB通讯库: libusb. 介绍 libusb是一个针对usb通讯的库. 使用它, 你不需要知道操作系统的细节, 你只需要对USB有足够的了解即可. 它也不需要你写驱动, 所有的工作都可以在用户态完成. 使用方法很简单, 这里有一个示例: http://sourceforge.net/apps/trac/libusb-win32/wiki/libusbwin32_documentation#IV.Examples , 是不是很简单? 原理 libusb自己带有一个内核驱动, 名字叫libusb0.sys, 放在WINDOWSSYSTEM32DRIVERS里面. 用户程序调用libusb0.dll, dll会把任务交由驱动来完成. 这样保证用户态就能够完成USB通讯的作业. 具体做了什么, 可以通过下载项目的源文件来了解, 等我有时间的时候再看看吧. 安装方法 libusb现在有好几个版本. 主页面在这里: http://www.libusb.org/ 因为我们一般是进行工程应用, 选择相对稳定的版本: libusb-0.1. 平台在windows下的话, 我们采用libusb-win32: http://www.libusb.org/wiki/libusb-win32 linux下一般已经添加到源里面去了, 查找libusb即可. windows下安装方法: http://www.libusb.org/wiki/libusb-win32#Installation 里面有2种安装方式, Filter Driver Installation 和 Device Driver Installation, 前面一个可以说是开发环境, 后面可以说是发布驱动本身. 我们因为是做系统, 选择前面一个方式, 省得麻烦. 使用 这里有比较详细的文档: http://sourceforge.net/apps/trac/libusb-win32/wiki/libusbwin32_documentation libusb-win32的下载地址http://sourceforge.net/apps/trac/libusb-win32/wiki 开发者论坛地址:http://libusb.6.n5.nabble.com/ libusb-1.0 API Reference:http://libusb.sourceforge.net/api-1.0/ 开发过程中有超时问题的原因:http://sourceforge.net/apps/trac/libusb-win32/wiki/libusbwin32_documentation
libusb是一个开源的、跨平台的应用程序接口库,它允许开发者在不同的操作系统上使用统一的API与USB设备进行通讯。Qt则是一个跨平台的C++应用程序开发框架,可以在不同的操作系统上编写图形用户界面。 在Windows系统上进行libusb和Qt的结合开发时,可以利用libusb提供的功能与USB设备进行通讯,同时使用Qt提供的图形界面进行用户交互。 首先,我们需要在Windows系统上安装libusb和Qt的相应库文件。然后,可以使用Qt IDE(例如Qt Creator)来创建一个新的工程。在工程中,需要添加libusb的头文件和库文件,以便在代码中调用libusb提供的函数和结构。 在程序的主界面中,可以添加按钮、文本框等控件来与用户交互。例如,可以添加一个按钮用来打开USB设备,通过点击按钮后,程序调用libusb函数来连接USB设备。 在代码中,可以通过libusb提供的函数来枚举已连接的USB设备、打开设备、进行读写操作等。例如,可以通过libusb_get_device_list函数获取已连接的USB设备列表,然后使用libusb_open_device_with_vid_pid函数打开指定的设备。之后,可以使用libusb_bulk_transfer函数进行读写数据操作。 需要注意的是,在Windows下使用libusb和Qt进行开发时,还需要处理Windows系统的驱动签名问题。如果遇到无法加载驱动的情况,可以考虑禁用驱动签名验证功能。 总之,libusb和Qt的结合可以实现在Windows系统上与USB设备进行通讯和交互的功能。通过调用libusb的函数和使用Qt的图形界面,可以方便地进行USB设备的控制和数据传输操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值