HarmonyOS 鸿蒙Next中USB中断传输开发

HarmonyOS 鸿蒙Next中USB中断传输开发 有没有USB 中断传输的相关demo?我按教程编写代码 能正常获取USB设备信息

但是 发送数据的时候 发送的是有数据的 但是返回空数据 特别奇怪 而且是超时的 唉

let data = new Uint8Array([0x4b, 0x27, 0x00, 0x00, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00])
try {
  // 通信接口注册成功,传输数据
  let transferParams: usbManager.UsbDataTransferParams = {
    devPipe: devicePipe,
    flags: usbManager.UsbTransferFlags.USB_TRANSFER_SHORT_NOT_OK,
    endpoint: usbEndpoint.address,
    type: usbManager.UsbEndpointTransferType.TRANSFER_TYPE_INTERRUPT,
    timeout: 2000,
    length: data.length,
    callback: () => {
    },
    userData: data,
    buffer: new Uint8Array(11),
    isoPacketCount: 2,
  };
  transferParams.callback = (err: Error, callBackData: usbManager.SubmitTransferCallback) => {
    console.info(`callBackData = ${callBackData}`);
    console.info(`transfer success,result = ${transferParams.buffer}`);
  }
  usbManager.usbSubmitTransfer(transferParams);
  console.info('USB transfer request submitted.');
} catch (error) {
  console.error(`USB transfer failed: ${error}`);
}

USB设备信息

[{
   "name": "1-6",
   "serial": "",
   "manufacturerName": "KTMicro",
   "productName": "xxx",
   "version": "0200",
   "vendorId": 12722,
   "productId": 257,
   "clazz": 0,
   "subClass": 0,
   "protocol": 0,
   "devAddress": 6,
   "busNum": 1,
   "configs": [{
      "id": 1,
      "attributes": 160,
      "isRemoteWakeup": true,
      "isSelfPowered": true,
      "maxPower": 100,
      "name": " ",
      "interfaces": [{
         "id": 0,
         "protocol": 0,
         "clazz": 1,
         "subClass": 1,
         "alternateSetting": 0,
         "name": " ",
         "endpoints": []
      }, {
         "id": 1,
         "protocol": 0,
         "clazz": 1,
         "subClass": 2,
         "alternateSetting": 0,
         "name": " ",
         "endpoints": []
      }, {
         "id": 1,
         "protocol": 0,
         "clazz": 1,
         "subClass": 2,
         "alternateSetting": 1,
         "name": " ",
         "endpoints": [{
            "address": 1,
            "attributes": 9,
            "interval": 1,
            "maxPacketSize": 384,
            "direction": 0,
            "number": 1,
            "type": 1,
            "interfaceId": 1
         }]
      }, {
         "id": 1,
         "protocol": 0,
         "clazz": 1,
         "subClass": 2,
         "alternateSetting": 2,
         "name": " ",
         "endpoints": [{
            "address": 1,
            "attributes": 9,
            "interval": 1,
            "maxPacketSize": 576,
            "direction": 0,
            "number": 1,
            "type": 1,
            "interfaceId": 1
         }]
      }, {
         "id": 2,
         "protocol": 0,
         "clazz": 3,
         "subClass": 0,
         "alternateSetting": 0,
         "name": " ",
         "endpoints": [{
            "address": 130,
            "attributes": 3,
            "interval": 1,
            "maxPacketSize": 16,
            "direction": 128,
            "number": 2,
            "type": 3,
            "interfaceId": 2
         }, {
            "address": 2,
            "attributes": 3,
            "interval": 1,
            "maxPacketSize": 16,
            "direction": 0,
            "number": 2,
            "type": 3,
            "interfaceId": 2
         }]
      }]
   }]
}]

更多关于HarmonyOS 鸿蒙Next中USB中断传输开发的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

开发者您好,您可参考官网demo:USBManagerSample

如果运行仍有报错,麻烦参考官网demo运行后提供一份hilog日志:

hdc shell hilog > test.log

业务执行完成复现问题后,在命令行中按快捷键Ctrl+C,结束任务,然后在对应路径下找到相关日志分析并给出复现问题的时间点。

更多关于HarmonyOS 鸿蒙Next中USB中断传输开发的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,USB中断传输开发主要涉及使用@ohos.usbV9接口。开发者需通过usb.getDevices()获取设备列表,选择目标设备后调用usb.connectDevice()建立连接。随后,通过usb.claimInterface()声明接口,并使用usb.interruptTransfer()执行中断传输。该API支持同步和异步模式,开发者需配置端点地址、传输方向及数据缓冲区。传输完成后,需调用usb.releaseInterface()usb.closeDevice()释放资源。整个过程需在设备配置文件module.json5中声明USB权限。

从你提供的设备信息来看,你的USB设备是一个复合设备,包含了多个接口。你代码中使用的端点地址是 usbEndpoint.address,但根据设备描述符,中断传输端点应该位于接口ID为2的HID类接口中。

具体来说,接口ID 2(Class 3, SubClass 0)有两个中断端点:

  • 端点地址 130(0x82):IN端点,方向为128(输入)
  • 端点地址 2(0x02):OUT端点,方向为0(输出)

你的代码问题可能在于:

  1. 端点选择错误:你使用的 usbEndpoint.address 可能不是正确的中断端点。需要确认你获取的是接口ID 2下的端点。

  2. 传输方向不匹配:从你的代码看,你似乎在进行OUT传输(发送数据),但你的 buffer 参数设置的是接收缓冲区。对于OUT传输,应该使用 userData 作为发送数据,而 buffer 参数可能不需要设置或设置为null。

  3. 端点类型确认:确保你选择的端点 type 值为3(中断传输),这在你的设备描述中是正确的。

建议修改步骤:

首先,确保正确获取中断端点:

// 获取接口ID为2的接口
let interface = device.configs[0].interfaces.find(intf => intf.id === 2);
// 获取OUT端点(地址2)
let endpoint = interface.endpoints.find(ep => ep.address === 2 && ep.direction === 0);

然后调整传输参数:

let transferParams: usbManager.UsbDataTransferParams = {
    devPipe: devicePipe,
    flags: usbManager.UsbTransferFlags.USB_TRANSFER_SHORT_NOT_OK,
    endpoint: endpoint.address, // 使用正确的端点地址
    type: usbManager.UsbEndpointTransferType.TRANSFER_TYPE_INTERRUPT,
    timeout: 2000,
    length: data.length,
    callback: (err: Error, callBackData: usbManager.SubmitTransferCallback) => {
        if (err) {
            console.error(`Transfer error: ${err}`);
        } else {
            console.info(`Transfer success, transferred: ${callBackData.actualLength}`);
        }
    },
    userData: data, // 发送的数据
    buffer: null, // OUT传输时buffer可以为null
    isoPacketCount: 0, // 非等时传输设为0
};

另外,检查是否已正确声明了HID接口的使用权限,并在传输前确保接口已被正确claim。

关于demo,目前HarmonyOS Next的USB完整示例可以参考官方文档中的USB开发指南,其中包含了控制传输、批量传输和中断传输的示例代码片段。

回到顶部