0

I am running the program below which works with some of my devices, but in three of them after init, open, claim and bulk (write) to work OK, my bulk (read) is returning 0x01 0x60 in my answer buffer. Actually it doesn't matter what I send; after opening USB, if I try to bulk_read... I'm having this answer every time (even in an infinite looping). What it can be?

int main() {
    libusb_device **devs; // Pointer to pointer of device, used to retrieve a list of devices
    libusb_device_handle *dev_handle; // A device handle
    libusb_context *ctx = NULL; // A libusb session
    struct libusb_control_setup setup;

    int r; // For return values
    int i = 0;
    int iCmdLen_BCR = 6;
    int iCmdLen = 0;
    int actual = 0; // Used to find out how many bytes were written

    ssize_t cnt; // Holding number of devices in list

    unsigned char sRsp[1024];
    unsigned char sAux[1024];
    unsigned char data[128];
    unsigned char data_BCR[128]; // Data to write

    memset(data, 0, sizeof(data));
    memset(data_BCR, 0, sizeof(data_BCR));
    memset(sRsp, 0, sizeof(sRsp));
    memset(sAux, 0, sizeof(sAux));


    r = libusb_init(&ctx); // Initialize the library for the session we just declared
    if(r < 0) {
        printf("[%04d] Init Error [%d]\n", __LINE__, r); // There was an error
        return 1;
    }
    libusb_set_debug(ctx, 3); // Set verbosity level to 3, as suggested in the documentation
    cnt = libusb_get_device_list(ctx, &devs); // Get the list of devices
    if(cnt < 0) {
        printf("[%04d] Get Device Error\n", __LINE__); // There was an error
        return 1;
    }
    printf("[%04d] Devices in list.\n", __LINE__);
    dev_handle = libusb_open_device_with_vid_pid(ctx, VD_ID_BCR, PD_ID_BCR); // These are vendorID and productID I found for my usb device
    if(dev_handle == NULL)
        printf("[%04d] Cannot open device\n", __LINE__);
    else
        printf("[%04d] Device Opened\n", __LINE__);
    libusb_free_device_list(devs, 1); // Free the list, unref the devices in it


    if(libusb_kernel_driver_active(dev_handle, 0) == 1) { // Find out if kernel driver is attached
        printf("[%04d] Kernel Driver Active\n", __LINE__);
        if(libusb_detach_kernel_driver(dev_handle, 0) == 0) // Detach it
            printf("[%04d] Kernel Driver Detached!\n", __LINE__);
    }

    r = libusb_claim_interface(dev_handle, 0); // Claim interface 0 (the first) of device (mine had jsut 1)
    if(r < 0) {
        printf("[%04d] Cannot Claim Interface\n", __LINE__);
        return 1;
    }
    printf("[%04d] Claimed Interface r[%d]\n", __LINE__, r);

    memcpy(data_BCR, "\x04\xC8\x04\x00\xFF\x30", iCmdLen_BCR);
    memcpy(data, data_BCR, iCmdLen_BCR);
    iCmdLen = iCmdLen_BCR;

    r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, iCmdLen, &actual, 0); // My device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
    if(r >= 0)
    {
        printf("[%04d] Writing Successful!\n", __LINE__);
        r = libusb_bulk_transfer(dev_handle, 0x81, sRsp, 1024, &actual, 10000);
        for(i=0; i<actual ; i++)
        {
          sprintf(sAux+strlen(sAux), "%02X ", sRsp[i]);
        }
        printf("[%04d] result bulk read[%d] actual[%d] Rsp[%s]\n", __LINE__, r, actual, sAux);
    }
    else
    {
        printf("[%04d] Write Error\n", __LINE__);
    }
    r = libusb_release_interface(dev_handle, 0); // Release the claimed interface
    if(r!=0) {
        printf("[%04d] Cannot Release Interface\n", __LINE__);
        return 1;
    }
    printf("[%04d] Released Interface\n", __LINE__);
    libusb_close(dev_handle); // Close the device we opened
    libusb_exit(ctx); // Needs to be called to end the
    //delete[] data; // Delete the allocated memory for data
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Not important, but you define `iCmdLen_BCR`, and yet you can't do `unsigned char Cmd_BCR[] = {0x04, 0xC8, 0x04, 0x00, 0xFF, 0x30};` and `memcpy(data, Cmd_BCR, sizeof(Cmd_BCR));`? – Iharob Al Asimi Mar 02 '17 at 14:04
  • Also, it would seem like `sprintf(sAux + 3 * i, "%02X ", sRsp[i])` would be better. Or even better, `fprintf(stdout, "%02X ", sRsp[i]);` – Iharob Al Asimi Mar 02 '17 at 14:09
  • My program had 6 devices, I tried to clean it to post here.. Then it has some unnecessary code, but is just a test program – Danilo Almeida Maletta Mar 02 '17 at 16:59

0 Answers0