We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
ReferencesSystemBasic FunctionsBasic Services KitArkTS APIDevice Management@ohos.busManager.serial (Serial Port Management)

@ohos.busManager.serial (Serial Port Management)

PC/2in126.0.0+

This module provides APIs for serial port management, which are applicable to scenarios where data needs to be exchanged with serial port devices, such as industrial control, sensor data collection, and embedded device communication. This module provides functions such as obtaining the serial port list, opening and closing serial ports, reading and writing data, and managing hardware flow control signals. It helps you easily communicate with external serial port devices, improving device interconnection efficiency.

Since: 26.0.0

Modules to Import

Collapse
Word wrap
Dark theme
Copy code
  1. import { serial } from '@kit.BasicServicesKit';

serial.getSerialPortList

PC/2in126.0.0+

getSerialPortList(): Promise<SerialPort[]>

Obtains the serial port list. This API uses a promise to return the result, which is a list of SerialPort objects. This API uses a promise to return the result. This API is used to identify available serial port devices in scenarios such as industrial device connection, Internet of Things (IoT) device management, and embedded system debugging.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<SerialPort[]> Promise that returns a list of serial ports.

Error codes

For details about the error codes, see Common Error Codes and Serial Port Management Error Codes.

Expand
ID Error Message
203 This function is prohibited by enterprise management policies.
35700001 Service error.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // Obtain the serial port list.
  3. serial.getSerialPortList().then((portList: serial.SerialPort[]) => {
  4. console.info(`getSerialPortList success, length: ${portList.length}`);
  5. if (portList.length > 0) {
  6. let portInfo: serial.SerialPortInfo = portList[0].portInfo;
  7. console.info(`portName: ${portInfo.portName}`);
  8. }
  9. }).catch((error: BusinessError) => {
  10. console.error(`Failed to get serial port list. Code: ${error.code}, message: ${error.message}`);
  11. });

SerialPort

PC/2in126.0.0+

Defines a serial port object, which provides information about the serial port device and the communication capability.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Properties

Expand
Name Type Read-Only Optional Description
portInfo SerialPortInfo Yes No Serial port information.

open

PC/2in126.0.0+

open(config?: SerialConfigs): Promise<void>

Opens a serial port device. This API uses a promise to return the result. This API is used to establish a communication connection with a serial port device, for example, to collect sensor data, send device control commands, or use a serial port printer.

API called in pairs

  • After calling open(), you must call close() to release the serial port resources after use.
  • Without doing so, serial port resources will be leaked, which may affect the use of serial ports by other applications.

Since: 26.0.0

System capability: SystemCapability.BusManager.Serial

Model restriction: This API can be used only in the stage model.

Parameters

Expand
Name Type Mandatory Description
config SerialConfigs No Communication parameters of the serial port. If the config parameter is not passed, the default configuration of SerialConfigs is used to open the serial port.

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700002 Invalid parameter.
35700003 Virtual serial port disconnected.
35700004 Port already in use.
35700007 User authorization required.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // Obtain the serial port list and open the first serial port.
  3. serial.getSerialPortList().then(async (portList: serial.SerialPort[]) => {
  4. if (portList.length === 0) {
  5. console.error('portList is empty');
  6. return;
  7. }
  8. let port: serial.SerialPort = portList[0];
  9. let config: serial.SerialConfigs = {
  10. baudRate: 115200,
  11. dataBits: serial.DataBits.EIGHT,
  12. stopBits: serial.StopBits.ONE,
  13. parity: serial.Parity.NONE
  14. };
  15. await port.open(config);
  16. console.info('open success');
  17. // Call port.close() to release resources after the serial port is used.
  18. await port.close();
  19. }).catch((error: BusinessError) => {
  20. console.error(`Failed to open serial port. Code: ${error.code}, message: ${error.message}`);
  21. });

close

PC/2in126.0.0+

close(): Promise<void>

Closes a serial port device. This API uses a promise to return the result. This method is used to disconnect from a serial port device, for example, when an application exits, a device is switched, or serial port resources are released after a task is complete. This method must be called after the serial port is opened.

