Почему libusb может писать работает, но читать не удается с помощью libusb_interrupt_transfer?

Код очень интуитивно понятен. Я уверен, что параметры правильные, потому что он работает, используя hid api окон с такими же параметрами. Я проанализировал логи libusb и нашел информацию об ошибке, которая указывает на ручку передачи, отмененную hid_abort_transfers является недействительным. Тогда это приводит к libusb_interrupt_transfer блокировка на 60 секунд снова и снова.

Вот мой код:

struct libusb_context*  hContxt;

libusb_init(&hContxt);

libusb_set_debug(hContxt, LIBUSB_LOG_LEVEL_DEBUG);

libusb_device_handle*   hUsb = libusb_open_device_with_vid_pid(hContxt, 0x0483, 0x3021);;

if (NULL == hUsb){
    printf("open failed!\n");
    return 0;
}

libusb_claim_interface(hUsb, 0);

int MaxPackSize = libusb_get_max_packet_size(libusb_get_device(hUsb), 0x01);

unsigned char _szCmd[1024] = "12355666";
int TransferedLen = 0;

int r = libusb_interrupt_transfer(hUsb, 0x01, _szCmd, MaxPackSize, &TransferedLen, 200);

if (r == LIBUSB_SUCCESS){
    printf("write Len: %d\n", TransferedLen);
}
else{
    printf("write error:<%s>\n", libusb_error_name(r));
}

unsigned char recv[1024];
int     recvExpectLen = 0;
int     recvActualLen = 0;

MaxPackSize = libusb_get_max_packet_size(libusb_get_device(hUsb), 0x81);

libusb_clear_halt(hUsb, 0x81);

while(1)
{
    memset(recv, 0x00, sizeof recv);

    r = libusb_interrupt_transfer(hUsb, 0x81,\
        const_cast<unsigned char*>(recv), MaxPackSize, &recvActualLen, 500);

    if (r == LIBUSB_SUCCESS){
        printf("recv Len: %d\n", recvActualLen);
    }
    else{
        printf("recv error:<%s>\n", libusb_error_name(r));
        break;
    }

    Sleep(1000);
}

Вот логи:

[timestamp] [threadID] facility level [function call] <message>
--------------------------------------------------------------------------------

