• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.usbManager.serial (串口管理)(系统接口)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: USB-->
5<!--Owner: @hwymlgitcode-->
6<!--Designer: @w00373942-->
7<!--Tester: @dong-dongzhen-->
8<!--Adviser: @w_Machine_cc-->
9
10本模块主要提供串口管理功能,包括打开和关闭设备的串口、写入和读取数据、设置和获取串口的配置参数、权限管理等。
11
12> **说明:**
13>
14> 本模块首批接口从API version 19开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15>
16> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.usbManager.serial (串口管理)](js-apis-serialManager.md)。
17
18## 导入模块
19
20```ts
21import { serialManager } from '@kit.BasicServicesKit';
22```
23
24## serialManager.addSerialRight
25
26addSerialRight(tokenId: number, portId: number): void
27
28为应用程序添加访问串口设备权限。
29
30serialManager.requestSerialRight会触发弹窗请求用户授权;addSerialRight不会触发弹窗,而是直接添加应用程序访问设备的权限。应用退出自动移除对串口设备的访问权限,在应用重启后需要重新申请授权。
31
32**系统接口:** 此接口为系统接口
33
34**需要权限:**  ohos.permission.MANAGE_USB_CONFIG
35
36**系统能力:**  SystemCapability.USB.USBManager.Serial
37
38**参数:**
39
40| 参数名     | 类型     | 必填 | 说明                                  |
41|---------|--------|----|-------------------------------------|
42| tokenId | number | 是  | 需要访问权限的tokenId。                  |
43| portId  | number | 是  | 端口号。 |
44
45**错误码:**
46
47以下错误码的详细介绍参见[USB服务错误码](errorcode-usb.md)。
48
49| 错误码ID | 错误信息                                                     |
50| -------- | ------------------------------------------------------------ |
51| 201      | Permission verification failed. The application does not have the permission required to call the API. |
52| 202      | Permission verification failed. A non-system application calls a system API. |
53| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
54| 14400005 | Database operation exception. |
55| 31400001 | Serial port management exception. |
56| 31400003 | Device does not exist. |
57
58**示例:**
59```ts
60import { bundleManager } from '@kit.AbilityKit';
61import { BusinessError } from '@kit.BasicServicesKit';
62import { JSON } from '@kit.ArkTS';
63import { serialManager } from '@kit.BasicServicesKit';
64
65// 获取串口列表
66let portList: serialManager.SerialPort[] = serialManager.getPortList();
67console.info('portList: ', JSON.stringify(portList));
68if (portList === undefined || portList.length === 0) {
69  console.info('portList is empty');
70  return;
71}
72
73let portId: number = portList[0].portId;
74// 串口增加权限
75let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
76bundleManager.getBundleInfoForSelf(bundleFlags).then((bundleInfo) => {
77  console.info('getBundleInfoForSelf successfully. Data: %{public}s', JSON.stringify(bundleInfo));
78  let tokenId = bundleInfo.appInfo.accessTokenId;
79  try {
80    serialManager.addSerialRight(tokenId, portId);
81    console.info('addSerialRight success, portId: ' + portId);
82  } catch (error) {
83    console.error('addSerialRight error, ' + JSON.stringify(error));
84  }
85}).catch((err : BusinessError) => {
86  console.error('getBundleInfoForSelf failed');
87});
88```
89