#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "libusb.h"
#define VID 0x8888
#define PID 0x0088
#define edp2in 0x82
#define edp2out 0x02
int main(void)
{
libusb_device **devs, *dev;
int ret, i;
ssize_t cnt;
usb_pro_t usb_pro;
struct libusb_device_handle *handle = NULL;
libusb_context *ctx = NULL;
ret = libusb_init(&ctx);
if (ret < 0)
return -1;
libusb_set_debug(ctx, 3);
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
printf("no usb dev on bus\r\n");
return -1;
}
i = 0;
while((dev = devs[i++]) != NULL) {
ret = libusb_get_device_descriptor(dev,&desc);
if (ret < 0) {
printf("failed to get device descriptor");
goto error;
}
if ((desc.idVendor == VID) && (desc.idProduct == PID)) {
printf("bLength: 0x%04x\r\n", desc.bLength);
printf("bDescriptorType: 0x%04x\r\n"
hts Reserved
stm32 usb通信上位机
30
peixiuhui
关注
STM32 USB 上位机程序实现 转载
2017-08-08 12:17:43
1点赞
peixiuhui
码龄14年
关注
libusb 介绍
libusb是开源的C库,使用该库是的用户可以在应用程序中直接访问 USB 设备,无需为 USB 设备编写内核驱动。libusb支持多个平台 (Linux, window, iOS),所以可以很方便地将应用程序移植到其他平台。
linux libusb 安装
从网上下载libusb的源码,下载地址:http://www., 下载后编译安装。
# tar jxvf libusb-1.0.20.tar.bz2
# cd libusb-1.0.20
# ./configure
# make
# sudo make install
1
2
3
4
5
6
1
2
3
4
5
6
ubuntu下可以通过以下命令快速安装。
sudo apt-get isntall libusb*
1
1
安装后,libusb的头文件被安装在/usr/local/include/libusb-1.0 ,链接库被安装在/usr/loacal/lib目录下。
usb bulk 传输例程
这个例程演示如何使用 libusb 库,编写 USB bulk xfer 上位机demo,可以正常接收和发送数据。注意,修改程序中的 VID 和 PID 的值和你 device 板子上所定义的一致,传输数据块的大小不要超过 device 定义的最大传输长度。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "libusb.h"
#define VID 0x8888
#define PID 0x0088
#define edp2in 0x82
#define edp2out 0x02
int main(void)
{
libusb_device **devs, *dev;
int ret, i;
ssize_t cnt;
usb_pro_t usb_pro;
struct libusb_device_handle *handle = NULL;
libusb_context *ctx = NULL;
ret = libusb_init(&ctx);
if (ret < 0)
return -1;
libusb_set_debug(ctx, 3);
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
printf("no usb dev on bus\r\n");
return -1;
}
i = 0;
while((dev = devs[i++]) != NULL) {
ret = libusb_get_device_descriptor(dev,&desc);
if (ret < 0) {
printf("failed to get device descriptor");
goto error;
}
if ((desc.idVendor == VID) && (desc.idProduct == PID)) {
printf("bLength: 0x%04x\r\n", desc.bLength);
printf("bDescriptorType: 0x%04x\r\n", desc.bDescriptorType);
printf("bcdUSB: 0x%04x\r\n", desc.bcdUSB);
printf("bDeviceClass: 0x%04x\r\n", desc.bDeviceClass);
printf("bDeviceSubClass: 0x%04x\r\n", desc.bDeviceSubClass);
printf("bDeviceProtocol: 0x%04x\r\n", desc.bDeviceProtocol);
printf("bMaxPacketSize0: 0x%04x\r\n", desc.bMaxPacketSize0);
printf("vendor id: 0x%04x\r\n", desc.idVendor);
printf("product id: 0x%04x\r\n", desc.idProduct);
printf("bcdDevice: 0x%04x\r\n", desc.bcdDevice);
printf("iManufacturer: 0x%04x\r\n", desc.iManufacturer);
printf("iProduct: 0x%04x\r\n", desc.iProduct);
printf("iSerialNumber: 0x%04x\r\n", desc.iSerialNumber);
printf("bNumConfigurations: 0x%04x\r\n", desc.bNumConfigurations);
}
}
handle = libusb_open_device_with_vid_pid(ctx, VID, PID);
if (handle == NULL) {
printf("cant't open device\r\n");
goto error;
} else {
printf("open device\r\n");
}
libusb_free_device_list(devs, 1);
if (libusb_kernel_driver_active(handle, 0) ==1) {
printf("kernel driver active, detach it \r\n");
if (libusb_detach_kernel_driver(handle, 0) == 0) {
printf("detached kernel driver\r\n");
}
else {
goto error;
}
}
ret = libusb_claim_interface(handle, 0);
if (ret < 0) {
printf("can't claim interface\r\n");
goto error;
} else {
printf("claimed interface\r\n");
}
char data[64];
int actual_len = 0;
int didi = 1000;
for (int i = 0; i< 1000; i++) {
memset(data, 0, sizeof(data));
/* receive data from device */
/*
ret = libusb_bulk_transfer(handle, edp2in, data, 64, &actual_len, 0);
if (actual_len = 0) {
printf("received nothing\r\n");
} else {
printf("bulk transfer: %s\r\n", data);
}
usleep(200000);
*/
char *str = "am host";
sprintf(data, "am host %d\r\n", i);
ret = libusb_bulk_transfer(handle, edp2out, data, strlen(data), &actual_len, 0);
if (actual_len != 0) {
printf("send data: %s\r\n", data);
}
usleep(200000);
}
libusb_close(handle);
error:
printf("free device list\r\n");
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;