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

下载编译安装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
 
  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. #多字节字符 指定汉字编码方式GB18030

  11. EXCHAR = -fexec-charset=GB18030

  12. # Source files

  13. SRCS = main.c

  14. # Object files

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

  16. # Make everything

  17. all: $(TARGET)

  18. # Make the application

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

  20. $(TARGET): $(OBJS)

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

  22. # Dependencies

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

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

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

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

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

  28. #

  29. # Clean all object files...

  30. #

  31. clean:

  32. $(RM) $(OBJS) $(TARGET)

main.c

1
 
  1. /*

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

  3. * Copyright ???? 2007 Daniel Drake

  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. #include

  20. #include "libusb.h"

  21. #define USB_VENDOR_ID 0x0483

  22. #define USB_PRODUCT_ID 0x8007

  23. #define BULK_ENDPOINT_OUT 1

  24. #define BULK_ENDPOINT_IN 2

  25. struct userDevice{

  26. /*Device descriptor*/

  27. /** USB-IF vendor ID */

  28. uint16_t idVendor;

  29. /** USB-IF product ID */

  30. uint16_t idProduct;

  31. /*Interface descriptor*/

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

  33. uint8_t bInterfaceClass;

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

  35. * bInterfaceClass value */

  36. uint8_t bInterfaceSubClass;

  37. /*Endpoint descriptor*/

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

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

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

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

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

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

  44. */

  45. uint8_t bmAttributes;

  46. /*save parameter*/

  47. libusb_device *dev;

  48. libusb_device **devs;

  49. u_int8_t bInEndpointAddress;

  50. u_int8_t bOutEndpointAddress;

  51. /* Number of this interface */

  52. uint8_t bInterfaceNumber;

  53. };

  54. int init_libusb(void)

  55. {

  56. /*1. init libusb lib*/

  57. int rv = 0;

  58. rv = libusb_init(NULL);

  59. if(rv < 0) {

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

  61. return -1;

  62. }

  63. return rv;

  64. }

  65. int get_device_descriptor(struct libusb_device_descriptor *dev_desc,struct userDevice *user_device)

  66. {

  67. /*2.get device descriptor*/

  68. int rv = -2;

  69. ssize_t cnt;

  70. int i = 0;

  71. libusb_device **devs;

  72. libusb_device *dev;

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

  74. if (cnt < 0)

  75. return (int) cnt;

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

  77. rv = libusb_get_device_descriptor(dev,dev_desc);

  78. if(rv < 0) {

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

  80. return -1;

  81. }

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

  83. {

  84. user_device->dev = dev;

  85. user_device->devs = devs;

  86. rv = 0;

  87. break;

  88. }

  89. }

  90. return rv;

  91. }

  92. int match_with_endpoint(const struct libusb_interface_descriptor * interface, struct userDevice *user_device)

  93. {

  94. int i;

  95. int ret=0;

  96. for(i=0;ibNumEndpoints;i++)

  97. {

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

  99. {

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

  101. {

  102. ret|=1;

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

  104. }

  105. else

  106. {

  107. ret|=2;

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

  109. }

  110. }

  111. }

  112. if(ret==3)

  113. {

  114. return 1;

  115. }

  116. else

  117. {

  118. return 0;

  119. }

  120. }

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

  122. {

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

  124. int rv = -2;

  125. int i,j,k;

  126. struct libusb_config_descriptor *conf_desc;

  127. u_int8_t isFind = 0;

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

  129. {

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

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

  132. if(rv < 0) {

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

  134. return -1;

  135. }

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

  137. {

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

  139. {

  140. if(

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

  142. )

  143. {

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

  145. {

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

  147. libusb_free_config_descriptor(conf_desc);

  148. rv = 0;

  149. return rv;

  150. }

  151. }

  152. }

  153. }

  154. }

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

  156. }

  157. int main(void)

  158. {

  159. int rv;

  160. int length;

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

  162. libusb_device_handle* g_usb_handle;

  163. struct userDevice user_device;

  164. struct libusb_device_descriptor dev_desc;

  165. user_device.idProduct = USB_PRODUCT_ID;

  166. user_device.idVendor = USB_VENDOR_ID ;

  167. user_device.bInterfaceClass = LIBUSB_CLASS_PRINTER ;

  168. user_device.bInterfaceSubClass = LIBUSB_CLASS_PRINTER ;

  169. user_device.bmAttributes = LIBUSB_TRANSFER_TYPE_BULK ;

  170. user_device.dev = NULL;

  171. init_libusb();

  172. rv = get_device_descriptor(&dev_desc,&user_device);

  173. if(rv < 0) {

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

  175. return -1;

  176. }

  177. rv = get_device_endpoint(&dev_desc,&user_device);

  178. if(rv < 0) {

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

  180. return -1;

  181. }

  182. /*4.open device and start communication by usb*/

  183. //open the usb device

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

  185. if(g_usb_handle == NULL) {

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

  187. return -1;

  188. }

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

  190. if(rv < 0) {

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

  192. if(rv < 0) {

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

  194. return -1;

  195. }

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

  197. if(rv < 0)

  198. {

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

  200. return -1;

  201. }

  202. }

  203. rv = libusb_bulk_transfer(g_usb_handle,BULK_ENDPOINT_OUT,a,100,&length,1000);

  204. if(rv < 0) {

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

  206. return -1;

  207. }

  208. libusb_close(g_usb_handle);

  209. libusb_release_interface(g_usb_handle,user_device.bInterfaceNumber);

  210. libusb_free_device_list(user_device.devs, 1);

  211. libusb_exit(NULL);

  212. }