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

@csllc/mb-conn-node-serial

v2.0.0

Published

Connection type for MODBUS using NodeJS Serialport package

Downloads

96

Readme

@csllc/mb-conn-node-serial

A Connection subclass for MODBUS over a Node.js serial port, backed by @serialport/stream, plus a scan() utility to enumerate available serial ports.

Installation

npm install @csllc/mb-conn-node-serial

Overview

This package provides:

  • SerialConnection — Extends Connection from @csllc/modbus-types. It bridges Node.js Buffer-based serialport data events to the ArrayBuffer-based Connection API expected by RtuTransport.
  • scan() — Async function that lists all available serial ports on the current machine.

To run MODBUS RTU over a serial port, pair SerialConnection with RtuTransport and createMaster from @csllc/modbus-types.

Usage

List available serial ports

import { scan } from '@csllc/mb-conn-node-serial';

const ports = await scan();
// ports is an array of SerialConnectionInfo:
// [{ id: '/dev/ttyUSB0', type: 'serial', path: '/dev/ttyUSB0' }, ...]
console.log(ports);

Open a port and create a MODBUS RTU master

import { SerialConnection, scan } from '@csllc/mb-conn-node-serial';
import { createMaster } from '@csllc/modbus-types';

const ports = await scan();
if (ports.length === 0) throw new Error('No serial ports found');

const connection = new SerialConnection(
  ports[0],          // SerialConnectionInfo — must have .path
  {
    baudRate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
  }
);

const master = createMaster({
  transport: { type: 'rtu', connection },
  defaultUnit: 1,
  defaultTimeout: 500,
  defaultMaxRetries: 2,
});

await connection.open();

master.on('connected', async () => {
  const response = await master.requestAsync(
    new (await import('@csllc/modbus-types')).Functions.ReadHoldingRegistersRequest({
      address: 0,
      quantity: 4,
    })
  );
  console.log('registers:', response);
  await connection.close();
});

Using a custom serialport binding (e.g. for mocking in tests)

import { SerialConnection } from '@csllc/mb-conn-node-serial';
import { MockBinding } from '@serialport/binding-mock';

MockBinding.createPort('/dev/ttyUSB0', { echo: false });

const connection = new SerialConnection(
  { id: '/dev/ttyUSB0', type: 'serial', path: '/dev/ttyUSB0' },
  { baudRate: 9600, binding: MockBinding }
);

API Reference

scan(): Promise<SerialConnectionInfo[]>

Enumerates all serial ports currently available on the system.

Returns: A promise that resolves to an array of SerialConnectionInfo objects.

Each entry has:

| Property | Type | Description | |---|---|---| | id | string | Same as path; unique identifier for this port. | | type | string | Always 'serial'. | | path | string | The OS device path (e.g. /dev/ttyUSB0 or COM3). |


class SerialConnection extends Connection

A Connection implementation backed by @serialport/stream. Translates between the Node.js Buffer API used by serialport and the ArrayBuffer API used by @csllc/modbus-types.

Constructor

new SerialConnection(port: SerialConnectionInfo, options: SerialConnectionOptions)

SerialConnectionInfo

| Property | Type | Description | |---|---|---| | id | string | Unique identifier for this connection. | | type | string | Connection type string. | | path | string | Serial port device path. |

SerialConnectionOptions

| Property | Type | Required | Description | |---|---|---|---| | baudRate | number | yes | Baud rate (e.g. 9600, 115200). | | dataBits | 5 \| 6 \| 7 \| 8 | no | Data bits per character. Defaults to 8. | | parity | 'none' \| 'even' \| 'odd' | no | Parity mode. Defaults to 'none'. | | stopBits | 1 \| 2 | no | Stop bits. Defaults to 1. | | binding | BindingInterface | no | Custom serialport binding (useful for testing). Defaults to the platform auto-detected binding. |

Methods

| Method | Returns | Description | |---|---|---| | open() | Promise<void> | Opens the serial port. Emits open on success. | | close() | Promise<void> | Closes the serial port. Emits close. | | write(data) | Promise<boolean> | Writes an ArrayBuffer to the port. Returns true. | | isOpen() | boolean | Returns true if the port is currently open. | | drain() | Promise<void> | Waits for all queued write data to be transmitted. | | destroy() | Promise<void> | Closes the port if open and cleans up. |

Events

Inherited from Connection (EventEmitter):

| Event | Args | Description | |---|---|---| | open | — | Port has opened successfully. | | close | — | Port has closed. | | data | data: ArrayBuffer | Bytes received from the serial port. | | error | err: Error | A serialport error occurred. |


interface SerialConnectionOptions

interface SerialConnectionOptions {
  baudRate: number;
  dataBits?: 5 | 6 | 7 | 8;
  parity?: 'none' | 'even' | 'odd';
  stopBits?: 1 | 2;
  binding?: BindingInterface;
}