目录

USB Serial Configuration Management

Overview

In the USB serial configuration management, the baud rate, data bit, parity bit, and stop bit are the core parameters of the serial port communication protocol. They define the format and rules of data transmission. By properly setting these parameters, you can significantly improve the reliability and efficiency of serial port communication.

[h2]Basic Concepts

Before developing the USB serial port, you should have a basic understanding of the following concepts:

Preparing the Environment

For details, see Preparing the Environment in USB Serial Communication Development Overview.

How to Develop

[h2]Available APIs

API Description
getAttribute(portId: number): Readonly<SerialAttribute> Obtains the serial port device configuration.
setAttribute(portId: number, attribute: SerialAttribute): void Sets the serial port device configuration.

[h2]Development Procedure

You can obtain and set the serial port configuration as follows:

The following sample code shows only a basic process. You should execute the code in a specific method.

  1. Import the usbManager module.

    // Import the usbManager module.
    import { serialManager } from '@kit.BasicServicesKit';
    import { BusinessError } from '@kit.BasicServicesKit'
    import { buffer } from '@kit.ArkTS';
    import { JSON } from '@kit.ArkTS';
  2. Obtain the USB device list.

    // Obtain the list of USB devices connected to the host.
    let portList: serialManager.SerialPort[] = serialManager.getPortList();
    console.info(`usbSerial portList: ${portList}`);
    this.logInfo_ += '\n[INFO] usbSerial portList: ' + JSON.stringify(portList);
    if (portList === undefined || portList.length === 0) {
      console.error('usbSerial portList is empty');
      this.logInfo_ += '\n[ERROR] usbSerial portList is empty';
      return;
    }
    this.portList_ = portList;
  3. Obtain the device operation permissions.

    if (this.portList_ === undefined || this.portList_.length === 0) {
      console.error('usbSerial portList is empty');
      this.logInfo_ += '\n[ERROR] usbSerial portList is empty';
      return;
    }
    let portList: serialManager.SerialPort[] = this.portList_;
    let portId: number = portList[0].portId;
    if (!serialManager.hasSerialRight(portId)) {
      serialManager.requestSerialRight(portId).then((result: boolean) => {
        console.info('serial device request right result: ' + result);
        this.logInfo_ += '\n[INFO] serial device request right result: ' + JSON.stringify(result);
      }).catch((error: BusinessError) => {
        console.error(`usb device request right failed : ${error}`);
        this.logInfo_ += '\n[ERROR] usb device request right failed: ' + JSON.stringify(error);
      });
    } else {
      console.info('serial device already request right');
      this.logInfo_ += '\n[INFO] serial device already request right';
    }
    this.portId_ = portId;
  4. Open the device based on the serial port.

    let portId: number = this.portId_;
    try {
      serialManager.open(portId)
      console.info(`open usbSerial success, portId: ${portId}`);
      this.logInfo_ += '\n[INFO] open usbSerial success, portId: ' + JSON.stringify(portId);
    } catch (error) {
      console.error(`open usbSerial error: ${error}`);
      this.logInfo_ += '\n[ERROR] open usbSerial error: ' + JSON.stringify(error);
    }
  5. Obtain and modify serial port configurations.

    let portId: number = this.portId_;
    // Obtain the serial port configuration.
    try {
      let attribute: serialManager.SerialAttribute = serialManager.getAttribute(portId);
      if (attribute === undefined) {
        console.error('getAttribute usbSerial error, attribute is undefined');
        this.logInfo_ += '\n[ERROR] getAttribute usbSerial error, attribute is undefined';
      } else {
        console.info(`getAttribute usbSerial success, attribute: ${attribute}`);
        this.logInfo_ += '\n[INFO] getAttribute usbSerial success, attribute: ' + JSON.stringify(attribute);
      }
    } catch (error) {
      console.error(`getAttribute usbSerial error: ${error}`);
      this.logInfo_ += '\n[ERROR] getAttribute usbSerial error: ' + JSON.stringify(error);
    }
    let portId: number = this.portId_;
    // Set the serial port configuration.
    try {
      let attribute: serialManager.SerialAttribute = {
        baudRate: serialManager.BaudRates.BAUDRATE_9600,
        dataBits: serialManager.DataBits.DATABIT_8,
        parity: serialManager.Parity.PARITY_NONE,
        stopBits: serialManager.StopBits.STOPBIT_1
      }
      serialManager.setAttribute(portId, attribute);
      console.info(`setAttribute usbSerial success, attribute: ${attribute}`);
      this.logInfo_ += '\n[INFO] setAttribute usbSerial success, attribute: ' + JSON.stringify(attribute);
    } catch (error) {
      console.error(`setAttribute usbSerial error: ${error}`);
      this.logInfo_ += '\n[ERROR] setAttribute usbSerial error: ' + JSON.stringify(error);
    }

[h2]Debugging and Verification

  1. Prepare a USB-to-serial cable. Connect the USB port and the serial port of the cable to that of the HarmonyOS device.
  2. Execute the preceding sample code on the HarmonyOS device.
  3. Return getAttribute usbSerial success and setAttribute usbSerial success if related APIs are successfully called. You can view the current serial port configuration.