C ++ libusb:如何将数据发送到设备

c++ libusb: how to send data to device?

本文关键字:数据 libusb      更新时间:2023-10-16

美好的一天。
我正在使用libusb为"MGX模拟信号发生器N5181B"编程接口。
首先,我必须初始化设备,因此下一步是发送数据。例如,我想更改将在生成器中显示的频率。
我的代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libusb.h>

using namespace std;
int main(int argc, char *argv[]) {
    if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 1){
        cout<<"This platform hasn't support for hotplug callback notification"<<endl;
        return 0;
    }
    libusb_device_handle* dev_handle;
    libusb_device* dev;
    libusb_context* ctx = NULL;
    int r;
    r = libusb_init(&ctx);
    if (r < 0) {
        cout<<"Init error "<<r<<endl;
    }
    libusb_set_debug(ctx, 3);
    dev_handle = libusb_open_device_with_vid_pid(ctx, 1266, 45940);
    unsigned char data[15] = {':', 'F', 'R', 'E', 'Q', ' ', '5', '0', '0', '0', '0', ' ', 'K', 'H', 'Z'};
    int actual_length;
    r = libusb_bulk_transfer(dev_handle, LIBUSB_ENDPOINT_IN, data, sizeof(data), &actual_length, 0);
    libusb_exit(ctx);
    return 0;
}

编译这个让我遵循:
libusb:错误 [submit_bulk_transfer] submiturb 失败错误 -1 errno=2

拜托,我做错了什么?
感谢您的关注。

如果要在当前设备上进行批量传输,请使用libusb_detach_kernel_driver和libusb_claim_interface。交易后release_inerface并关闭设备

您忘记将设备终端节点批量传输它将是这样的:

libusb_bulk_transfer(dev_handle, (0x81 | LIBUSB_ENDPOINT_IN), response, 24, &actual, 0);

注意:0x81是我的设备的端点