【问题标题】:c++ libusb: how to send data to device?c++ libusb:如何向设备发送数据?
【发布时间】:2017-08-12 03:42:51
【问题描述】:

美好的一天。
我正在使用 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

请问,我做错了什么?
感谢您的关注。

【问题讨论】:

    标签: c++ qt c++11 libusb libusb-1.0


    【解决方案1】:

    如果您想在当前设备上进行批量传输,请使用 libusb_detach_kernel_driver 和 libusb_claim_interface。事务release_inerface后关闭设备

    【讨论】:

    • status = libusb_open2(device, &devh, fd); if (status != LIBUSB_SUCCESS) 返回状态; if (libusb_kernel_driver_active(devh, 0) == 1) { if (!libusb_detach_kernel_driver(devh, 0)) { status = libusb_claim_interface(devh, 0);
    • 谢谢你,看起来很有帮助,但我现在没有设备。请告诉我,我每次都必须像第二个参数一样使用 0 吗?
    • 0 为接口号
    【解决方案2】:

    您忘记将您的设备端点进行批量传输 它会是这样的:

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

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

    【讨论】:

      猜你喜欢
      • 2014-05-06
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 2012-08-11
      • 1970-01-01
      • 2023-03-04
      • 2014-11-15
      相关资源
      最近更新 更多