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 in windows相关推荐
- java libusb_Windows下的Java访问USB设备解决之道(翻译Java libusb / libusb-win32 wrapper)收...
Java libusb / libusb-win32 封装包 概要 Java libusb 封装包是对libsub和libusb-win32 USB library的Jave封装. libusb旨在创 ...
- 基于libhid/libusb进行usb传输数据
一.libhid/libusb介绍 libusb介绍: libusb 设计了一系列的外部API 为应用程序所调用,通过这些API应用程序可以操作硬件,从libusb的源代码可以看出,这些API 调用了 ...
- libusb系列-002-Windows下libusb源码编译
libusb系列-002-Windows下libusb源码编译 文章目录 libusb系列-002-Windows下libusb源码编译 摘要 Windows下编译libusb库 下载源码 进入msv ...
- 使用libusb检测USB设备插拔状态
libusb是一个提供USB设备访问的跨平台用户模式程序库.该项目最新网址:http://www.libusb.info, 支持主流的操作系统:Linux.Mac OS X. Windows.Open ...
- 关于libusb-win32开发的经验
关于libusb-win32开发的经验 (2012-11-10 11:37:50) 转载▼ 标签: win32 rs232 驱动 usb libusb it 分类:外围器件接口 作为设备开发者, 一般 ...
- libusb-win32 在visual studio2008中编译
usb协议在windows上使用的最多的估计就是libusb-win32 svn代码仓库: 由于要修改驱动里面相关的信息,所以要重新编译这个驱动安装,在安装的过程中,发现inf-wizard这个程序一 ...
- USB学习入门(五)------蓦然回首,那人却在灯火阑珊处
认识了USB设备,我们的程序里怎样实现主机和USB设备的通讯呢,你首先想到得的开发USB设备的驱动程序了,毕竟我们见到的很多的USB设备使用之前都需要安装驱动程序,开始接触USB设备的时候,我也是这样 ...
- windows libusb打开失败_如何彻底解决windows更新问题!
相信大家都为windows更新的问题所困扰,为了解决这个问题,我在网上查询各种方法,但是都没有什么用,Windows更新照样更新,最后经过我的尝试,我终于找到了一个彻底解决windows更新的绝顶方法 ...
- windows下mingw32 编译 libusb[实测可行]
之前项目需要libusb开发QT下的USB硬件相关的应用程序,一直没有整理文档,今天重走一遍,并把文档整理出来. 一.材料准备 1.windows7 x64 2.mingw32 作用:使用gcc,g+ ...
最新文章
- Linux那些事儿 之 戏说USB(29)驱动的生命线(一)
- “单细胞”前瞻 |新型微滴反应筛选技术ATAC-seq数据分析新篇章
- 802.11介质访问控制——CSMA/CD协议详解
- android 调用线程方法,Android使用线程获取网络图片的方法
- 安装mysql客户端
- Arduino操作记录---雨滴传感器的使用
- Nodejs 批量检测 Excel 中url链接是否可访问
- ArcGis利用栅格处理工具进行影像裁剪
- methodsignature java_Java MethodSignature.getMethod方法代碼示例
- 随手练——字符串按最小(大)字典序拼接
- win7 win7 我的win7
- 【安全服务】应急响应1:流程、排查与分析
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- 3.HTML——表格数据
- 与其道听途说,不如少走点弯路,轻松跟我来全面剖析 darknet 官网!
- 英勇的战士——斯巴达
- Java自学习day15-抽象类练习-编写工资系统,实现不同类型员工(多态)的按月发放工资
- 【postgresql】centos7安装postgresql-devel步骤
- cocos2d-x碰撞检测学习笔记
- JS正则只允许输入文字、数字、英文