尝试通过libusb_bulk_transfer接收时出现LIBUSB_ERROR_TIMEOUT

问题描述 投票:0回答:1

嘿,我正在尝试在 Ubuntu 22.04.3 LTS 上通过 USB 与有问题的激光雷达传感器进行通信。目前我可以打开并声明端口并发送数据。但如果我尝试接收数据,则会收到 LIBUSB_ERROR_TIMEOUT。我尝试了不同的端点、超时时间(1-6s)和传输大小(8-2048),但没有成功。您可能知道为什么它不起作用吗?谢谢你:)

我的代码:

    int8_t endpointAddress = 0x85;
    int maxPacketSize = libusb_get_max_packet_size(libusb_get_device(handle), endpointAddress);

    std::vector<unsigned char> buffer(maxPacketSize);

    int transferred;
    int result = libusb_bulk_transfer(handle, endpointAddress, buffer.data(), sizeof(buffer), &transferred, 1000);

    if (result == 0) {
        std::cout << "Received " << transferred << " bytes of data:" << std::endl;
        for (int i = 0; i < transferred; ++i) {
            std::cout << std::hex << static_cast<int>(buffer[i]) << " ";
        }
        std::cout << std::dec << std::endl;
    } else {
        std::cerr << "Failed to receive data. libusb_bulk_transfer error: " << libusb_error_name(result) << std::endl;
    }

Libusb日志:

[ 0.001609] [00005ec1] libusb: debug [libusb_get_device_list]  
[ 0.001624] [00005ec1] libusb: debug [libusb_get_device_list]  
[ 0.001627] [00005ec1] libusb: debug [libusb_get_device_descriptor]  
[ 0.001628] [00005ec1] libusb: debug [libusb_get_device_descriptor]  
[ 0.001629] [00005ec1] libusb: debug [libusb_get_device_descriptor]  
[ 0.001629] [00005ec1] libusb: debug [libusb_get_device_descriptor]  
[ 0.001631] [00005ec1] libusb: debug [libusb_open] open 1.4
[ 0.001642] [00005ec1] libusb: debug [usbi_add_event_source] add fd 26 events 4
[ 0.001644] [00005ec1] libusb: debug [libusb_detach_kernel_driver] interface 0
[ 0.001648] [00005ec1] libusb: debug [libusb_claim_interface] interface 0
[ 0.001745] [00005ec1] libusb: debug [libusb_get_device_list]  
[ 0.001810] [00005ec1] libusb: debug [libusb_submit_transfer] transfer 0x5555559f19b0
[ 0.001812] [00005ec1] libusb: debug [add_to_flying_list] arm timer for timeout in 1000ms (first in line)
[ 0.001816] [00005ec1] libusb: debug [submit_bulk_transfer] need 1 urbs for new transfer with length 20
[ 0.001834] [00005ec1] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
[ 0.001835] [00005ec1] libusb: debug [handle_events] event sources modified, reallocating event data
[ 0.001838] [00005ec1] libusb: debug [usbi_wait_for_events] poll() 3 fds with timeout in 60000ms
[ 1.001824] [00005ec1] libusb: debug [usbi_wait_for_events] poll() returned 1
[ 1.001838] [00005ec1] libusb: debug [libusb_cancel_transfer] transfer 0x5555559f19b0
[ 1.001993] [00005ec1] libusb: debug [arm_timer_for_next_timeout] no timeouts, disarming timer
[ 1.002003] [00005ec1] libusb: debug [libusb_handle_events_timeout_completed] doing our own event handling
[ 1.002005] [00005ec1] libusb: debug [usbi_wait_for_events] poll() 3 fds with timeout in 60000ms
[ 1.002007] [00005ec1] libusb: debug [usbi_wait_for_events] poll() returned 1
[ 1.002009] [00005ec1] libusb: debug [reap_for_handle] urb type=3 status=-2 transferred=0
[ 1.002010] [00005ec1] libusb: debug [handle_bulk_completion] handling completion status -2 of bulk urb 1/1
[ 1.002011] [00005ec1] libusb: debug [handle_bulk_completion] abnormal reap: urb status -2
[ 1.002012] [00005ec1] libusb: debug [handle_bulk_completion] abnormal reap: last URB handled, reporting
[ 1.002014] [00005ec1] libusb: debug [usbi_handle_transfer_cancellation] detected timeout cancellation
[ 1.002015] [00005ec1] libusb: debug [arm_timer_for_next_timeout] no timeouts, disarming timer
[ 1.002016] [00005ec1] libusb: debug [usbi_handle_transfer_completion] transfer 0x5555559f19b0 has callback 0x7ffff79f1100
[ 1.002017] [00005ec1] libusb: debug [sync_transfer_cb] actual_length=0
[ 1.002019] [00005ec1] libusb: debug [libusb_free_transfer] transfer 0x5555559f19b0
Failed to send data. libusb_bulk_transfer error: LIBUSB_ERROR_TIMEOUT

USB 设备信息:

Bus 001 Device 004: ID 19a2:2001 SICK AG SICK Safety Device
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass          255 Vendor Specific Class
  bDeviceSubClass         0 
  bDeviceProtocol       255 
  bMaxPacketSize0        64
  idVendor           0x19a2 
  idProduct          0x2001 
  bcdDevice            0.00
  iManufacturer           1 SICK AG
  iProduct                2 SICK Safety Device
  iSerial                 4 109445223214336
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0020
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xc0
      Self Powered
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              3 SOPAS
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x85  EP 5 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x05  EP 5 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
Device Qualifier (for other device speed):
  bLength                10
  bDescriptorType         6
  bcdUSB               2.00
  bDeviceClass          255 Vendor Specific Class
  bDeviceSubClass         0 
  bDeviceProtocol       255 
  bMaxPacketSize0        64
  bNumConfigurations      1
Device Status:     0x0001
  Self Powered

尝试改变端点、超时时间和传输大小,但都没有改变错误。

ubuntu libusb libusb-1.0
1个回答
0
投票

我找到了解决方案,代码没有问题,我需要在打开传感器电源之前连接USB。现在我可以毫无问题地从传感器发送和接收数据。

© www.soinside.com 2019 - 2024. All rights reserved.