簡體   English   中英

LibUSB C ++ USB傳輸格式不同

[英]LibUSB C++ Format of USB transfer differs

我已經使用堆棧溢出很長時間了,大多數問題的解決方案已經存在。 這是我實際上第一次無法通過網絡弄清楚它。 我希望有人可以解決以下問題。

簡介我目前正在從事一個應該能夠執行命令並根據其響應采取行動的項目。 該項目在c ++控制台應用程序中的基於debian的系統上運行。 為了能夠執行此類命令,我嘗試使用LibUSB庫。

問題無論何時發送數據包,它都不會返回硬件文檔中所述的有效響應。 有一個默認的工具可以觸發callibration命令,我用Wireshark嗅探了這些數據包,但是callibration工具的OUT中斷調用的結構與LibUSB生成的不同,因此(我認為)導致該命令無法執行。

該文檔提供了以下命令之一,該命令應運行診斷檢查以返回5個字節的數據。

[0] Header: 0x02   
[1] Command: 0x4C  
[2] Byte to send: 0x02 (N bytes to send, Argument + data size)   
[3] Argument: 0x09  
[4] Data: 0x00

響應應采用以下格式:

[0] Header: 0x02  
[1] Command: 0x4C  
[2] Byte to send: 0x03 (N bytes to send, Argument + data size)  
[3] Argument: 0x09  
[4] Processing result: D-1  
[5] Diagnostic result: D-2

D-1 :0x01:正常或0x00錯誤D-2 :0x00:正常或非0x00,鏈接的錯誤代碼。

到目前為止嘗試過

  • 轉移類型:
    • 同步:
    • Libusb_bulk_transfer
    • Libusb_control_transfer
    • libusb_interrupt_transfer
    • 異步:
    • Libusb_fill_bulk_transfer
    • Libusb_fill_control_transfer
    • Libusb_fill_interrupt_transfer

我嘗試將兩個異步都作為LibUSB庫的同步實現。 在嘗試填充變量的最合乎邏輯的方法用完之后,我嘗試隨機切換變量,但是沒有成功,這是預料之中的。 由於在數據包嗅探中發現的結果清楚地表明正在進行INTERRUPT呼叫。

