我正在编写一个控制台程序来使用 libusb 函数与 USB 设备进行通信。
该设备已正确枚举,并且有两个端点:
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);
现在我看到数据结束了命令的执行。