libusbライブラリを使用した通信の問題

libusbライブラリを使用した通信の問題

libusb機能を使用してUSBデバイスと通信するコンソールプログラムを作成しています。
デバイスが正しく列挙され、2つのエンドポイントがあります。

  Endpoint Descriptor:
    bLength                 7
    bDescriptorType         5
    bEndpointAddress     0x81  EP 1 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     0x02  EP 2 OUT
    bmAttributes            2
      Transfer Type            Bulk
      Synch Type               None
      Usage Type               Data
    wMaxPacketSize     0x0040  1x 64 bytes
    bInterval               0

バッチ機能で3バイトを送信しようとしています。

dump( cmdPrinter, nCmdSize, "[usbxsfer] OUT:");
    
ret = libusb_bulk_transfer( devHandle, EP_OUT, 
            (unsigned char*)&cmdPrinter, nCmdSize, &wrote, timeout);
LOG4("[usbxsfer] OUT[x%02X] ret: %d - wrote: %d - pcksize=%d\n", 
    EP_OUT, ret, wrote, nCmdSize);

そして答えを読んでください:

int toRead = 10;
ret = libusb_bulk_transfer(devHandle, EP_IN, (unsigned char*)bufferRx+offs, toRead, &xferred, timeout);
LOG2("[usbxsfer] IN ret: %d - xferred: %d\n", ret, xferred);

デバイスはコマンドを実行せず(一部のタブが表示されます)、応答しません(エラー-7 =タイムアウト(3秒))。

tsharkケーブルにスニッフィングバイトを取り付け、「1D E0 0F」バイト転送を試みました。純粋な猫は次のとおりです。

Frame 3: 67 bytes on wire (536 bits), 67 bytes captured (536 bits) on interface 0
    Interface id: 0 (usbmon0)
        Interface name: usbmon0
    Encapsulation type: USB packets with Linux header and padding (115)
    Arrival Time: Aug 26, 2022 18:11:55.083144000 CEST
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1661530315.083144000 seconds
    [Time delta from previous captured frame: 0.001053000 seconds]
    [Time delta from previous displayed frame: 0.001053000 seconds]
    [Time since reference or first frame: 0.001211000 seconds]
    Frame Number: 3
    Frame Length: 67 bytes (536 bits)
    Capture Length: 67 bytes (536 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: usb]
USB URB
    [Source: host]
    [Destination: 5.6.2]
    URB id: 0xffffffc0774cfe40
    URB type: URB_SUBMIT ('S')
    URB transfer type: URB_BULK (0x03)
    Endpoint: 0x02, Direction: OUT
        0... .... = Direction: OUT (0)
        .... 0010 = Endpoint number: 2
    Device: 6
    URB bus id: 5
    Device setup request: not relevant ('-')
    Data: present (0)
    URB sec: 1661530315
    URB usec: 83144
    URB status: Operation now in progress (-EINPROGRESS) (-115)
    URB length [bytes]: 3
    Data length [bytes]: 3
    [bInterfaceClass: Unknown (0xffff)]
    Unused Setup Header
    Interval: 0
    Start frame: 0
    Copy of Transfer Flags: 0x00000000
    Number of ISO descriptors: 0
Leftover Capture Data: d8abcb

0000  40 fe 4c 77 c0 ff ff ff 53 03 02 06 05 00 2d 00   @.Lw....S.....-.
0010  cb f0 08 63 00 00 00 00 c8 44 01 00 8d ff ff ff   ...c.....D......
0020  03 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00   ................
0030  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
0040  d8 ab cb                                          ...

これはデバイスに送信される最初のパケットでなければなりません。バッファで私のバイトを見ることはできません!とにかく結果は良好で(worte = 3)、3バイトが送信されます。

[2022-08-26 18:11:55] [usbxsfer] devHandle: 0x55a8dbdac0, nCmdSize: 3
[2022-08-26 18:11:55] [usbxsfer] OUT: [3] 1D E0 0F
[2022-08-26 18:11:55] [usbxsfer] OUT[x02] ret: 0 - wrote: 3 - pcksize=3

バッファは正しいようですが、データはバッファにありません。おそらく、これがコマンドが実行されず、応答しない理由です。

どんなアイデアがありますか?

ベストアンサー1

解決しました! !

問題は libusb_bulk_transfer() 呼び出しにありました。ポインタをポインタに渡していました。

正しいコードは次のとおりです。

ret = libusb_bulk_transfer( devHandle, EP_OUT, 
        (unsigned char*)cmdPrinter, nCmdSize, &wrote, timeout);

これで、データがコマンドの実行を終了することがわかります。

おすすめ記事