API called in pairs

  • You must call open() to open the serial port before calling close() to close the serial port.
  • After close() is called, the serial port resources are released. To use the serial port again, you need to call open() again.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Close the serial port device.
  4. port.close().then(() => {
  5. console.info('close success');
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to close serial port. Code: ${error.code}, message: ${error.message}`);
  8. });

write

PC/2in126.0.0+

write(data: Uint8Array, timeout?: number): Promise<number>

Writes data to a serial port device. The value range of the data length is (0, 4096]. This API uses a promise to return the result. This API is used to send control commands, data packets, and configuration parameters to a connected serial port device, for example, in industrial control, device debugging, and data collection scenarios. This method must be called after the serial port is opened.

Calling sequence

  • You must call open() to open the serial port before calling write() to send data.
  • If write() is called before open(), error code 35700005 (Port not open) will be thrown.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
data Uint8Array Yes Data to be written. Length range: (0, 4096]. If the data to be sent exceeds 4096 bytes, you are advised to call the write method for multiple times.
timeout number No Timeout interval, in milliseconds. The value must be an integer within the range of [0, 300000]. The default value 0 is returned when data cannot be written into the target port. If a negative number, a non-integer, or a number greater than 300000 is passed, error code 35700002 is returned.

Return value

Expand
Type Description
Promise<number> Promise used to return the length of the data written.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700002 Invalid parameter.
35700003 Virtual serial port disconnected.
35700005 Port not open.
35700006 Transmission timeout.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import buffer from @kit.ArkTS.
  2. // Import BusinessError from @kit.BasicServicesKit.
  3. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  4. // Write data to a serial port device.
  5. let writeData: Uint8Array = new Uint8Array(buffer.from('Hello World', 'utf-8').buffer);
  6. port.write(writeData, 2000).then((size: number) => {
  7. console.info('write success, size: ' + size);
  8. }).catch((error: BusinessError) => {
  9. console.error(`Failed to write to serial port. Code: ${error.code}, message: ${error.message}`);
  10. });

onDataRead

PC/2in126.0.0+

onDataRead(callback: Callback<Uint8Array>): void

Listens for data receiving events on the serial port. This API uses an asynchronous callback to return the received data. This API must be called after the serial port is opened. After close is called, all callback registrations will be cleared. This API is used to receive data sent by serial port devices in real time, such as sensor data monitoring, device status feedback, and real-time data collection.

API called in pairs

  • This API is used in pairs with offDataRead(), which is used to unregister the listener.
  • You are advised to call offDataRead() to release resources when the listener is no longer needed.

Calling sequence

  • You must call open() to open the serial port before calling onDataRead() to listen for data.
  • If onDataRead() is called before open(), error code 35700005 (Port not open) will be thrown.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
callback Callback<Uint8Array> Yes Callback used to return the data received by the serial port. This callback is used to listen for data receiving events on the serial port. After the callback is registered, it will be triggered when the serial port receives data.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  2. // Listen for data receiving events on the serial port.
  3. port.onDataRead((data: Uint8Array) => {
  4. console.info(`onDataRead, length: ${data.length}`);
  5. });

offDataRead

PC/2in126.0.0+

offDataRead(callback?: Callback<Uint8Array>): void

Cancels listening for data receiving events on the serial port. This API is used to release resources when listening for data receiving events on the serial port is no longer required, for example, when the application switches to another function or the connection is proactively disconnected.

API called in pairs

  • This API is used in pairs with onDataRead() to unregister the listener registered by onDataRead().
  • You can unregister all listeners or a specified listener.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
callback Callback<Uint8Array> No Callback used to return the result. If a callback is passed, the listener for data receiving events on the specified serial port is unregistered. If no callback is passed, the listeners for data receiving events on all serial ports are unregistered.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  2. // Cancel listening for data receiving events on the serial port.
  3. port.offDataRead();
  4. // Cancel the specified listener callback.
  5. let callback = (data: Uint8Array) => {
  6. console.info(`received data length: ${data.length}`);
  7. };
  8. port.offDataRead(callback);

flush

PC/2in126.0.0+

flush(): Promise<void>

Flushes the serial port buffer, including the read buffer and write buffer. Data in the buffer will be directly discarded and will not be sent or read. This API uses a promise to return the result. This method must be called after the serial port is opened. This method is used to discard invalid or outdated data in the buffer, for example, when the buffer needs to be cleared and data needs to be retransmitted due to a transmission error, or when old data needs to be cleared during a communication protocol switch.

Calling sequence

  • You must call open() to open the serial port before calling flush() to clear the buffer.
  • If flush() is called before open(), error code 35700005 (Port not open) will be thrown.

Difference between flush() and drain(): flush() directly discards all data in the buffer and is suitable for scenarios where the buffer needs to be quickly cleared or invalid data needs to be discarded. drain() waits until the data in the write buffer is completely sent and is suitable for scenarios where complete data transmission is required.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Flush the serial port buffer.
  4. port.flush().then(() => {
  5. console.info('flush success');
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to flush serial port. Code: ${error.code}, message: ${error.message}`);
  8. });

