我已经使用了很长时间的堆栈溢出,并且大多数问题的解决方案已经可用。这是我第一次真正无法通过网络弄清楚它。我希望有人可以解决以下问题。

简介
我目前正在从事一个应该能够执行命令并根据其响应采取行动的项目。该项目在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 call 。

    接口(interface):硬件有两个接口(interface)。接口(interface)0包含OUT 0x02和IN 0x81,接口(interface)1包含OUT 0x04和IN 0x83。如果接口(interface)1被用于诊断命令,则通过工具触发的USB中断调用的嗅探。 (还尝试了同时带有IN和OUT的接口(interface)0,无法使其正常工作。

    使用Wireshark进行数据包嗅探

    数据包嗅探的结果
    使用该工具生成的请求和响应:IMG: Interrupt OUT(我标记了实际提供命令的位置)IMG: Interrupt IN响应该代码实际上有效,并在其数据槽中返回预期的数据集。 (如上所述,返回格式正确,分别为0x01和0x00)。

    LibUSB使用以下代码生成的请求和响应:IMG: Interrupt OUT IMG: Interrupt IN response

    是的,我还尝试将缓冲区的大小设置为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;
    }
    

    托马斯和大卫,非常感谢!

    〜马克

    09-26 04:31