Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions ModbusRTU.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { Socket, SocketConstructorOpts, TcpSocketConnectOpts } from 'net';
import { TestPort } from "./TestPort";
import { PortInfo } from '@serialport/bindings-cpp';

/**
* Subset of `serialport` `SerialPort.list()` entries — defined here so typings do not require
* `@serialport/bindings-cpp` at install time when using `npm install --no-optional`. Values from
* `getPorts()` are compatible with `PortInfo` when `serialport` is installed.
*/
export interface ModbusSerialPortListEntry {
path: string;
manufacturer?: string;
serialNumber?: string;
pnpId?: string;
locationId?: string;
productId?: string;
vendorId?: string;
}

export class ModbusRTU {
constructor(port?: any);
static TestPort: typeof TestPort

static getPorts(): Promise<PortInfo[]>
static getPorts(): Promise<ModbusSerialPortListEntry[]>

open(callback?: Function): void;
close(callback?: Function): void;
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ try these options on npm install to build, if you have problems to install

--unsafe-perm --build-from-source

##### Optional `serialport` dependency

[`serialport`](https://www.npmjs.com/package/serialport) is an **optional** dependency: npm tries to install it, but if the install fails (or you skip optionals), `modbus-serial` still installs and TCP/UDP usage works.

**Skip installing optional dependencies** (no native serial build, smaller tree):

npm install modbus-serial --no-optional

Or in `.npmrc`: `optional=false`

If you skipped or dropped `serialport` and later need serial RTU/ASCII, install it explicitly:

npm install serialport

Without `serialport`, serial-specific APIs fail at **runtime** when used, for example:

- `ModbusRTU.getPorts()`, `connectRTU`, `connectRTUBuffered`, `connectAsciiSerial`
- `ModbusRTU.RTUBufferedPort`, `ModbusRTU.ServerSerial` (not exported if `serialport` cannot be loaded)

TypeScript: `ServerSerial.d.ts` imports `serialport` types. If you use `--no-optional` and have no `serialport` in the tree, use **`skipLibCheck`** or add **`serialport` as a devDependency** for type-checking.

#### Development

This repo uses **npm** and a committed **`package-lock.json`**. After cloning:
Expand Down
4 changes: 4 additions & 0 deletions ServerSerial.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* References `serialport` / `@serialport/stream`. `serialport` is optional; add it (or `skipLibCheck`)
* if this file must type-check when optional deps were skipped.
*/
import * as events from 'events';
import * as stream from 'stream';
import { SerialPortOpenOptions } from 'serialport';
Expand Down
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {ModbusRTU} from "./ModbusRTU";
/**
* `serialport` is an optional dependency. Some declarations import `serialport` modules; if it is
* not installed (`npm install --no-optional`), TypeScript may not resolve those modules — use
* `skipLibCheck` or add `serialport` as a devDependency. Runtime serial APIs are optional (README).
*/
import { ModbusRTU } from "./ModbusRTU";
export * from "./ServerTCP";
export * from "./ServerSerial";

Expand Down
39 changes: 33 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
{
"name": "modbus-serial",
"version": "8.0.23",
"description": "A pure JavaScript implementation of MODBUS-RTU (Serial and TCP) for NodeJS.",
"description": "A pure JavaScript implementation of MODBUS-RTU (Serial and TCP) for NodeJS. The serialport package is an optional dependency (use npm install --no-optional to skip).",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js",
"apis",
"ports",
"servers",
"utils",
"index.d.ts",
"ModbusRTU.d.ts",
"ServerTCP.d.ts",
"ServerSerial.d.ts",
"TestPort.d.ts",
"README.md",
"LICENSE"
],
"scripts": {
"test": "nyc --reporter=lcov --reporter=text mocha --recursive",
"lint": "eslint .",
Expand Down Expand Up @@ -48,7 +63,9 @@
"webbluetooth": "^3.6.0"
},
"dependencies": {
"debug": "^4.3.1",
"debug": "^4.3.1"
},
"optionalDependencies": {
"serialport": "^12.0.0"
}
}
Loading