• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.usbManager.serial (Serial Port Management) (system API)
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
10This module provides the serial port management features, including enabling and disabling the serial port of the device, writing and reading data, setting and obtaining the configuration parameters of the serial port, and managing permissions.
11
12> **NOTE**
13>
14> The initial APIs of this module are supported since API version 19. Newly added APIs will be marked with a superscript to indicate their earliest API version.
15>
16> This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.usbManager.serial (USB Manager)](js-apis-serialManager.md).
17
18## Modules to Import
19
20```ts
21import { serialManager } from '@kit.BasicServicesKit';
22```
23
24## serialManager.addSerialRight
25
26addSerialRight(tokenId: number, portId: number): void
27
28Adds the permission to an application for accessing the serial port device.
29
30**serialManager.requestSerialRight** triggers a dialog box to request user authorization. **addSerialRight** does not trigger a dialog box but directly adds the device access permission for the application. After the application exits, the access permission on the serial port device is automatically removed. After the application is restarted, you need to request the permission again.
31
32**System API**: This is a system API.
33
34**Required permissions**: ohos.permission.MANAGE_USB_CONFIG
35
36**System capability**: SystemCapability.USB.USBManager.Serial
37
38**Parameters**
39
40| Name    | Type    | Mandatory| Description                                 |
41|---------|--------|----|-------------------------------------|
42| tokenId | number | Yes | ID of the token that requires the access permission.                 |
43| portId  | number | Yes | Port number.|
44
45**Error codes**
46
47For details about the error codes, see [USB Service Error Codes](errorcode-usb.md).
48
49| ID| Error Message                                                    |
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**Example**
59```ts
60import { bundleManager } from '@kit.AbilityKit';
61import { BusinessError } from '@kit.BasicServicesKit';
62import { JSON } from '@kit.ArkTS';
63import { serialManager } from '@kit.BasicServicesKit';
64
65// Obtain the serial port list.
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// Add permissions to the serial port.
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