温馨提示×

如何在Ubuntu上使用libusb

小樊
114
2024-08-02 04:02:12
栏目: 智能运维

要在Ubuntu上使用libusb,您需要安装libusb开发包。您可以通过以下命令在终端中安装libusb开发包:

sudo apt-get install libusb-1.0-0-dev

安装完成后,您可以编写使用libusb库的程序,并将其编译为可执行文件。下面是一个简单的示例程序,它使用libusb库打开USB设备并向其发送数据:

#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>

int main() {
    libusb_device_handle *handle;
    libusb_init(NULL);
    
    handle = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, PRODUCT_ID);
    if (handle == NULL) {
        fprintf(stderr, "Failed to open device\n");
        return 1;
    }

    // Send data to the device
    unsigned char data[64];
    int bytes_written;
    int result = libusb_bulk_transfer(handle, ENDPOINT_OUT, data, sizeof(data), &bytes_written, TIMEOUT);
    if (result != 0) {
        fprintf(stderr, "Failed to send data\n");
        return 1;
    }

    libusb_close(handle);
    libusb_exit(NULL);
    
    return 0;
}

请注意,示例程序中的VENDOR_ID、PRODUCT_ID、ENDPOINT_OUT和TIMEOUT需要根据您的USB设备的规格进行更改。您可以使用lsusb命令查看您的USB设备的厂商和产品ID。

0