linux下 libusb使用--打开usb设备进行通信

原文地址::https://blog.csdn.net/u011598479/article/details/82705496

相关文章

1、Linux 中libusb安装与调试----https://blog.csdn.net/gd6321374/article/details/79903132

2、linux下libusb的安装与测试----https://www.cnblogs.com/lvdongjie/p/7921433.html

3、Linux libusb 安装及简单使用----https://developer.aliyun.com/article/323231

4、linux下交叉编译libusb的方法及编译一个使用了libusb库的test程序的方法----https://blog.csdn.net/xfc_1939/article/details/53422071

5、libusb之libusb_bulk_transfer之完整实例----https://blog.csdn.net/fxbjye/article/details/78292589

6、libusb-1.0-成功调用libusb_bulk_transfer()后,数据将流向何处?(libusb-1.0 - Where does the data go after a successful libusb_bulk_transfer() call?)----https://www.it1352.com/1816784.html

7、libusb_bulk_transfer() 阻塞长达60s的原因----https://blog.csdn.net/encourage2011/article/details/78760276?utm_source=blogxgwz3

8、基于libusb的通信实例----https://www.cnblogs.com/chorm590/p/13838336.html

 

下载编译安装libusb:https://blog.csdn.net/u011598479/article/details/82705378

1.确定你要通信的USB设备的一些参数。

    user_device.idProduct = USB_PRODUCT_ID; //PID

    user_device.idVendor = USB_VENDOR_ID ; //VID

    user_device.bInterfaceClass = LIBUSB_CLASS_PRINTER ; //打印机设备

    user_device.bInterfaceSubClass = LIBUSB_CLASS_PER_INTERFACE ; //预留接口

    user_device.bmAttributes = LIBUSB_TRANSFER_TYPE_BULK ; //usb bulk 通信方式

    user_device.dev = NULL; //初始化参数

 

user_device为我设置的结构体,用来保存一些要用到的参数。

 

2.init_libusb(); //调用这个库函数初始化库

 

 

3.获取设备描述符,然后根据第一步的设备参数遍历配置描述符,接口描述符,端点描述符,找到USB设备端点地址

4.打开设备libusb_open_device_with_vid_pid()

 

5.声明接口libusb_claim_interface()

 

6.发送数据libusb_bulk_transfer()

 

7.关闭设备,释放接口,释放申请的空间,退出库

libusb_close(g_usb_handle);

libusb_release_interface(g_usb_handle,user_device.bInterfaceNumber);

libusb_free_device_list(user_device.devs, 1);

 libusb_exit(NULL);

 

Makefile

 
  1. # C compiler options

  2. CC = gcc

  3. #CFLAGS = -g -O2

  4. TARGET = main

  5. LIBSPATH = ../lib

  6. PLATFORM = linux32

  7. #LIBS = /usr/local/lib -lusb-1.0

  8. LIBS = /usr/local/lib/libusb-1.0.a -lm -lpthread

  9. INC = /usr/local/include/libusb-1.0

  10.  
  11. #多字节字符 指定汉字编码方式GB18030

  12. EXCHAR = -fexec-charset=GB18030

  13.  
  14. # Source files

  15. SRCS = main.c

  16.  
  17. # Object files

  18. OBJS = $(SRCS:.c=.o)

  19.  
  20. # Make everything

  21. all: $(TARGET)

  22.  
  23. # Make the application

  24. #-lstdc++:编译c++的时候用到

  25. $(TARGET): $(OBJS)

  26. $(CC) $(CFLAGS) $(EXCHAR) -o $(TARGET) $(OBJS) $(LIBS)

  27.  
  28. # Dependencies

  29. #$@:表示目标文件,即:demo.o

  30. #$<:表示依赖文件,即:demo.c

  31. #-I$(INC) :指向包含了的.h文件的路径 即wbe,h的路径

  32. $(OBJS): %.o: %.c

  33. $(CC) -c $(CFLAGS) $(EXCHAR) -o $@ $< -I$(INC)

  34. #

  35. # Clean all object files...

  36. #

  37.  
  38. clean:

  39. $(RM) $(OBJS) $(TARGET)

  40.  

 

