Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Good day.
I'm progrmming interface for "MGX Analog Signal Generator N5181B" with libusb.
For a starting I have to init device, so next step is send data. For example I wanna change frequency that will displayed in generator.
My code:

#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;
}

Compile this get me follow:
libusb: error [submit_bulk_transfer] submiturb failed error -1 errno=2

Please, What I do wrong?
Thx for your attention.

like image 340
Denny Avatar asked Sep 11 '25 18:09

Denny


1 Answers

use libusb_detach_kernel_driver and libusb_claim_interface if you want to make bulk transfer on current device. After transaction release_inerface and close device

like image 172
Simon L. Avatar answered Sep 13 '25 09:09

Simon L.