讨论广场 问答详情
Purple pi OH5.0app调用ttyS0串口问题,命令行root用户可以操作,app无法调用
ycf_119 2025-04-15 14:33:42
40 评论 分享

用napi写可串口的open。返回失败。  /dev/ttyS0给了777权限    selinux是

# getenforce
Permissive

 

hilog:

04-15 13:28:17.443  7043  7043 I C03900/Ace: [ui_content_impl.cpp(1608)-(-2:-1:undefined)] [com.example.testnative][entry][-1]: SetLanguage: zh, colorMode: light, deviceAccess: 0
04-15 13:28:17.443  7043  7043 I C03900/Ace: [ui_content_impl.cpp(1636)-(-2:-1:undefined)] [com.example.testnative][entry][-1]: Initialize UIContent isModelJson:true
04-15 13:28:17.528  7043  7043 I C03900/Ace: [ui_content_impl.cpp(2103)-(100000:100000:scope)] [com.example.testnative][entry][100000]: window focus
04-15 13:28:17.531   517   918 I C01120/BMS: [bundle_mgr_host.cpp(HandleDumpInfos:1817)]dump info com.example.testnative
04-15 13:28:17.532  7043  7043 I C03900/Ace: [ui_content_impl.cpp(1928)-(100000:100000:scope)] [com.example.testnative][entry][100000]: SetMinPlatformVersion is 12
04-15 13:28:17.538  7043  7043 I C03900/Ace: [ui_content_impl.cpp(803)-(-2:100000:singleton)] [com.example.testnative][entry][100000]: Initialize: pages/Index
04-15 13:28:17.538  7043  7043 I C03900/Ace: [ui_content_impl.cpp(2483)-(-2:100000:singleton)] [com.example.testnative][entry][100000]: UpdateWindowMode: 1, hasDeco: 0
04-15 13:28:17.539  7043  7043 I C03900/Ace: [ui_content_impl.cpp(2491)-(-2:100000:singleton)] [com.example.testnative][entry][100000]: UpdateWindowVisible: 0, hasDeco: 0
04-15 13:28:17.539  7043  7043 I C03900/Ace: [ui_content_impl.cpp(2048)-(-2:100000:singleton)] [com.example.testnative][entry][100000]: window foreground
04-15 13:28:17.541  7043  7043 I C03900/Ace: [ui_content_impl.cpp(2365)-(-2:100000:singleton)] [com.example.testnative][entry][100000]: UpdateViewportConfig Viewport config: size: (800, 1280) orientation: 0 density: 1.500000 position: (0, 0) transformHint: 0
04-15 13:28:17.547  7043  7043 I C04200/JsWindowStage: <338>[NAPI]Window [33, testnative0] load content end, ret = 0
04-15 13:28:17.572   517  6994 W C01311/AppMS: [app_mgr_service_inner.cpp:5433]FaultData is: name: Error, faultType: 1, uid: 20010042, pid: 7043,bundleName: com.example.testnative, faultData.forceExit==0, faultData.waitSaveState==0
04-15 13:28:17.574  7043  7043 I C01317/AppKit: [main_thread.cpp:1542]hisysevent write result=0, send event [FRAMEWORK,PROCESS_KILL], pid=7043, processName=com.example.testnative, msg=Kill Reason:Js Error
04-15 13:28:17.574  7043  7043 E C01317/AppKit: com.example.testnative is about to exit due to RuntimeError
04-15 13:28:17.576   249   478 I C02d11/Faultlogger: OnEvent: Receive JS_ERROR Event:{"domain_":"AAFWK","name_":"JS_ERROR","type_":1,"time_":1744694897564,"tz_":"+0800","pid_":7043,"tid_":7043,"uid_":20010042,"log_":1,"id_":"03973487401224633202","PACKAGE_NAME":"com.example.testnative","VERSION":"1000000","TYPE":3,"HAPPEN_TIME":1744694897,"REASON":"Error","JSVM":"ARK","SUMMARY":"Error name:Error\nError message:Failed to open serial port\nSourceCode:\n        let fd = testNapi.openSerialPort(\"/dev/ttyS0\");\n                 ^\nStacktrace:\n    at aboutToAppear (entry/src/main/ets/pages/Index.ets:15:14)\n","APP_RUNNING_UNIQUE_ID":"143915004","tag_":"STABILITY","level_":"CRITICAL"}.
04-15 13:28:17.576   249   478 I C02d11/Faultlogger: AddFaultLogIfNeed: Start saving Faultlog of Process:7043, Name:com.example.testnative, Reason:Error.
04-15 13:28:17.578   249   478 I C02d12/Sanitizer: bundleName of uid:20010042 is com.example.testnative
04-15 13:28:17.583   249   478 I C02d12/Sanitizer: The version of com.example.testnative is 1.0.0
04-15 13:28:17.606   249   478 I C02d11/FaultLogManager: SaveFaultLogToFile: create log jscrash-com.example.testnative-20010042-20250415132817
04-15 13:28:17.606   249   478 I C02d11/Faultlogger: ModuleName:com.example.testnative
04-15 13:28:17.608   249   478 I C02d10/ExtractRule: IsMatchPath: sourceFile is /data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817, name is /data/log/faultlog/, pattern is JS_ERROR\d+.
04-15 13:28:17.608   249   478 I C02d10/ExtractRule: IsMatchPath: sourceFile is /data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817, name is /data/log/faultlog/, pattern is JS_ERROR\d+.
04-15 13:28:17.609   249   478 I C02d10/ExtractRule: IsMatchPath: sourceFile is /data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817, name is /data/log/faultlog/faultlogger/, pattern is jscrash-.*-\d+-\d+.
04-15 13:28:17.609   249   478 I C02d10/ExtractRule: IsMatchPath: desPath is /data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817.
04-15 13:28:17.609   249   478 I C02d10/ExtractRule: IsMatchPath: sourceFile is /data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817, name is /data/log/faultlog/faultlogger/, pattern is jscrash-.*-\d+-\d+.
04-15 13:28:17.609   249   478 I C02d10/ExtractRule: IsMatchPath: desPath is /data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817.
04-15 13:28:17.611   249   478 I C02d10/FeatureAnalysis: AnalysisLog: <1744694897> begin analysis </data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817>, eventType is <JS_ERROR>.
04-15 13:28:17.612   249   478 I C02d10/FeatureAnalysis: AnalysisLog: <1744694897> begin analysis </data/log/faultlog/faultlogger/jscrash-com.example.testnative-20010042-20250415132817>, eventType is <JS_ERROR>.
04-15 13:28:17.629   331   922 W C02801/MMIService: [][OnDisconnected:531] Enter, session desc:fd = 46, programName = com.example.testnative, moduleType = 0, opened, uid = 20010042, pid = 7043, tokenType = -1
04-15 13:28:17.631   248   248 W C02c11/APPSPAWN: [appspawn_service.c:144]com.example.testnative with pid 7043 exit with code:254
04-15 13:28:17.631   248   248 E C02c11/APPSPAWN: [appspawn_cgroup.c:147]Open file fail /dev/pids/100/com.example.testnative/app_7043/cgroup.procs errno: 2
04-15 13:28:17.632   523   523 I C01406/OHOS::RS: RSSurfaceRenderNode:SetIsOnTheTree, node:[name: testnative0, id: 30249454665729], on tree: 0, nodeType: 1
04-15 13:28:17.634   517   713 I C04200/Container: <1690>DumpScreenWindowTree: testnative         0    33    1    1    0    3           0            0 [   0    0  800 1280]
04-15 13:28:17.638   517   713 I C01406/OHOS::RS: RSNode::RemoveChild, Id: 2220498092077, SurfaceNode:[Id: 30249454665729, name: testnative0]
04-15 13:28:17.635   517  6993 I C01311/AppMS: [app_running_record.cpp:847]The ability(bundle:com.example.testnative, ability:EntryAbility) state will change.
04-15 13:28:17.645   373  1474 I C01799/MemMgr: AppStateObserver::OnAbilityStateChanged called, uid=20010042, pid=7043, bundleName=com.example.testnative FOREGROUND
04-15 13:28:17.646   373   414 I C01799/MemMgr: BundlePriorityInfo::UpdatePriority bundleName=com.example.testnative, priority=0
04-15 13:28:17.646   373   543 I C01799/MemMgr: ReclaimStrategyManager::HandleAppStateChanged_ pid:7043 bundle:20010042 com.example.testnative userId:100 score:0 action:APP_FOREGROUND
04-15 13:28:17.646   517  6993 I C01311/AppMS: [app_running_manager.cpp:427]processName: com.example.testnative.

 

 

 

 