drain

PC/2in126.0.0+

drain(): Promise<void>

Waits until all write requests are complete. This API uses a promise to return the result. This method must be called after the serial port is opened. This method is used to ensure that the follow-up procedure is performed only after all data is written. For example, the serial port is closed after data transmission is complete, or the hardware response is received after data is sent.

Calling sequence

  • You must call open() to open the serial port before calling drain().
  • Call drain() after write() to ensure that all written data is sent.
  • You are advised to call drain() before close() to ensure that all data is transferred before the serial port is closed.
  • If drain() is called before open(), error code 35700005 (Port not open) will be thrown.

The differences between drain() and flush() are as follows:

  • drain() waits until the data in the write buffer is sent completely, which is suitable for scenarios where complete data transmission is required. flush() directly discards all data in the buffer, which is suitable for scenarios where the buffer needs to be quickly cleared or invalid data needs to be discarded.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Wait until all write requests are complete.
  4. port.drain().then(() => {
  5. console.info('drain success');
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to drain serial port. Code: ${error.code}, message: ${error.message}`);
  8. });

setRts

PC/2in126.0.0+

setRts(enable: boolean): Promise<void>

Sets the status of the Request to Send (RTS) signal. This API uses a promise to return the result. This method must be called after the serial port is opened. This method is used to control the request sending signal for hardware-based flow control, such as the transmission permission when hardware-based flow control via RTS/CTS is enabled or communication with devices that support hardware-based flow control.

Calling sequence

  • You must call open() to open the serial port before calling setRts() to set the RTS signal.
  • If setRts() is called before open(), error code 35700005 (Port not open) will be thrown.

Difference between setRts() and setDtr(): setRts() controls the RTS/CTS signal, while setDtr() controls the DTR/DSR signal. RTS/CTS is mainly used for data flow control, and automatic flow control can be enabled through SerialConfigs.rtscts. DTR/DSR is mainly used for device status control and detection, and is used for special protocols or device status management.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
enable boolean Yes RTS signal status. The value true indicates requesting to send data, and the value false indicates otherwise.

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Set the RTS signal.
  4. port.setRts(true).then(() => {
  5. console.info('setRts success');
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to set RTS. Code: ${error.code}, message: ${error.message}`);
  8. });

getCts

PC/2in126.0.0+

getCts(): Promise<boolean>

Obtains the status of the Clear to Send (CTS) signal. This API uses a promise to return the result. This method must be called after the serial port is opened. This method is used to query the CTS signal status for hardware-based flow control to determine whether data can be sent. For example, you can use this method to check the transmission permission when hardware-based flow control via RTS/CTS is enabled or check the status before communicating with a device that supports hardware-based flow control.

Calling sequence

  • You must call open() to open the serial port before calling getCts() to obtain the CTS signal.
  • If getCts() is called before open(), error code 35700005 (Port not open) will be thrown.

The differences between getCts() and getDsr() are as follows:

  • getCts() queries the CTS signal, and the RTS/CTS signal is used to implement hardware-based flow control and determine whether data can be sent. getDsr() queries the DSR signal, and the DTR/DSR signal is used to determine whether the communication device is ready.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<boolean> Promise used to return the CTS signal status. The value true indicates that data can be sent, and the value false indicates otherwise.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Obtain the CTS signal status.
  4. port.getCts().then((cts: boolean) => {
  5. console.info('getCts success, cts: ' + cts);
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to get CTS. Code: ${error.code}, message: ${error.message}`);
  8. });

sendBrk

PC/2in126.0.0+

sendBrk(): Promise<void>

Sends a BRK signal. This API uses a promise to return the result. This method must be called after the serial port is opened. This method is used to send an interrupt signal to a device, for example, to stop device communication immediately, notify the device to reset, or perform signal interaction required by a special protocol.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Send a BRK signal.
  4. port.sendBrk().then(() => {
  5. console.info('sendBrk success');
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to send BRK. Code: ${error.code}, message: ${error.message}`);
  8. });