[ 0.031200] [00000ff4] libusb: debug [libusb_get_device_list]
[ 0.218400] [00000ff4] libusb: debug [winusb_get_device_list] found new PnP enum
erator string 'USB'
[ 0.218400] [00000ff4] libusb: debug [winusb_get_device_list] allocating new dev
ice for session [20B8]
[ 0.218400] [00000ff4] libusb: debug [winusb_get_device_list] allocating new dev
ice for session [20EC]
[ 0.218400] [00000ff4] libusb: debug [winusb_get_device_list] allocating new dev
ice for session [2128]
[ 0.234000] [00000ff4] libusb: debug [get_api_type] driver(s): BTHUSB
[ 0.234000] [00000ff4] libusb: debug [winusb_get_device_list] allocating new dev
ice for session [800]
[ 0.234000] [00000ff4] libusb: debug [get_api_type] driver(s): usbccgp
[ 0.234000] [00000ff4] libusb: debug [get_api_type] matched driver name against
Composite API
[ 0.234000] [00000ff4] libusb: debug [winusb_get_device_list] allocating new dev
ice for session [1CF0]
[ 0.234000] [00000ff4] libusb: debug [get_api_type] driver(s): usbccgp
[ 0.234000] [00000ff4] libusb: debug [get_api_type] matched driver name against
Composite API
[ 0.234000] [00000ff4] libusb: debug [winusb_get_device_list] allocating new dev
ice for session [1D44]
[ 0.234000] [00000ff4] libusb: debug [enumerate_hcd_root_hub] assigning HCD 'PCI
\VEN_15AD&DEV_0770&SUBSYS_077015AD&REV_00\4&3AD87E0A&0&1888' bus number 1
[ 0.234000] [00000ff4] libusb: debug [enumerate_hcd_root_hub] assigning HCD 'PCI
\VEN_15AD&DEV_0774&SUBSYS_197615AD&REV_00\4&3AD87E0A&0&0088' bus number 2
[ 0.265200] [00000ff4] libusb: debug [init_device] (bus: 2, addr: 0, depth: 0, p
ort: 0): 'USB\ROOT_HUB\5&3BB57B&0'
[ 0.265200] [00000ff4] libusb: debug [init_device] (bus: 1, addr: 0, depth: 0, p
ort: 0): 'USB\ROOT_HUB20\5&299E1C9F&0'
[ 0.280800] [00000ff4] libusb: debug [winusb_get_device_list] found existing dev
ice for session [1CF0]
[ 0.280800] [00000ff4] libusb: debug [init_device] found 1 configurations (activ
e conf: 1)
[ 0.561601] [00000ff4] libusb: debug [cache_config_descriptors] cached config de
scriptor 0 (bConfigurationValue=1, 82 bytes)
[ 0.561601] [00000ff4] libusb: debug [init_device] (bus: 2, addr: 4, depth: 2, p
ort: 2): 'USB\VID_0483&PID_3021\BAD_STRING_INDEX'
[ 0.577201] [00000ff4] libusb: debug [winusb_get_device_list] found existing dev
ice for session [2128]
[ 0.592801] [00000ff4] libusb: debug [init_device] found 1 configurations (activ
e conf: 1)
[ 0.624001] [00000ff4] libusb: debug [cache_config_descriptors] cached config de
scriptor 0 (bConfigurationValue=1, 25 bytes)
[ 0.639601] [00000ff4] libusb: debug [init_device] (bus: 2, addr: 2, depth: 1, p
ort: 2): 'USB\VID_0E0F&PID_0002\6&B77DA92&0&2'
[ 0.670801] [00000ff4] libusb: debug [winusb_get_device_list] found existing dev
ice for session [1D44]
[ 0.702001] [00000ff4] libusb: debug [init_device] found 1 configurations (activ
e conf: 1)
[ 0.733201] [00000ff4] libusb: debug [cache_config_descriptors] cached config de
scriptor 0 (bConfigurationValue=1, 59 bytes)
[ 0.733201] [00000ff4] libusb: debug [init_device] (bus: 2, addr: 1, depth: 1, p
ort: 1): 'USB\VID_0E0F&PID_0003\6&B77DA92&0&1'
[ 0.764401] [00000ff4] libusb: debug [winusb_get_device_list] found existing dev
ice for session [800]
[ 0.764401] [00000ff4] libusb: debug [init_device] found 1 configurations (activ
e conf: 1)
[ 0.795601] [00000ff4] libusb: debug [cache_config_descriptors] cached config de
scriptor 0 (bConfigurationValue=1, 177 bytes)
[ 0.795601] [00000ff4] libusb: debug [init_device] (bus: 2, addr: 3, depth: 2, p
ort: 1): 'USB\VID_0E0F&PID_0008\000650268328'
[ 0.826801] [00000ff4] libusb: debug [winusb_get_device_list] setting composite
interface for [1D44]:
[ 0.826801] [00000ff4] libusb: debug [set_composite_interface] interface[0] = \\
.\HID#VID_0E0F&PID_0003&MI_00#8&17BE0303&0&0000#{4D1E55B2-F16F-11CF-88CB-0011110
00030}
[ 0.842401] [00000ff4] libusb: debug [winusb_get_device_list] setting composite
interface for [1D44]:
[ 0.842401] [00000ff4] libusb: debug [set_composite_interface] interface[1] = \\
.\HID#VID_0E0F&PID_0003&MI_01#8&2F818F48&0&0000#{4D1E55B2-F16F-11CF-88CB-0011110
00030}
[ 0.873601] [00000ff4] libusb: debug [winusb_get_device_list] setting composite
interface for [1CF0]:
[ 0.889201] [00000ff4] libusb: debug [set_composite_interface] interface[0] = \\
.\HID#VID_0483&PID_3021&MI_00#9&343DDB5F&0&0000#{4D1E55B2-F16F-11CF-88CB-0011110
00030}
[ 0.904801] [00000ff4] libusb: debug [winusb_get_device_list] setting composite
interface for [1CF0]:
[ 0.904801] [00000ff4] libusb: debug [set_composite_interface] interface[1] = \\
.\HID#VID_0483&PID_3021&MI_01#9&1C7A4F1A&0&0000#{4D1E55B2-F16F-11CF-88CB-0011110
00030}
[ 0.920401] [00000ff4] libusb: debug [libusb_get_device_descriptor]
[ 0.936001] [00000ff4] libusb: debug [libusb_get_device_descriptor]
[ 0.951601] [00000ff4] libusb: debug [libusb_get_device_descriptor]
[ 0.967201] [00000ff4] libusb: debug [libusb_open] open 2.4
[ 0.967201] [00000ff4] libusb: warning [hid_open] could not open HID device in R
/W mode (keyboard or mouse?) - trying without
[ 0.982801] [00000ff4] libusb: debug [hid_open] set maximum input buffer size to
 512
