Hey I'm trying to communicate with a sick lidar sensor via usb on Ubuntu 22.04.3 LTS. Currently I'm able to open and claim the port and to send data. But if I try to receive data I'm getting a LIBUSB_ERROR_TIMEOUT. I tried different Endpoints, timeout times (1-6s) and transfer sizes (8-2048) without success. Do you maybe have an idea why it does not work? Thank you :)
My Code:
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 log:
[ 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 device info:
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
Tried to vary endpoints, timeout time and transfer size, none of which changed the error.
I found the solution, nothing is wrong with the code, I needed to connect the USB before powering the sensor on. Now I can send and receive data from the sensor without a problem.