setDtr

PC/2in126.0.0+

setDtr(enable: boolean): Promise<void>

Sets the status of the data terminal ready (DTR) signal. This API uses a promise to return the result. This method must be called after the serial port is opened. This method is used to control the DTR signal. For example, it can be used to notify a device that the terminal is ready, control device power-on or reset through the DTR signal, or communicate with a device that requires DTR signal detection.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
enable boolean Yes DTR signal status. The value true indicates that the data terminal is ready, and the value false indicates that the data terminal is not ready.

Return value

Expand
Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Set the DTR signal.
  4. port.setDtr(true).then(() => {
  5. console.info('setDtr success');
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to set DTR. Code: ${error.code}, message: ${error.message}`);
  8. });

getDsr

PC/2in126.0.0+

getDsr(): Promise<boolean>

Obtains the status of the data set ready (DSR) signal. This API uses a promise to return the result. This method must be called after the serial port is opened. This method queries the status of the DSR signal to determine whether the communication device is ready, for example, checking the device connection status or starting communication after the device is ready.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Return value

Expand
Type Description
Promise<boolean> Promise used to return the DSR signal status. The value true indicates that the data device is ready, and the value false indicates that the data device is not ready.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700003 Virtual serial port disconnected.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // Import BusinessError from @kit.BasicServicesKit.
  2. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  3. // Obtain the DSR signal status.
  4. port.getDsr().then((dsr: boolean) => {
  5. console.info('getDsr success, dsr: ' + dsr);
  6. }).catch((error: BusinessError) => {
  7. console.error(`Failed to get DSR. Code: ${error.code}, message: ${error.message}`);
  8. });

onDisconnect

PC/2in126.0.0+

onDisconnect(callback: Callback<void>): void

Subscribes to serial port disconnection events. This API uses an asynchronous callback to return the result. After close() is called, all callbacks will be unregistered. This method subscribes to serial port disconnection events, such as removal of a USB virtual serial port, device power-off, or connection interruption. This allows you to handle exceptions in a timely manner, notify users, or attempt to reconnect.

API called in pairs

  • This API is used in pairs with offDisconnect(), which is used to unregister the listener.
  • You are advised to call offDisconnect() to release resources when the listener is no longer needed.

Calling sequence

  • You must call open() to open the serial port before calling onDisconnect() to listen for the disconnect event.
  • If onDisconnect() is called before open(), error code 35700005 (Port not open) will be thrown.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
callback Callback<void> Yes Callback used to return the result, which is triggered when the serial port is disconnected. This callback is used to listen for disconnection events on the serial port. After the callback is registered, it will be triggered when the serial port is disconnected.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  2. // Subscribe to serial port disconnection events.
  3. port.onDisconnect(() => {
  4. console.info('serial port disconnected');
  5. });

offDisconnect

PC/2in126.0.0+

offDisconnect(callback?: Callback<void>): void

Unsubscribes from serial port disconnection events. This method must be called after the serial port is opened. This API is used to release resources when listening for serial port disconnection events is no longer required, for example, when the application switches to another function or the connection is proactively disconnected.

Calling sequence

  • You must call open() to open the serial port before calling offDisconnect() to cancel listening.
  • If offDisconnect() is called before open(), error code 35700005 (Port not open) will be thrown.

API called in pairs

  • This API is used in pairs with onDisconnect() to unregister the listener registered by onDisconnect().
  • You can unregister all listeners or a specified listener.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Parameters

Expand
Name Type Mandatory Description
callback Callback<void> No Callback used to return the result, which can be unregistered only after being registered using onDisconnect(). If a callback is passed, the listener for disconnection events on the specified serial port is unregistered. If no callback is passed, the listeners for disconnection events on all serial ports are unregistered.

Error codes

For details about the error codes, see Serial Port Management Error Codes.

Expand
ID Error Message
35700001 Service error.
35700005 Port not open.

Example

Collapse
Word wrap
Dark theme
Copy code
  1. // port is a serial port object, which needs to be obtained through serial.getSerialPortList().
  2. // Unsubscribe from serial port disconnection events.
  3. port.offDisconnect();
  4. // Cancel the specified listener callback.
  5. let disconnectedCallback = () => {
  6. console.info('serial port disconnected');
  7. };
  8. port.offDisconnect(disconnectedCallback);

SerialPortInfo

PC/2in126.0.0+

Describes the serial port information.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Expand
Name Type Read-Only Optional Description
portName string No No Port name.
vendorId number No Yes Vendor ID of the USB virtual serial port.
productId number No Yes Product ID of the USB virtual serial port.
manufacturer string No Yes Manufacturer name of the USB virtual serial port.

DataBits

PC/2in126.0.0+

Enumerates the number of data bits.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Expand
Name Value Description
FIVE 5 Five data bits.
SIX 6 Six data bits.
SEVEN 7 Seven data bits.
EIGHT 8 Eight data bits.

StopBits

PC/2in126.0.0+

Enumerates the number of stop bits.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Expand
Name Value Description
ONE 1 One stop bit.
TWO 2 Two stop bits.

Parity

PC/2in126.0.0+

Enumerates the number of parity bits.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Expand
Name Value Description
NONE 'none' No parity.
EVEN 'even' Even parity.
ODD 'odd' Odd parity.
MARK 'mark' Mark parity. The parity bit is always 1.
SPACE 'space' Space parity. The parity bit is always 0.

SerialConfigs

PC/2in126.0.0+

Defines the communication parameters of the serial port.

Since: 26.0.0

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.BusManager.Serial

Expand
Name Type Read-Only Optional Description
baudRate number No Yes Baud rate. The value must be a positive integer. Whether non-standard baud rates are supported depends on the hardware. Unit: bit/s. The default value is 115200.
dataBits DataBits No Yes Data bits. The default value is EIGHT, indicating 8 data bits for standard communication. Values FIVE, SIX, and SEVEN are used for old devices or special protocols.
stopBits StopBits No Yes Stop bits. The default value is ONE. One stop bit is used for standard communication. Two stop bits are used to enhance signal stability during low-speed communication or communication with old devices.
parity Parity No Yes Parity bit. The default value is NONE, indicating no parity check. EVEN and ODD are used in scenarios that require high data accuracy. MARK and SPACE are used for special communication protocols.
rtscts boolean No Yes Whether to enable hardware-based automatic flow control via RTS/CTS. Hardware-based flow control via RTS/CTS is an automatic data flow control mechanism implemented through hardware signals. The RTS and CTS signal lines work together to prevent buffer overflow. If this flow control is enabled, the system automatically controls RTS and CTS signals to manage mobile data. The value true indicates this feature is enabled, and false indicates otherwise. The default value is false.
xon boolean No Yes Whether to enable XON (Xmitter On) to control the sending of flows. XON indicates transmitter on. XON is a control character (with the ASCII value of 17) in the software flow control protocol. When there is space in the receive buffer, XON is sent to instruct the sender to resume data transmission. The value true indicates this feature is enabled, and false indicates otherwise. The default value is false.
xoff boolean No Yes Whether to enable XOFF (Xmitter Off) to control the sending of flows. XOFF indicates transmitter off. XOFF is a control character (with the ASCII value of 19) in the software flow control protocol. When the receive buffer is about to overflow, XOFF is sent to instruct the sender to stop sending data. The value true indicates this feature is enabled, and false indicates otherwise. The default value is false.
xany boolean No Yes Whether to enable XANY (Any Character Resume) to control the flow. XANY is an extended mode in the software flow control protocol and takes effect only when XON or XOFF is enabled. When XANY is enabled, any character can be used as the signal to resume transmission, not just the XON character. If software flow control (XON/XOFF) is not enabled, the XANY setting is invalid. The value true indicates this feature is enabled, and false indicates otherwise. The default value is false.
Search in References
Enter a keyword.