这两月换了工作,回了成都。
新工作一和设备仪器相关,负责对应的上位机软件开发。
第一个工作就是libusb通信开发和调试。
记录本次libusb的一些代码和调试经历。


1 #include "libusb.h"
2 int main(int argc, char* argv[])
3 {
4 QCoreApplication a(argc, argv);
5
6 // return a.exec();
7
8 libusb_context *usb_context = nullptr;
9 libusb_device **dev_list;
10 libusb_device *dev = NULL;
11 libusb_device *dev_temp;
12 libusb_device_handle *dev_handle = NULL;
13
14 int ret = -1;
15 ssize_t cnt;
16
17
18 if (ret = libusb_init(&usb_context) < 0)
19 {
20 printf("init error:%d\n",ret);
21 return ret;
22 }
23
24 libusb_set_debug(usb_context, 1);
25 if (cnt = libusb_get_device_list(usb_context, &dev_list) < 0)
26 {
27 printf("get device list error:%d\n", cnt);
28 return cnt;
29 }
30
31 int i = 0;
32 while ((dev_temp = dev_list[i++]) != NULL)
33 {
34 struct libusb_device_descriptor desc;
35 int r = libusb_get_device_descriptor(dev_temp, &desc);
36 if (r < 0) {
37 printf("get device descriptor error:%d\n");
38 return NULL;
39 }
40
41
42 printf("%x:%x (bus %d, device %d),bNumberInterfaces:%d\n",
43 desc.idVendor, desc.idProduct,
44 libusb_get_bus_number(dev_temp), libusb_get_device_address(dev_temp), desc.bNumConfigurations);
45 //if ((desc.idProduct == 0xea80) && (desc.idVendor == 0x10c4))
46 if ((desc.idProduct == 0xe008) && (desc.idVendor == 0x1a86))
47 {
48 dev = dev_temp;
49 break;
50 }
51 }
52 if (dev == NULL)
53 {
54 printf("find dev error\n");
55 return -1;
56 }
57
58
59 ret = libusb_open(dev, &dev_handle);
60 if (dev_handle == NULL)
61 {
62 printf("open device error:%d\n", ret);
63 return ret;
64 }
65
66
67 ret = libusb_kernel_driver_active(dev_handle, 0);
68 if (ret == 1)
69 {
70 printf("driver error, kernel_active ret:%d,%s,", ret, libusb_strerror((libusb_error)ret));
71 ret = libusb_detach_kernel_driver(dev_handle, 0);
72 if(ret != 0)
73 printf("driver error, detach_driver ret:%d,%s,", ret, libusb_strerror((libusb_error)ret));
74 }
75
76
77 if (ret = libusb_claim_interface(dev_handle, 0) < 0)
78 {
79 printf("claim interface error:%d\n",ret);
80 libusb_free_device_list(dev_list, 1);
81 libusb_exit(usb_context);
82 return ret;
83 }
84
85 libusb_reset_device(dev_handle);
86
87 libusb_config_descriptor *config_descriptor_in = NULL, *config_descriptor_out = NULL;
88 libusb_get_config_descriptor(dev, 0, &config_descriptor_in);
89 libusb_get_config_descriptor(dev, 1, &config_descriptor_out);
90
91 struct libusb_config_descriptor* conf = nullptr;
92 int config = 0;
93 libusb_get_configuration(dev_handle, &config);
94 printf("libusb_get_configuration config:%d.\n", config);
95 libusb_set_configuration(dev_handle, 0);
96
97
98
99
100 libusb_get_active_config_descriptor(dev, &conf);
101 if(conf){
102 printf("bConfigurationValue:%d\n", conf->bConfigurationValue);
103 //以下分别打印了输出和输入端点
104 for(int q = 0; q < conf->interface->altsetting->bNumEndpoints; ++q){
105 printf("conf-bEndpointAddress:0x%02x.\n", conf->interface->altsetting->endpoint[q].bEndpointAddress);
106 }
107 unsigned char data_rec[128] = "\0";
108 int length = 0;
109 unsigned char data[1] = {90};
110 //ret = libusb_bulk_transfer(dev_handle, config_descriptor_in->interface->altsetting->endpoint->bEndpointAddress, data, 9, &length, 0);
111 //ret = libusb_bulk_transfer(dev_handle, 2, data, 4096, &length, 0);//host--------------->device
112
113
114 unsigned char buf[2] = {0x60, 0x09};
115 //控制传输
116 ret = libusb_control_transfer(dev_handle, 0x21, 0x9, 0x0300, 0, buf, sizeof(buf), 1000);
117 //批量传输
118 ret = libusb_bulk_transfer(dev_handle, 0x02, data, 1, &length, 0);//0x02 是输出端点host->device
119
120 for(int i = 0; i < 100000; i++){
121 memset(data_rec, 0, 128);
122 //断点传输
123 ret = libusb_interrupt_transfer(dev_handle, 0x82, data_rec, 128, &length, 0);//0x82 是输入端点device->host
124 QString str((const char*)data_rec);
125 if(ret == 0){
126 for(int i = 0; i < str.size(); ++i){
127 printf("i = %d,receive data:%02x\n",i, str.at(i));
128 }
129 printf("receive length:%d\n", length);
130 }else{
131 printf("ret = %d,%s\n", ret, libusb_strerror((libusb_error)16));
132 }
133 }
134
135
136 //ret = libusb_bulk_transfer(dev_handle, 0x82, data_rec, 4096, &length, 0);//device--------->host
137 // if(ret < 0){
138 // printf("ret = %d,%s\n", ret, libusb_strerror((libusb_error)16));
139 // }else{
140 // printf("ret = %d,receive data:%s\n", ret, data_rec);
141 // }
142
143 //{
144 // for (int i = 0; i < 100000; i++)
145 // {
146 // memset(data_rec, 0, 4096);
147 // ret = libusb_bulk_transfer(dev_handle, 0x82, data_rec, 4096, &length, 0);//device--------->host
148 // if(ret == 0){
149 // printf("i = %d,receive data:%s\n",i, data_rec);
150 // }else{
151 // printf("ret = %d,%s\n", ret, libusb_strerror((libusb_error)16));
152 // }
153 // }
154 //}
155 }
156 else
157 printf("get config_descriptor error\n");
158
159 libusb_release_interface(dev_handle, 0);//对应libusb_claim_interface
160 libusb_close(dev_handle);
161 libusb_free_device_list(dev_list,1);
162 libusb_exit(usb_context);
163
164 return 0;
165 }
ps:在测试中有一款设备,需要通过调用libusb_reset_device重置之后,才能获取到对应的productName;所以实际处理是通过获取到的vid和pid找到这个设备,然后对它单独重置。