modbus-rs 0.14.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +51 -33
  2. package/index.d.ts +743 -129
  3. package/index.js +88 -52
  4. package/package.json +8 -8
package/README.md CHANGED
@@ -5,10 +5,13 @@ High-performance Modbus TCP/RTU/ASCII client, server, and gateway for Node.js, p
5
5
  ## Features
6
6
 
7
7
  - **Async/Promise-based API** - All operations return Promises
8
- - **TCP Client** - Full Modbus TCP/IP client implementation
8
+ - **TCP Client** - Full Modbus TCP/IP client implementation (supports communicating with multiple unit IDs behind a single IP address and a serial port)
9
9
  - **Serial Client** - Modbus RTU and ASCII over serial port
10
- - **TCP Server** - Build Modbus TCP servers with JavaScript handlers
11
- - **TCP Gateway** - Route requests to multiple downstream servers based on unit ID
10
+ - **TCP & Serial Servers** - Build Modbus TCP servers, or Serial RTU and ASCII servers, using custom JavaScript handlers to respond to incoming requests
11
+ - **Modbus Gateway** - Deploy high-performance gateways supporting WebSockets, TCP, and Serial (RTU/ASCII) as upstream channels, and TCP/Serial (RTU/ASCII) as downstream channels (WebSocket downstream support planned for a future release), dynamically routing requests based on unit ID mapping tables
12
+ - **Thread Safety & Concurrency** - Rust-backed concurrent architecture ensures safe access across multiple async execution contexts
13
+ - **Safety Locks** - Integrated bus locking to prevent command collisions and state corruption
14
+ - **Multi-drop Serial Support** - Manage and communicate with multiple device unit IDs on a single physical RTU/ASCII bus
12
15
  - **High Performance** - Native Rust core with napi-rs bindings
13
16
  - **Type Safe** - Full TypeScript definitions included
14
17
  - **Cross Platform** - Pre-built binaries for Linux, macOS, and Windows