[ 0.998401] [00000ff4] libusb: debug [hid_open] 0 HID input report value(s) foun
d
[ 0.998401] [00000ff4] libusb: debug [hid_open] 0 HID output report value(s) fou
nd
[ 1.014001] [00000ff4] libusb: debug [hid_open] 0 HID feature report value(s) fo
und
[ 1.170002] [00000ff4] libusb: debug [libusb_unref_device] destroy device 1.0
[ 1.170002] [00000ff4] libusb: debug [libusb_unref_device] destroy device 2.1
[ 1.185602] [00000ff4] libusb: debug [libusb_unref_device] destroy device 2.3
[ 1.185602] [00000ff4] libusb: debug [libusb_claim_interface] interface 0
[ 1.201202] [00000ff4] libusb: debug [hid_claim_interface] claimed interface 0
[ 1.216802] [00000ff4] libusb: debug [parse_configuration] skipping descriptor 0
xb
[ 1.216802] [00000ff4] libusb: debug [parse_endpoint] skipping descriptor b
[ 1.232402] [00000ff4] libusb: debug [windows_assign_endpoints] (re)assigned end
point 01 to interface 0
[ 1.248002] [00000ff4] libusb: debug [windows_assign_endpoints] (re)assigned end
point 81 to interface 0
[ 1.263602] [00000ff4] libusb: debug [parse_configuration] skipping descriptor 0
xb
[ 1.263602] [00000ff4] libusb: debug [parse_endpoint] skipping descriptor b
[ 1.279202] [00000ff4] libusb: debug [libusb_alloc_transfer] transfer 005D4628
[ 1.279202] [00000ff4] libusb: debug [libusb_submit_transfer] transfer 005D4628
[ 1.294802] [00000ff4] libusb: debug [usbi_add_pollfd] add fd 2 events 4
[ 1.310402] [00000ff4] libusb: debug [hid_submit_bulk_transfer] matched endpoint
 01 with interface 0
[ 1.326002] [00000ff4] libusb: debug [hid_submit_bulk_transfer] writing 65 bytes
 (report ID: 0x00)
[ 1.341602] [00000ff4] libusb: debug [libusb_get_next_timeout] next timeout in 0
.157200s
[ 1.341602] [00000ff4] libusb: debug [libusb_handle_events_timeout_completed] do
ing our own event handling
[ 1.357202] [00000ff4] libusb: debug [handle_events] poll fds modified, realloca
ting
[ 1.372802] [00000ff4] libusb: debug [handle_events] poll() 2 fds with timeout i
n 158ms
[ 1.372802] [00000ff4] libusb: debug [handle_events] poll() returned 1
[ 1.372802] [00000ff4] libusb: debug [windows_handle_events] checking fd 2 with
revents = 0004
[ 1.388402] [00000ff4] libusb: debug [usbi_remove_pollfd] remove fd 2
[ 1.404002] [00000ff4] libusb: debug [windows_transfer_callback] handling I/O co
mpletion with errcode 0, size 65
[ 1.404002] [00000ff4] libusb: debug [usbi_handle_transfer_completion] transfer
005D4628 has callback 71CCAD40
[ 1.419602] [00000ff4] libusb: debug [sync_transfer_cb] actual_length=65
[ 1.435202] [00000ff4] libusb: debug [libusb_free_transfer] transfer 005D4628
write Len: 65
[ 1.450802] [00000ff4] libusb: debug [parse_configuration] skipping descriptor 0
xb
[ 1.466402] [00000ff4] libusb: debug [parse_endpoint] skipping descriptor b
[ 1.466402] [00000ff4] libusb: debug [libusb_clear_halt] endpoint 81
[ 1.482002] [00000ff4] libusb: debug [hid_clear_halt] matched endpoint 81 with i
nterface 0
[ 1.497602] [00000ff4] libusb: debug [libusb_alloc_transfer] transfer 005D4628
[ 1.497602] [00000ff4] libusb: debug [libusb_submit_transfer] transfer 005D4628
[ 1.513202] [00000ff4] libusb: debug [usbi_add_pollfd] add fd 2 events 1
[ 1.528802] [00000ff4] libusb: debug [hid_submit_bulk_transfer] matched endpoint
 81 with interface 0