接口:硬件有兩個接口。 接口0包含OUT 0x02和IN 0x81,接口1包含OUT 0x04和IN 0x83。 如果接口1用於診斷命令,則通過工具觸發的USB中斷調用的嗅探將通過設備觸發。 (還嘗試了同時使用IN和OUT的接口0,無法使其正常工作。

使用Wireshark進行數據包嗅探

使用工具生成的數據包嗅探請求和響應的結果: IMG:中斷OUT (我標記了實際提供命令的位) IMG:中斷 IN響應該代碼實際起作用,並在其數據槽中返回預期的數據集。 (如上所述,返回格式正確,分別為0x01和0x00)。

LibUSB使用以下代碼生成的請求和響應: IMG:中斷OUT IMG:中斷IN響應

是的,我還嘗試將緩沖區的大小設置為64,即硬件的最大緩沖區大小。 可悲的是沒有工作。 可以清楚地看到,兩個請求相差很大,我使用的傳輸方法是否錯誤? 它是您可以發送命令的另一種受支持的格式嗎?

使用的代碼段:該代碼段有些過時,我嘗試對其進行多次重寫/編輯,最后的實現來自在線示例。

#define USB_VENDOR_ID       <VENDOR_ID>/* USB vendor ID used by the device
                                         * 0x0483 is STMs ID
                                         */
#define USB_PRODUCT_ID      <PRODUCT_ID>      /* USB product ID used by the device */
#define USB_ENDPOINT_IN     (LIBUSB_ENDPOINT_IN  | 0x83)   /* endpoint address */
#define USB_ENDPOINT_OUT    (LIBUSB_ENDPOINT_OUT | 0x04)   /* endpoint address */
#define USB_TIMEOUT         3000        /* Connection timeout (in ms) */
#define INTERFACE_NO        1

static libusb_context *ctx = NULL;
static libusb_device_handle *handle;

static uint8_t receiveBuf[64];
uint8_t transferBuf[64];

uint16_t counter=0;


int main(int argc, char **argv) {
    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
    int r; //for return values
    ssize_t cnt; //holding number of devices in list
    r = libusb_init(&ctx); //initialize the library for the session we just declared
    if(r < 0) {
        qDebug()<<"Init Error "<<r<<endl; //there was an error
        return 1;
    }
    libusb_set_debug(ctx, 4); //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) {
        qDebug()<<"Get Device Error"<<endl; //there was an error
        return 1;
    }
    qDebug()<<cnt<<" Devices in list."<<endl;

    dev_handle = libusb_open_device_with_vid_pid(ctx, 0x0AFA, 0x7D3); //these are vendorID and productID I found for my usb device
    if(dev_handle == NULL)
        qDebug()<<"Cannot open device"<<endl;
    else
        qDebug()<<"Device Opened"<<endl;
    libusb_free_device_list(devs, 1); //free the list, unref the devices in it

    unsigned char *data = new unsigned char[5] { 0x02, 0x4C, 0x02, 0x09, 0 }; //data to write
    data[0]= 0x02;data[1]= 0x4C;data[2]=0x02;data[3]=0x09; data[4]= 0; //some dummy values

    int actual; //used to find out how many bytes were written
    if(libusb_kernel_driver_active(dev_handle, INTERFACE_NO) == 1) { //find out if kernel driver is attached
        qDebug()<<"Kernel Driver Active"<<endl;
        if(libusb_detach_kernel_driver(dev_handle, INTERFACE_NO) == 0) //detach it
            qDebug()<<"Kernel Driver Detached!"<<endl;
    }
    r = libusb_claim_interface(dev_handle, INTERFACE_NO); //claim interface 0 (the first) of device (mine had jsut 1)
    if(r < 0) {
        qDebug()<<"Cannot Claim Interface"<<endl;
        return 1;
    }
    qDebug()<<"Claimed Interface"<<endl;

    for(int i = 0; i != sizeof(data); i++) {
        fprintf(stderr, "[%d] - %02x\n", i, data[i]);
    }
    qDebug()<<"Writing Data..."<<endl;
    r = libusb_bulk_transfer(dev_handle, (USB_ENDPOINT_OUT | LIBUSB_ENDPOINT_OUT), data, sizeof(data), &actual, 0); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129
    if(r == 0 && actual == sizeof(data)) //we wrote the 4 bytes successfully
        qDebug()<<"Writing Successful!"<<endl;
    else
        qDebug()<<"Write Error"<<endl;
        fprintf(stderr, "Error Writing: %s", libusb_strerror(static_cast<libusb_error>(r)));

    r = libusb_release_interface(dev_handle, INTERFACE_NO); //release the claimed interface
    if(r!=0) {
        qDebug()<<"Cannot Release Interface"<<endl;
        return 1;
    }
    qDebug()<<"Released Interface"<<endl;

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

我希望我能有一個能干並願意在這里幫助我的人,因為我已經連續進行了三天的工作,但仍然沒有找到解決這個問題的合理方法。

提前致謝!

〜馬克

感謝您的答復! 我目前找到了解決該問題的方法! 與同時使用C / C ++無關。 對不起,代碼有點混亂。 我寫了好幾次,所以整理不是我的優先事項,盡管我會在以后有關StackOverflow的文章中牢記這一點。 即使解決了問題,我仍然增加了嗅探進入和退出數據包的結果,希望它可以幫助其他可能遇到相同問題的人。

好吧,這是什么問題?
因此,工具的捕獲表明最后64位是請求及其數據的有效負載,這是針對OUT和IN的。 (從現在實際提供的圖像中可以看出),正如我之前說過的,我嘗試分配大小為64的數組,並使用操作所需的數據設置前幾個插槽。 至於其他插槽,它們被剩余在那些分配的內存地址上的剩余空間所充滿。

我做了什么修復
因此,我要做的是以下幾點。 在初始化數組並為其分配大小為64之后,我使用memset命令將所有分配的插槽設置為0,這樣就可以完全清除數組中的剩余數據。 這給了我一個干凈的數組,可以在其中設置要發送的命令所需的變量。 (請參見以下代碼段)

// Initialize array of 64 bytes.
    uint8_t *data = new uint8_t[64];
    memset(data, 0x00, 64);
    data[0] = 0x02; data[1] = 0x4C; data[2] = 0x01; data[3] = 0x17;

我整理了一些代碼以提供更好的可讀性,這是我使用的有效代碼! 希望其他人發現此信息有用。

//*** DEPENDENCIES *************************************************************
// QT
#include <QCoreApplication>
#include <QtCore/QDebug>
// Others
#include <libusb.h>
#include <iostream>


//*** VARIABLES ****************************************************************
#define USB_VENDOR_ID      <VENDOR_ID_GOES_HERE>
#define USB_PRODUCT_ID     <PRODUCT_ID_GOES_HERE>
#define USB_ENDPOINT_OUT    0x04
#define USB_ENDPOINT_IN     0x83

#define INTERFACE_NO        0x01

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    libusb_device *dev;
    libusb_device_handle *dev_handle;
    libusb_context *ctx = NULL;


    //*** INITIALIZATION *******************************************************
    uint r = libusb_init(&ctx);
    // Check if initiated succesfully
    if ( r < 0 )                            { qDebug() << "Init error."; return 1; }
    libusb_set_debug(ctx, 4);

    dev_handle = libusb_open_device_with_vid_pid(ctx, USB_VENDOR_ID, USB_PRODUCT_ID);

    if (dev_handle == NULL) { qDebug() << "Could not open device."; return 1;}
    qDebug() << "Device opened succesfully!";


    // Check if kernel driver, detach
    if(libusb_kernel_driver_active(dev_handle, INTERFACE_NO) == 1) {
            qDebug() << "Kernel Driver Active";
            if(libusb_detach_kernel_driver(dev_handle, INTERFACE_NO) == 0) {
               qDebug() << "Kernel Driver Detached";
        }
    }

    // Claim interface
    r = libusb_claim_interface(dev_handle, INTERFACE_NO);
    if ( r < 0 ) {
        qDebug() << "Could not claim interface.";
        return 1;
    }
    qDebug() << "Interface claimed.";


    //*** EXECUTION OF USB TRANSFERS *******************************************

    // Prepare command
    int actual_written;
    // Initialize array of 64 bytes.
    uint8_t *data = new uint8_t[64];
    memset(data, 0x00, 64);
    data[0] = 0x02; data[1] = 0x4C; data[2] = 0x01; data[3] = 0x17;

    qDebug() << "================= OUT ==============================";
    //*** ATTEMPT TO WRITE COMMAND *********************************************
    r = libusb_bulk_transfer(dev_handle,
                             USB_ENDPOINT_OUT,
                             data, 64,
                             &actual_written,
                             10000);

    qDebug() << "OUT status: " << libusb_strerror(static_cast<libusb_error>(r));
    if (r == 0 && actual_written == 64) {
        qDebug() << "Succesfully written!";
    } else {
        qDebug() << "||" << r << "||"<< actual_written << "||"
                 << "Could not write.";
    }
    qDebug() << "================== IN ===============================";


    //*** ATTEMPT TO READ FEEDBACK *********************************************
    // Initialize array of 64 bytes.
    uint8_t *feedback = new uint8_t[64];
    memset(feedback, 0x00, 64);

    int actual_received;
    r = libusb_bulk_transfer(
                dev_handle,
                USB_ENDPOINT_IN,
                feedback,
                64,
                &actual_received,
                0);

    qDebug() << "IN status: " << libusb_strerror(static_cast<libusb_error>(r));
   if(r == 0 && actual_received == 64) {
            qDebug("\nRetrieval successful!");
            qDebug("\nSent %d bytes with string: %s\n", actual_received, feedback);
    } else {
       qDebug() << actual_received << "||" <<feedback << "||"
                << "Could not read incoming data. ||";
   }

    for( int m = 0; m < 64; m++)
    {
        fprintf(stderr, "[%d] - %02x\n", m, feedback[m]);
    }

    if (feedback[4] != 0x01) {
        qDebug() << "Unsuccesful offset adjustment.";
        return -1;
    }

    // Further code should go here.

    //*** FREEING USB **********************************************************
    // Releasing interface
    r = libusb_release_interface(dev_handle, INTERFACE_NO);
    if ( r < 0 )        { qDebug() << "Could not release interface."; return 1; }
    qDebug() << "Interface released.";

    libusb_close(dev_handle);
    libusb_exit(ctx);
    delete[] data;
    delete[] feedback;


    qDebug() << "End of main";
    return 0;
}

托馬斯和大衛,非常感謝!

〜馬克

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM