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

cy-plugin-modbus

v1.1.4

Published

modbus 插件

Readme

cy-plugin-modbus

封装浏览器端 modbus 模块

npm

📦 安装

npm install cy-plugin-modbus

🦄 使用

import { CySerialPort, Queue } from 'cy-plugin-modbus';
import type {
  IReadDateFormat,
  IWriteDateFormat,
  ICySerialPortSendDataFormat
} from 'cy-plugin-modbus';

//初始化初始化串口对象
const port = new CySerialPort({
  // 开启调试
  debug: true,
  // 连接
  onConnect: () => {},
  // 失败
  onFail: () => {}
});

//初始化队列
const queue = new Queue({
  debug: true,
  onSuccess: (result, extraData) => {
    console.log('每次任务成功都调用', result, extraData);
  },
  onError: (error, extraData) => {
    console.log('每次任务失败都调用', error, extraData);
  }
});

// 连接(完整参数示例)
await port.connect({
  baudRate: 9600, // 波特率
  parity: 'none', // 校验位: 'none' | 'even' | 'odd'
  dataBits: 8, // 数据位: 7 或 8
  stopBits: 1, // 停止位: 1 或 2
  bufferSize: 255, // 读写缓冲区大小 (必须小于 16MB)
  flowControl: 'none' // 流量控制: 'none' | 'hardware' | 'software'
});

// 断开连接
await port.disconnect();

// 直接发送消息示例
const readResult = await port.send({
  slave: 1, // 设备地址
  function_code: EnumModbusOptionType.ReadHoldingRegisters, // 操作类型
  starting_address: 0, // 起始地址
  quantity_of_x: 10, // 寄存器个数
  output_value: [] // 写入数据
});

//队列发送消息
queue.push({
  taskParams: data, // data 为 modbus 请求参数格式
  task: async (param) => {
    return await serialPort.send(param);
    // try {
    //   return await port.send(param);
    // } catch (error) {
    //这里拦截 send 异常 抛出的错误
    //  console.log('error', error);
    //  return Promise.reject(error);
    //}
  },
  retryCount: 1, // 重试次数 retryCount - 1
  extraData: { msg: '额外参数' },
  onSuccess: (res, extra) => {
    console.log('✅ taskOnSuccess', JSON.stringify({ res, extra }, null, 2));
  },
  onError: (error, extra) => {
    console.log('❌ taskOnError', JSON.stringify({ error, extra }, null, 2));
  }
});

// =========================================
// 读取示例(参数格式 IReadDateFormat)
// 生成对应操作的数据结构(生成后使用 send 方法发送数据)
// =========================================

// 读线圈
serialPort.readCoilsFormat({});

// 读离散输入
serialPort.readDiscreteInputsFormat({});

// 读保持寄存器
serialPort.readHoldingRegistersFormat({});

// 读输入寄存器
serialPort.readInputRegistersFormat({});

// =======================================
// 写入示例(参数格式 IWriteDateFormat
// 生成对应操作的数据结构(生成后使用 send 方法发送数据)
// =======================================

// 写单个线圈
serialPort.writeSingleCoilFormat({});

// 写单个保持寄存器
serialPort.writeSingleRegisterFormat({});

// 写多个线圈
serialPort.writeMultipleCoilsFormat({});

// 写多个保持寄存器
serialPort.writeMultipleRegistersFormat({});