main.c

 
  1. /*

  2. * libusb example program to list devices on the bus

  3. * Copyright © 2007 Daniel Drake <dsd@gentoo.org>

  4. *

  5. * This library is free software; you can redistribute it and/or

  6. * modify it under the terms of the GNU Lesser General Public

  7. * License as published by the Free Software Foundation; either

  8. * version 2.1 of the License, or (at your option) any later version.

  9. *

  10. * This library is distributed in the hope that it will be useful,

  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of

  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

  13. * Lesser General Public License for more details.

  14. *

  15. * You should have received a copy of the GNU Lesser General Public

  16. * License along with this library; if not, write to the Free Software

  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

  18. */

  19.  
  20. #include <stdio.h>

  21.  
  22. #include "libusb.h"

  23.  
  24. #define USB_VENDOR_ID 0x0483

  25. #define USB_PRODUCT_ID 0x8007

  26.  
  27. #define BULK_ENDPOINT_OUT 1

  28. #define BULK_ENDPOINT_IN 2

  29.  
  30. struct userDevice{

  31. /*Device descriptor*/

  32.  
  33. /** USB-IF vendor ID */

  34. uint16_t idVendor;

  35.  
  36. /** USB-IF product ID */

  37. uint16_t idProduct;

  38.  
  39. /*Interface descriptor*/

  40.  
  41. /** USB-IF class code for this interface. See \ref libusb_class_code. */

  42. uint8_t bInterfaceClass;

  43.  
  44. /** USB-IF subclass code for this interface, qualified by the

  45. * bInterfaceClass value */

  46. uint8_t bInterfaceSubClass;

  47.  
  48. /*Endpoint descriptor*/

  49.  
  50. /** Attributes which apply to the endpoint when it is configured using

  51. * the bConfigurationValue. Bits 0:1 determine the transfer type and

  52. * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for

  53. * isochronous endpoints and correspond to \ref libusb_iso_sync_type.

  54. * Bits 4:5 are also only used for isochronous endpoints and correspond to

  55. * \ref libusb_iso_usage_type. Bits 6:7 are reserved.

  56. */

  57.  
  58. uint8_t bmAttributes;

  59.  
  60.  
  61. /*save parameter*/

  62.  
  63. libusb_device *dev;

  64. libusb_device **devs;

  65.  
  66.  
  67. u_int8_t bInEndpointAddress;

  68.  
  69. u_int8_t bOutEndpointAddress;

  70.  
  71. /* Number of this interface */

  72. uint8_t bInterfaceNumber;

  73. };

  74.  
  75.  
  76. int init_libusb(void)

  77. {

  78. /*1. init libusb lib*/

  79. int rv = 0;

  80.  
  81. rv = libusb_init(NULL);

  82. if(rv < 0) {

  83. printf("*** initial USB lib failed! \n");

  84. return -1;

  85. }

  86. return rv;

  87. }

  88.  
  89.  
  90. int get_device_descriptor(struct libusb_device_descriptor *dev_desc,struct userDevice *user_device)

  91. {

  92. /*2.get device descriptor*/

  93. int rv = -2;

  94. ssize_t cnt;

  95. int i = 0;

  96.  
  97. libusb_device **devs;

  98. libusb_device *dev;

  99.  
  100. cnt = libusb_get_device_list(NULL, &devs); //check the device number

  101. if (cnt < 0)

  102. return (int) cnt;

  103.  
  104. while ((dev = devs[i++]) != NULL) {

  105. rv = libusb_get_device_descriptor(dev,dev_desc);

  106. if(rv < 0) {

  107. printf("*** libusb_get_device_descriptor failed! i:%d \n",i);

  108. return -1;

  109. }

  110. if(dev_desc->idProduct==user_device->idProduct &&dev_desc->idVendor==user_device->idVendor)

  111. {

  112. user_device->dev = dev;

  113. user_device->devs = devs;

  114. rv = 0;

  115. break;

  116. }

  117. }

  118. return rv;

  119. }

  120.  
  121.  
  122. int match_with_endpoint(const struct libusb_interface_descriptor * interface, struct userDevice *user_device)

  123. {

  124. int i;

  125. int ret=0;

  126. for(i=0;i<interface->bNumEndpoints;i++)

  127. {

  128. if((interface->endpoint[i].bmAttributes&0x03)==user_device->bmAttributes) //transfer type :bulk ,control, interrupt

  129. {

  130. if(interface->endpoint[i].bEndpointAddress&0x80) //out endpoint & in endpoint

  131. {

  132. ret|=1;

  133. user_device->bInEndpointAddress = interface->endpoint[i].bEndpointAddress;

  134. }

  135. else

  136. {

  137. ret|=2;

  138. user_device->bOutEndpointAddress = interface->endpoint[i].bEndpointAddress;

  139. }

  140. }

  141.  
  142. }

  143. if(ret==3)

  144. {

  145. return 1;

  146. }

  147. else

  148. {

  149. return 0;

  150. }

  151. }

  152.  
  153. int get_device_endpoint(struct libusb_device_descriptor *dev_desc,struct userDevice *user_device)

  154. {

  155. /*3.get device endpoint that you need */

  156. int rv = -2;

  157. int i,j,k;

  158. struct libusb_config_descriptor *conf_desc;

  159. u_int8_t isFind = 0;

  160. for (i=0; i< dev_desc->bNumConfigurations; i++)

  161. {

  162. if(user_device->dev != NULL)

  163. rv = libusb_get_config_descriptor(user_device->dev,i,&conf_desc);

  164. if(rv < 0) {

  165. printf("*** libusb_get_config_descriptor failed! \n");

  166. return -1;

  167. }

  168. for (j=0; j< conf_desc->bNumInterfaces; j++)

  169. {

  170. for (k=0; k < conf_desc->interface[j].num_altsetting; k++)

  171. {

  172. if(

  173. conf_desc->interface[j].altsetting[k].bInterfaceClass==user_device->bInterfaceClass

  174. )

  175. {

  176. if(match_with_endpoint(&(conf_desc->interface[j].altsetting[k] ), user_device ))

  177. {

  178. user_device->bInterfaceNumber = conf_desc->interface[j].altsetting[k].bInterfaceNumber;

  179. libusb_free_config_descriptor(conf_desc);

  180. rv = 0;

  181. return rv;

  182. }

  183. }

  184. }

  185. }

  186. }

  187. return -2; //don't find user device

  188. }

  189.  
  190.  
  191. int main(void)

  192. {

  193. int rv;

  194.  
  195. int length;

  196.  
  197. unsigned char a[100] = {0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a};

  198.  
  199. libusb_device_handle* g_usb_handle;

  200.  
  201. struct userDevice user_device;

  202. struct libusb_device_descriptor dev_desc;

  203.  
  204. user_device.idProduct = USB_PRODUCT_ID;

  205. user_device.idVendor = USB_VENDOR_ID ;

  206. user_device.bInterfaceClass = LIBUSB_CLASS_PRINTER ;

  207. user_device.bInterfaceSubClass = LIBUSB_CLASS_PRINTER ;

  208. user_device.bmAttributes = LIBUSB_TRANSFER_TYPE_BULK ;

  209. user_device.dev = NULL;

  210.  
  211. init_libusb();

  212.  
  213. rv = get_device_descriptor(&dev_desc,&user_device);

  214. if(rv < 0) {

  215. printf("*** get_device_descriptor failed! \n");

  216. return -1;

  217. }

  218.  
  219. rv = get_device_endpoint(&dev_desc,&user_device);

  220. if(rv < 0) {

  221. printf("*** get_device_endpoint failed! rv:%d \n",rv);

  222. return -1;

  223. }

  224.  
  225.  
  226. /*4.open device and start communication by usb*/

  227. //open the usb device

  228. g_usb_handle = libusb_open_device_with_vid_pid(NULL, user_device.idVendor, user_device.idProduct);

  229. if(g_usb_handle == NULL) {

  230. printf("*** Permission denied or Can not find the USB board (Maybe the USB driver has not been installed correctly), quit!\n");

  231. return -1;

  232. }

  233.  
  234. rv = libusb_claim_interface(g_usb_handle,user_device.bInterfaceNumber);

  235. if(rv < 0) {

  236. rv = libusb_detach_kernel_driver(g_usb_handle,user_device.bInterfaceNumber);

  237. if(rv < 0) {

  238. printf("*** libusb_detach_kernel_driver failed! rv%d\n",rv);

  239. return -1;

  240. }

  241. rv = libusb_claim_interface(g_usb_handle,user_device.bInterfaceNumber);

  242. if(rv < 0)

  243. {

  244. printf("*** libusb_claim_interface failed! rv%d\n",rv);

  245. return -1;

  246. }

  247.  
  248. }

  249.  
  250.  
  251. rv = libusb_bulk_transfer(g_usb_handle,BULK_ENDPOINT_OUT,a,100,&length,1000);

  252. if(rv < 0) {

  253. printf("*** bulk_transfer failed! \n");

  254. return -1;

  255. }

  256.  
  257. libusb_close(g_usb_handle);

  258.  
  259. libusb_release_interface(g_usb_handle,user_device.bInterfaceNumber);

  260.  
  261. libusb_free_device_list(user_device.devs, 1);

  262.  
  263. libusb_exit(NULL);

  264. }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值