native部分代码

napi_value OpenSerialPort(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1];
     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
   

  char path[256];
  size_t path_len;
  napi_get_value_string_utf8(env, args[1], path, sizeof(path), &path_len);

  int fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    napi_throw_error(env, NULL, "Failed to open serial port");
        OH_LOG_INFO(LOG_APP, "open device :  b:%{public}s. error", path); 
    return NULL;
  }

  // 配置串口参数
  struct termios options;
  tcgetattr(fd, &options);
  cfsetispeed(&options, B9600);  // 波特率 9600
  cfsetospeed(&options, B9600);
  options.c_cflag |= (CLOCAL | CREAD);  // 本地连接,接收使能
  options.c_cflag &= ~PARENB;           // 无校验
  options.c_cflag &= ~CSTOPB;           // 1位停止位
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;               // 8位数据位
  tcsetattr(fd, TCSANOW, &options);
    OH_LOG_INFO(LOG_APP, "open device :  succeed"); 

  napi_value result;
  napi_create_int32(env, fd, &result);
  return result;
}

 

 

 

 

open失败, 应该是权限问题。  请问大佬。   除了/dev/ttyS0节点权限, selinux权限, 

"requestPermissions"是否还需要设置权限,需要的话设置哪个权限

 

 

40 评论 分享
写回答
全部评论(1)

我之前正常给 /dev/ttyS0权限之后,再执行setenforce 0之后权限方面就没有问题了,你可以用DevEco Studio编译器debug调试看一下具体哪一步出错了,以及出错的原因

2025-04-16 09:33:11