gpt4 book ai didi

Android 与 USB 到串行设备的通信和 controlTransfers

转载 作者:太空狗 更新时间:2023-10-29 15:04:08 27 4
gpt4 key购买 nike

我搜索了很多帖子,比如这个: Using Android to Communicate with a USB HID Device

但我仍然不明白您如何确定 controlTransfer 调用中的 requestType

public int controlTransfer (int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)

我需要为我的设备设置EVEN 奇偶校验,但它似乎不起作用。这是我的代码:

        UsbDeviceConnection conn = mUsbManager.openDevice(mDevice);
l("Device opened...");

l("Trying to open interface on 0");
UsbInterface deviceInterface = mDevice.getInterface(0);
if (!conn.claimInterface(deviceInterface, true)) {
l("Could not claim interface on 0");
return;
}

int defaultDataBits = 8;
int config = defaultDataBits;
config |= (0x02 << 8); //even parity
config |= (0x00 << 11); //stop bits

conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
conn.controlTransfer(0x40, 0x04, config, 0, null, 0, 0);// set even parity
conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);// set 9600 baud rate

requestType 0x40 对我来说没有任何意义,一些示例在 0x21 或 0x81 或 0xA1...

获取正确请求类型的最佳方法是什么?

我还应该提到,我希望在 PC 上以偶校验接收数据,如果我将 PC 上串行端口的奇偶校验设置为无 - 我接收到预期数据,所以我得出结论,我对设备进行的 controlTransfer 调用不起作用。

这是我尝试从 Android 配置的 USB 转串口设备:

Device Info 
Device Path: /dev/bus/usb/001/002
Device Class: Use class information in the Interface Descriptors (0x0)
Vendor ID: 067b
Vendor Name: Prolific Technology, Inc.
Product ID: 03ea


Interfaces
Interface #0
Class: Vendor Specific (0xff)
Endpoint: #0
Address : 129 (10000001)
Number : 1
Direction : Inbound (0x80)
Type : Intrrupt (0x3)
Poll Interval : 1
Max Packet Size: 10
Attributes : 000000011
Endpoint: #1
Address : 2 (000000010)
Number : 2
Direction : Outbound (0x0)
Type : Bulk (0x2)
Poll Interval : 0
Max Packet Size: 64
Attributes : 000000010
Endpoint: #2
Address : 131 (10000011)
Number : 3
Direction : Inbound (0x80)
Type : Bulk (0x2)
Poll Interval : 0
Max Packet Size: 64
Attributes : 000000010

感谢您的帮助。

最佳答案

这是 USB spec 的一部分,特别是 bmRequestType。这是 C 中的位掩码列表,您可以在项目中自己将这些定义为 static final int。它们有时是在 SDK 或驱动程序包中围绕操作系统定义的,或者您可以只指定原始的十六进制字节(如上所述,但定义的位掩码有利于提高可读性):

/* Setup Data Constants */

#define USB_SETUP_HOST_TO_DEVICE 0x00 // Device Request bmRequestType transfer direction - host to device transfer
#define USB_SETUP_DEVICE_TO_HOST 0x80 // Device Request bmRequestType transfer direction - device to host transfer

#define USB_SETUP_TYPE_STANDARD 0x00 // Device Request bmRequestType type - standard
#define USB_SETUP_TYPE_CLASS 0x20 // Device Request bmRequestType type - class
#define USB_SETUP_TYPE_VENDOR 0x40 // Device Request bmRequestType type - vendor

#define USB_SETUP_RECIPIENT_DEVICE 0x00 // Device Request bmRequestType recipient - device
#define USB_SETUP_RECIPIENT_INTERFACE 0x01 // Device Request bmRequestType recipient - interface
#define USB_SETUP_RECIPIENT_ENDPOINT 0x02 // Device Request bmRequestType recipient - endpoint
#define USB_SETUP_RECIPIENT_OTHER 0x03 // Device Request bmRequestType recipient - other

您需要为请求类型提供三个掩码

  • 方向
  • 实际请求类型
  • 收件人

在您的情况下,0x40 表示从 Android 主机到您连接的设备的供应商请求,设备本身作为接收者(相对于特定端点或接口(interface))。如果您有多个接口(interface),您可能需要指定 USB_SETUP_RECIPIENT_INTERFACE,然后在 index 参数中传递接口(interface)编号以将其放置到正确的位置。但是您的收件人实际上取决于您的设备对数据的期望方式,而 Prolific 可能对此有规范。您可以尝试设置 0x01 位以查看它是否解决了您的问题,可能是该请求专门针对接口(interface) 0,但这可能无关紧要,因为您的设备无论如何只有一个接口(interface)。

请注意,此参数完全特定于 USB,与您设备上的奇偶校验无关,但如果您没有正确获取这些数据包,那么您的设置将不会在设备上生效,如您所见。如果 USB 请求正确,则检查 config 值。

关于Android 与 USB 到串行设备的通信和 controlTransfers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23507905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com