@@ -33,7 +36,7 @@ async function main() {
33
36
  const transport = await AsyncTcpTransport.connect({
34
37
  host: '127.0.0.1',
35
38
  port: 502,
36
- timeoutMs: 5000,
39
+ requestTimeoutMs: 5000,
37
40
  });
38
41
 
39
42
  const client = transport.createClient({ unitId: 1 });
@@ -96,8 +99,8 @@ const { AsyncTcpModbusServer } = require('modbus-rs');
96
99
 
97
100
  const holdingRegisters = new Array(1000).fill(0);
98
101
 
99
- function main() {
100
- const server = AsyncTcpModbusServer.bind(
102
+ async function main() {
103
+ const server = await AsyncTcpModbusServer.bind(
101
104
  { host: '0.0.0.0', port: 502, unitId: 1 },
102
105
  {
103
106
  onReadHoldingRegisters: (req) => {
@@ -117,11 +120,7 @@ function main() {
117
120
  });
118
121
  }
119
122
 
120
- try {
121
- main();
122
- } catch (err) {
123
- console.error(err);
124
- }
123
+ main().catch(console.error);
125
124
  ```
126
125
 
127
126
  ### TCP Gateway
@@ -155,6 +154,37 @@ async function main() {
155
154
  main().catch(console.error);
156
155
  ```
157
156
 
157
+ ## Migration Guide
158
+
159
+ Detailed step-by-step migration guides are available in the [Migration Guides](https://github.com/Raghava-Ch/modbus-rs/tree/main/documentation/migrations) directory.
160
+
161
+
162
+ ## Error Handling with Code Constants
163
+
164
+ Error code constants are now exported:
165
+
166
+ ```js
167
+ const { getModbusErrorCode, ModbusErrorCode } = require('modbus-rs');
168
+
169
+ try {
170
+ await client.readHoldingRegisters({ address: 0, quantity: 10 });
171
+ } catch (err) {
172
+ const code = getModbusErrorCode(err);
173
+ switch (code) {
174
+ case ModbusErrorCode.EXCEPTION: console.error('Modbus exception'); break;
175
+ case ModbusErrorCode.TIMEOUT: console.error('Request timed out'); break;
176
+ case ModbusErrorCode.CONNECTION_CLOSED: console.error('Disconnected'); break;
177
+ default: console.error('Unknown error:', err.message);
178
+ }
179
+ }
180
+ ```
181
+
182
+ ## Known Limitations
183
+
184
+ - **AbortSignal**: Uses `signal.onabort` instead of `signal.addEventListener`. Only one abort handler per signal object is supported.
185
+ - **Gateway route limit**: `AsyncTcpGateway` supports a maximum of **64 routing entries**. Attempting to add more will throw at `bind()` time.
186
+ - **Gateway Downstream**: WebSockets are currently not supported as a downstream channel (support is planned for a future release).
187
+
158
188
  ## API Reference
159
189
 
160
190
  ### AsyncTcpTransport
@@ -162,7 +192,7 @@ main().catch(console.error);
162
192
  - `static connect(opts: TcpTransportOptions): Promise<AsyncTcpTransport>` - Connect to a Modbus TCP server
163
193
  - `close(): Promise<void>` - Close the connection
164
194
  - `reconnect(): Promise<void>` - Re-establish the connection
165
- - `createClient(opts?: CreateClientOptions): AsyncTcpModbusClient` - Create a logical client instance bound to a specific unit ID (defaults to `1`)
195
+ - `createClient(opts: CreateClientOptions): AsyncTcpModbusClient` - Create a logical client instance bound to a specific unit ID (required)
166
196
  - `setRequestTimeout(ms: number): void` - Set a global request timeout (in milliseconds)
167
197
  - `clearRequestTimeout(): void` - Clear the global request timeout
168
198
  - `pendingRequests: boolean` - (Getter) Returns whether there are requests currently in flight
@@ -199,31 +229,19 @@ These logical clients contain all the Modbus function code methods:
199
229
 
200
230
  ### AsyncTcpModbusServer
201
231
 
202
- - `bind(opts, handlers)` - Create and start a TCP server
203
- - `shutdown()` - Stop the server
204
-
205
- ### AsyncTcpGateway
232
+ - `static bind(opts, handlers): Promise<AsyncTcpModbusServer>` - Create and start a TCP server
233
+ - `shutdown(): Promise<void>` - Stop the server
206
234
 
207
- - `bind(opts, config)` - Create and start a gateway
208
- - `shutdown()` - Stop the gateway
235
+ ### AsyncSerialModbusServer
209
236
 
210
- ## Error Handling
237
+ - `static bindRtu(opts, handlers): Promise<AsyncSerialModbusServer>` - Create and start a Serial RTU server
238
+ - `static bindAscii(opts, handlers): Promise<AsyncSerialModbusServer>` - Create and start a Serial ASCII server
239
+ - `shutdown(): Promise<void>` - Stop the server
211
240
 
212
- All errors are thrown as JavaScript Error objects with descriptive messages:
241
+ ### AsyncTcpGateway
213
242
 
214
- ```javascript
215
- try {
216
- await client.readHoldingRegisters({ address: 0, quantity: 10 });
217
- } catch (err) {
218
- if (err.message.includes('MODBUS_EXCEPTION')) {
219
- console.error('Modbus exception:', err.message);
220
- } else if (err.message.includes('MODBUS_TIMEOUT')) {
221
- console.error('Request timed out');
222
- } else {
223
- console.error('Error:', err.message);
224
- }
225
- }
226
- ```
243
+ - `static bind(opts, config): Promise<AsyncTcpGateway>` - Create and start a gateway
244
+ - `shutdown(): Promise<void>` - Stop the gateway
227
245
 
228
246
  ## Supported platforms
229
247