HarmonyOS鸿蒙Next中NAPI开发USB-串口应用(CH340转换),打开/dev/bus下文件无权限

HarmonyOS鸿蒙Next中NAPI开发USB-串口应用(CH340转换),打开/dev/bus下文件无权限 项目需要连接CH340的USB转串口连接

借用一个NAPI开发的例程:SerialPortTestOpenHarmony: 基于openharmony开源鸿蒙的串口调试工具

几经修正后,先是设备文件路径沿用/dev/ttyS0时,提示“No such file or directory”

怎么在c层识别并成功打开ch340的串口驱动-华为开发者问答 | 华为开发者联盟 :提到CH340文件路径可能是/dev/bus/下,尝试修改后,提示Error: Permission denied

native c++ 中这么获取/dev目录下的字符设备文件权限?-华为开发者问答 | 华为开发者联盟:提到修改SELINUX模式。

基础储备不足,无从下手!请问有人遇到类似问题并有解决的经验吗?

真机:

  • 型号名称 HUAWEl MatePad 11.5"S
  • HarmonyOS 版本 5.0.0
  • 型号代号 TGR-W00
  • 软件版本 5.0.0.155(SP6C00E155R2P7patch05)
  • 内核版本HarmonyOS@localhost #1 SMP Fri Apr 1105:17:29 UTC 2025
  • API版本 5.0.4(16)
  • OpenHarmony 版本 OpenHarmony 5.0.1
05-06 15:45:47.149  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  I     Callee constructor is OK string
05-06 15:45:47.149  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  I     Ability::constructor callee is object [object Object]
05-06 15:45:47.162  6854-6854  A00000/com.exa...erial/testTag  com.examp...b_serial  I     Ability onCreate
05-06 15:45:47.181  6854-6854  A00000/com.exa...erial/testTag  com.examp...b_serial  I     Ability onWindowStageCreate
05-06 15:45:47.189  6854-6854  A00000/com.exa...erial/testTag  com.examp...b_serial  I     Ability onForeground
05-06 15:45:47.243  6854-6854  A00000/com.exa...erial/testTag  com.examp...b_serial  I     Succeeded in loading the content.
05-06 15:45:47.278  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart],   onPageShow
05-06 15:45:47.300  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], This send data is FF 55 01 23 88
05-06 15:45:49.540  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], {
                                                                                                 "name": "4-2",
                                                                                                 "serial": "",
                                                                                                 "manufacturerName": " ",
                                                                                                 "productName": "USB Serial",
                                                                                                 "version": "1001",
                                                                                                 "vendorId": 6790,
                                                                                                 "productId": 29987,
                                                                                                 "clazz": 255,
                                                                                                 "subClass": 0,
                                                                                                 "protocol": -1,
                                                                                                 "devAddress": 2,
                                                                                                 "busNum": 4,
                                                                                                 "configs": [
                                                                                                   {
                                                                                                     "id": 1,
                                                                                                     "attributes": 128,
                                                                                                     "isRemoteWakeup": false,
                                                                                                     "isSelfPowered": true,
                                                                                                     "maxPower": 196,
                                                                                                     "name": " ",
                                                                                                     "interfaces": [
                                                                                                       {
                                                                                                         "id": 0,
                                                                                                         "protocol": 2,
                                                                                                         "clazz": 255,
                                                                                                         "subClass": 1,
                                                                                                         "alternateSetting": 0,
                                                                                                         "name": " ",
                                                                                                         "endpoints": [
                                                                                                           {
                                                                                                             "address": 130,
                                                                                                             "attributes": 2,
                                                                                                             "interval": 0,
                                                                                                             "maxPacketSize": 32,
                                                                                                             "direction": 128,
                                                                                                             "number": 2,
                                                                                                             "type": 2,
                                                                                                             "interfaceId": 0
                                                                                                           },
                                                                                                           {
                                                                                                             "address": 2,
                                                                                                             "attributes": 2,
                                                                                                             "interval": 0,
                                                                                                             "maxPacketSize": 32,
                                                                                                             "direction": 0,
                                                                                                             "number": 2,
                                                                                                             "type": 2,
                                                                                                             "interfaceId": 0
                                                                                                           },
                                                                                                           {
                                                                                                             "address": 129,
                                                                                                             "attributes": 3,
                                                                                                             "interval": 1,
                                                                                                             "maxPacketSize": 8,
                                                                                                             "direction": 128,
                                                                                                             "number": 1,
                                                                                                             "type": 3,
                                                                                                             "interfaceId": 0
                                                                                                           }
                                                                                                         ]
                                                                                                       }
                                                                                                     ]
                                                                                                   }
                                                                                                 ]
                                                                                               }
05-06 15:45:56.006  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  I     Select baudrate is :115200
05-06 15:45:56.007  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], This baudrate is 115200
05-06 15:45:56.728  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], 打開串口
05-06 15:45:56.728  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], 串口未打開
05-06 15:45:56.728  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  E     打开串口失败: Error: No such file or directory
05-06 15:46:04.949  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  I     Select port is :/dev/bus/ttyS0
05-06 15:46:04.949  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], This port is /dev/bus/ttyS0
05-06 15:46:06.093  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], 打開串口
05-06 15:46:06.093  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], 串口未打開
05-06 15:46:06.094  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  E     打开串口失败: Error: Permission denied
05-06 15:46:09.556  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  I     Select port is :/dev/bus/ttyUSB0
05-06 15:46:09.556  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], This port is /dev/bus/ttyUSB0
05-06 15:46:10.185  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], 打開串口
05-06 15:46:10.185  6854-6854  A0FF00/com.exa...l/[NativeAPI]  com.examp...b_serial  I     [IdoUart], 串口未打開
05-06 15:46:10.185  6854-6854  A03D00/com.exa..._serial/JSAPP  com.examp...b_serial  E     打开串口失败: Error: Permission denied

更多关于HarmonyOS鸿蒙Next中NAPI开发USB-串口应用(CH340转换),打开/dev/bus下文件无权限的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中NAPI开发USB-串口应用(CH340转换),打开/dev/bus下文件无权限的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中开发USB-串口应用时,若遇到打开/dev/bus下文件无权限的问题,通常是由于权限配置不当所致。解决方法如下:

  1. 检查权限:确保应用在config.json中声明了ohos.permission.USB权限。
  2. 修改SELinux策略:如果系统启用了SELinux,可能需要调整策略以允许应用访问USB设备。
  3. 使用root权限:在开发阶段,可以通过su命令获取root权限进行调试,但生产环境中不建议使用。
  4. 设备节点权限:检查/dev/bus/usb下的设备节点权限,确保应用用户有读写权限。

通过这些步骤,可以有效解决权限问题,确保应用正常访问USB-串口设备。

回到顶部