Options
All
  • Public
  • Public/Protected
  • All
Menu

SerialPort 串口协议 API 包括以下几个部分:

下面是完整的代码使用案例:

import { CommonDevices } from "@ubtech/ucode-extension-common-sdk";

const { SerialPortHardwareDevice, getSerialPortDeviceRegister } = CommonDevices.SerialPort;

class DemoSerialPortHardwareDevice extends SerialPortHardwareDevice { // 继承 SerialPortHardwareDevice
say(data) {
this.send(Buffer.from(data));
}
}

export const spRegister = getSerialPortDeviceRegister({
DeviceConnection: DemoSerialPortHardwareDevice,
// 以下配置均为可选配置
Options: {
openOptions: {
baudRate: 115200, // 串口打开的波特率
bufferSize: 12 * 1024 * 1024, // 缓冲区大小
},
queueOptions: {
enable: true, // 数据发送是否启用队列
interval: 70, // 启用队列时数据发送的间隔
},
// 发现设备时过滤用的vid和pid,配置后将只显示和配置id一致的串口设备
filter: {
vid: "0403",
pid: "6001",
},
// 自定义显示串口设备名
customDeviceName: (data) => `myRobot_${data?.comName}`,
},
});

除了这些以外,还导出了一些 TypeScript 的类型定义,你都可以从下面找到对应的入口

Index

Type aliases

QueueConstructorType

QueueConstructorType: { enable: boolean; interval?: number; length?: number }

Type declaration

  • enable: boolean
  • Optional interval?: number
  • Optional length?: number

SerialPortConstructorOptionType

SerialPortConstructorOptionType: { openOptions?: OpenOptions; queueOptions?: QueueConstructorType }

串口构建函数参数类型

Type declaration

  • Optional openOptions?: OpenOptions

    串口打开参数,可以设置 波特率 等

    const openOptions = {
    baudRate: 115200, // 串口打开的波特率
    bufferSize: 12 * 1024 * 1024, // 缓冲区大小
    };
  • Optional queueOptions?: QueueConstructorType

    串口 队列参数,可以设置 队列 发送的 间隔 或者 数量

SerialPortCustomDeviceName

SerialPortCustomDeviceName: (data?: { comName: string; pid?: string; serial?: string; vid?: string }) => string

Type declaration

    • (data?: { comName: string; pid?: string; serial?: string; vid?: string }): string
    • 自定义串口设备名字

      可以通过传递一个函数,data 里面包含这些参数:

      • comName: 串口号
      • pid: Product ID
      • vid: Vendor ID
      • serial: 串口序列号

      下面是案例:

      const customDeviceName = (data) => `myRobot_${data?.comName}`;
      

      Parameters

      • Optional data: { comName: string; pid?: string; serial?: string; vid?: string }
        • comName: string
        • Optional pid?: string
        • Optional serial?: string
        • Optional vid?: string

      Returns string

SerialPortFilterOptions

SerialPortFilterOptions: { pid: string | string[]; vid: string | string[] }

串口过滤器类型

例如:

const filer = {
vid: "0403",
pid: "6001",
};

Type declaration

  • pid: string | string[]

    Product ID 产品 ID 十六进制显示

  • vid: string | string[]

    Vendor ID 厂商 ID 十六进制显示

SerialPortRegisterOptions

SerialPortRegisterOptions: { customDeviceName?: SerialPortCustomDeviceName; filter?: SerialPortFilterOptions; openOptions?: OpenOptions; queueOptions?: QueueConstructorType; scanTime?: number }

串口注册参数类型

Type declaration

  • Optional customDeviceName?: SerialPortCustomDeviceName

    自定义串口设备名:可以通过一些数据,自己生成新的设备名

  • Optional filter?: SerialPortFilterOptions

    串口过滤器,这个参数是实例化 SerialPortDiscover 的时候作为第三个参数传递的

  • Optional openOptions?: OpenOptions

    串口打开参数,可以设置 波特率 等

  • Optional queueOptions?: QueueConstructorType

    串口 队列参数,可以设置 队列 发送的 间隔 或者 数量

  • Optional scanTime?: number

    扫描时间

Variables

SerialPortDeviceType

SerialPortDeviceType: DiscoverDeviceType = ...

串口协议 设备类型 常量 默认是输出:

{
id: DeviceTypeId.serialport,
connectType: 'discover',
name: 'USB连接'
}
  • id: 如果要使用一些标准的 uCode 串口协议权限 id 必须和 DeviceTypeID.serialport 保持一致
  • connectType: 串口的话必须指定为 discover 类型,否则无法正常搜索
  • name: 显示名称可以自定义

Functions

getSerialPortDeviceRegister

Generated using TypeDoc