npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

serial-ports

v1.2.6

Published

Capacitor SerialPort Plugin (Android Only)

Readme

Capacitor SerialPort Plugin

这是一个用于Capacitor的串口通信插件,目前仅支持Android平台。该插件提供了串口通信、GPIO控制、系统控制等功能。

功能特性

串口通信

  • 支持同时控制多个串口
  • 打开/关闭串口
  • 读取串口数据
  • 写入十六进制数据到串口
  • 控制USB充放电
  • 获取USB充电状态

GPIO控制

  • 设置GPIO引脚模式
  • 读取GPIO引脚状态
  • 写入GPIO引脚状态

系统控制

  • 关机
  • 重启
  • 恢复出厂设置
  • OTA更新
  • 设置定时开关机
  • 设置系统时间

系统信息

  • 获取设备型号
  • 获取设备序列号

显示控制

  • LCD/HDMI开关控制
  • 状态栏显示控制
  • 手势状态栏控制

安装

npm install serial-ports

使用方法

初始化

import { SerialPort } from 'serial-ports';

// 初始化插件
const serialPort = new SerialPort();

串口操作

// 打开第一个串口
await serialPort.open({
  portId: 'port1',  // 串口标识符
  device: '/dev/ttyS1',
  baudRate: 9600
});

// 打开第二个串口
await serialPort.open({
  portId: 'port2',  // 串口标识符
  device: '/dev/ttyS2',
  baudRate: 9600
});

// 向第一个串口写入数据
await serialPort.write({
  portId: 'port1',
  data: 'AA55'
});

// 从第二个串口读取数据
const result = await serialPort.read({
  portId: 'port2'
});
console.log(result.data);

// 关闭指定串口
await serialPort.close({
  portId: 'port1'
});

// 关闭所有串口
await serialPort.close();

// 控制USB充放电
await serialPort.controlCharging({
  portId: 'port1',
  usb1Charging: true,  // USB1充电
  usb2Charging: false  // USB2不充电
});

// 获取USB充电状态
const chargingStatus = await serialPort.getChargingStatus({
  portId: 'port1'
});
console.log('USB1充满: ' + chargingStatus.usb1Full);
console.log('USB2充满: ' + chargingStatus.usb2Full);

GPIO操作

// 设置GPIO模式
await serialPort.setMulSelGpio({
  group: 'O',
  num: 1,
  value: 1
});

// 读取GPIO状态
const result = await serialPort.readGpio({
  group: 'O',
  num: 1
});
console.log(result.value);

// 写入GPIO状态
await serialPort.writeGpio({
  group: 'O',
  num: 1,
  value: 1
});

系统控制

// 关机
await serialPort.shutDown();

// 重启
await serialPort.reboot();

// 恢复出厂设置
await serialPort.factoryReset();

// OTA更新
await serialPort.updateOta();

// 设置定时开关机
await serialPort.setPowerOnOffTime({
  enable: true,
  onTime: [2024, 3, 15, 8, 0],  // [年, 月, 日, 时, 分]
  offTime: [2024, 3, 15, 18, 0]
});

// 设置系统时间
await serialPort.setSystemTime({
  time: [2024, 3, 15, 8, 0, 0]  // [年, 月, 日, 时, 分, 秒]
});

系统信息

// 获取设备型号
const model = await serialPort.getBuildModel();
console.log(model.model);

// 获取设备序列号
const serial = await serialPort.getBuildSerial();
console.log(serial.serial);

显示控制

// 控制LCD/HDMI开关
await serialPort.setLcdOnOff({
  enable: true,
  lcdOrHdmi: 0  // 0: LCD, 1: HDMI
});

// 控制状态栏显示
await serialPort.setStatusBar({
  enable: true
});

// 控制手势状态栏
await serialPort.setGestureStatusBar({
  enable: true
});

错误处理

插件使用Promise处理异步操作,所有方法都可能抛出以下错误:

  • SERIAL_NOT_INITIALIZED: 串口未初始化
  • INVALID_PARAMETER: 参数无效
  • OPERATION_FAILED: 操作失败

注意事项

  1. 本插件仅支持Android平台
  2. 使用串口功能需要相应的系统权限
  3. 部分功能可能需要root权限
  4. GPIO操作仅支持'O'组引脚

许可证

MIT License