目录

USB串口配置管理

简介

USB串口配置管理中,波特率、数据位、校验位和停止位是串口通信协议的核心参数,它们共同定义了数据传输的格式和规则。通过合理配置这些参数,可以显著提升串口通信的可靠性和效率。

[h2]基本概念

在进行USB串口开发时,开发者应了解以下基本概念:

环境准备

请参考USB串口通信服务开发概述环境准备。

开发指导

[h2]接口说明

接口名 描述
getAttribute(portId: number): Readonly<SerialAttribute> 获取串口设备配置。
setAttribute(portId: number, attribute: SerialAttribute): void 设置串口设备配置。

[h2]开发步骤

开发者可以通过上述接口获取和设置串口的配置:

以下示例代码只是获取和设置串口的配置的必要流程,需要放入具体的方法中执行。

  1. 导入模块。

    // 导入serialManager模块
    import { serialManager, BusinessError } from '@kit.BasicServicesKit';
    import { buffer, JSON } from '@kit.ArkTS';
  2. 获取设备列表。

    // 获取连接主设备的USB设备列表
    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. 获取设备操作权限。

    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 : code=${error.code}, message=${error.message}`);
        this.logInfo_ += '\n[ERROR] usb device request right failed: code=' + error.code + ', message=' + error.message;
      });
    } else {
      console.info('serial device already request right');
      this.logInfo_ += '\n[INFO] serial device already request right';
    }
    this.portId_ = portId;
  4. 根据串口打开设备。

    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) {
      let errorObj: BusinessError = error as BusinessError;
      console.error(`open usbSerial error: code=${errorObj.code}, message=${errorObj.message}`);
      this.logInfo_ += '\n[ERROR] open usbSerial error: code=' + errorObj.code + ', message=' + errorObj.message;
    }
  5. 获取和修改串口配置。

    let portId: number = this.portId_;
    // 获取串口配置
    try {
      let attribute: serialManager.SerialAttribute = serialManager.getAttribute(portId);
      console.info(`getAttribute usbSerial success, attribute: ${JSON.stringify(attribute)}`);
      this.logInfo_ += '\n[INFO] getAttribute usbSerial success, attribute: ' + JSON.stringify(attribute);
    } catch (error) {
      let errorObj: BusinessError = error as BusinessError;
      console.error(`getAttribute usbSerial error: code=${errorObj.code}, message=${errorObj.message}`);
      this.logInfo_ += '\n[ERROR] getAttribute usbSerial error: code=' + errorObj.code + ', message=' + errorObj.message;
    }
    let portId: number = this.portId_;
    // 设置串口配置
    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: ${JSON.stringify(attribute)}`);
      this.logInfo_ += '\n[INFO] setAttribute usbSerial success, attribute: ' + JSON.stringify(attribute);
    } catch (error) {
      let errorObj: BusinessError = error as BusinessError;
      console.error(`setAttribute usbSerial error: code=${errorObj.code}, message=${errorObj.message}`);
      this.logInfo_ += '\n[ERROR] setAttribute usbSerial error: code=' + errorObj.code + ', message=' + errorObj.message;
    }

[h2]调测验证

  1. 准备一根USB转串口线缆,线缆的USB接口连接到HarmonyOS设备USB端口(该端口需支持USB转串口),线缆的串口接口连接到目标设备的串口上。
  2. 在HarmonyOS设备上执行上述示例。
  3. 返回getAttribute usbSerial success和setAttribute usbSerial success表示相关接口调用成功,可以查看当前串口的配置。