[ 1.528802] [00000ff4] libusb: debug [hid_submit_bulk_transfer] reading 65 bytes
 (report ID: 0x00)
[ 1.544402] [00000ff4] libusb: debug [libusb_get_next_timeout] next timeout in 0
.467900s
[ 1.560002] [00000ff4] libusb: debug [libusb_handle_events_timeout_completed] do
ing our own event handling
[ 1.560002] [00000ff4] libusb: debug [handle_events] poll fds modified, realloca
ting
[ 1.575602] [00000ff4] libusb: debug [handle_events] poll() 2 fds with timeout i
n 468ms
[ 2.059203] [00000ff4] libusb: debug [handle_events] poll() returned 0
[ 2.059203] [00000ff4] libusb: debug [libusb_cancel_transfer] transfer 005D4628
[ 2.074803] [00000ff4] libusb: debug [hid_abort_transfers] will use interface 0
[ 2.090403] [00000ff4] libusb: warning [hid_abort_transfers] cancel failed: [6]
句柄无效。
[ 2.106003] [00000ff4] libusb: debug [libusb_cancel_transfer] cancel transfer fa
iled error -5
[ 2.106003] [00000ff4] libusb: warning [handle_timeout] async cancel failed -5 e
rrno=0
[ 2.121603] [00000ff4] libusb: debug [libusb_get_next_timeout] no URB with timeo
ut or all handled by OS; no timeout!
[ 2.137203] [00000ff4] libusb: debug [libusb_handle_events_timeout_completed] do
ing our own event handling
[ 2.137203] [00000ff4] libusb: debug [handle_events] poll() 2 fds with timeout i
n 60000ms
[62.166109] [00000ff4] libusb: debug [handle_events] poll() returned 0
[62.166109] [00000ff4] libusb: debug [libusb_get_next_timeout] no URB with timeo
ut or all handled by OS; no timeout!
[62.181709] [00000ff4] libusb: debug [libusb_handle_events_timeout_completed] do
ing our own event handling
[62.197309] [00000ff4] libusb: debug [handle_events] poll() 2 fds with timeout i
n 60000ms
[122.210614] [00000ff4] libusb: debug [handle_events] poll() returned 0
[122.210614] [00000ff4] libusb: debug [libusb_get_next_timeout] no URB with time
out or all handled by OS; no timeout!
[122.226214] [00000ff4] libusb: debug [libusb_handle_events_timeout_completed] d
oing our own event handling
[122.226214] [00000ff4] libusb: debug [handle_events] poll() 2 fds with timeout
in 60000ms
[182.255120] [00000ff4] libusb: debug [handle_events] poll() returned 0
[182.255120] [00000ff4] libusb: debug [libusb_get_next_timeout] no URB with time
out or all handled by OS; no timeout!
[182.270720] [00000ff4] libusb: debug [libusb_handle_events_timeout_completed] d
oing our own event handling
[182.286320] [00000ff4] libusb: debug [handle_events] poll() 2 fds with timeout
in 60000ms

Должен ли я установить что-то с другими API? Сталкивался ли кто-нибудь еще с такой проблемой?

0 ответов

Другие вопросы по тегам