modbus-rs 0.13.0 → 0.15.0

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 +86 -47
  2. package/index.d.ts +202 -66
  3. package/index.js +78 -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
@@ -21,19 +24,23 @@ npm install modbus-rs
21
24
 
22
25
  ## Quick Start
23
26
 
27
+ ### Examples
28
+ [https://github.com/Raghava-Ch/modbus-rs/tree/main/mbus-ffi/nodejs/examples](https://github.com/Raghava-Ch/modbus-rs/tree/main/mbus-ffi/nodejs/examples)
29
+
24
30
  ### TCP Client
25
31
 
26
32
  ```javascript
27
- const { AsyncTcpModbusClient } = require('modbus-rs');
33
+ const { AsyncTcpTransport } = require('modbus-rs');
28
34
 
29
35
  async function main() {
30
- const client = await AsyncTcpModbusClient.connect({
36
+ const transport = await AsyncTcpTransport.connect({
31
37
  host: '127.0.0.1',
32
38
  port: 502,
33
- unitId: 1,
34
- timeoutMs: 5000,
39
+ requestTimeoutMs: 5000,
35
40
  });
36
41
 
42
+ const client = transport.createClient({ unitId: 1 });
43
+
37
44
  try {
38
45
  // Read holding registers (FC03)
39
46
  const registers = await client.readHoldingRegisters({
@@ -48,7 +55,7 @@ async function main() {
48
55
  value: 12345,
49
56
  });
50
57
  } finally {
51
- await client.close();
58
+ await transport.close();
52
59
  }
53
60
  }
54
61
 
@@ -58,18 +65,19 @@ main().catch(console.error);
58
65
  ### Serial RTU Client
59
66
 
60
67
  ```javascript
61
- const { AsyncSerialModbusClient } = require('modbus-rs');
68
+ const { AsyncRtuTransport } = require('modbus-rs');
62
69
 
63
70
  async function main() {
64
- const client = await AsyncSerialModbusClient.connectRtu({
71
+ const transport = await AsyncRtuTransport.open({
65
72
  portPath: '/dev/ttyUSB0',
66
- unitId: 1,
67
73
  baudRate: 19200,
68
74
  dataBits: 8,
69
75
  stopBits: 1,
70
76
  parity: 'even',
71
77
  });
72
78
 
79
+ const client = transport.createClient({ unitId: 1 });
80
+
73
81
  try {
74
82
  const registers = await client.readHoldingRegisters({
75
83
  address: 0,
@@ -77,7 +85,7 @@ async function main() {
77
85
  });
78
86
  console.log('Registers:', registers);
79
87
  } finally {
80
- await client.close();
88
+ await transport.close();
81
89
  }
82
90
  }
83
91
 
@@ -93,14 +101,13 @@ const holdingRegisters = new Array(1000).fill(0);
93
101
 
94
102
  async function main() {
95
103
  const server = await AsyncTcpModbusServer.bind(
96
- { host: '0.0.0.0', port: 502 },
104
+ { host: '0.0.0.0', port: 502, unitId: 1 },
97
105
  {
98
106
  onReadHoldingRegisters: (req) => {
99
- return holdingRegisters.slice(req.address, req.address + req.count);
107
+ return holdingRegisters.slice(req.address, req.address + req.quantity);
100
108
  },
101
109
  onWriteSingleRegister: (req) => {
102
110
  holdingRegisters[req.address] = req.value;
103
- return true;
104
111
  },
105
112
  }
106
113
  );
@@ -147,12 +154,63 @@ async function main() {
147
154
  main().catch(console.error);
148
155
  ```
149
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
+
150
188
  ## API Reference
151
189
 
152
- ### AsyncTcpModbusClient
190
+ ### AsyncTcpTransport
191
+
192
+ - `static connect(opts: TcpTransportOptions): Promise<AsyncTcpTransport>` - Connect to a Modbus TCP server
193
+ - `close(): Promise<void>` - Close the connection
194
+ - `reconnect(): Promise<void>` - Re-establish the connection
195
+ - `createClient(opts: CreateClientOptions): AsyncTcpModbusClient` - Create a logical client instance bound to a specific unit ID (required)
196
+ - `setRequestTimeout(ms: number): void` - Set a global request timeout (in milliseconds)
197
+ - `clearRequestTimeout(): void` - Clear the global request timeout
198
+ - `pendingRequests: boolean` - (Getter) Returns whether there are requests currently in flight
199
+
200
+ ### AsyncRtuTransport / AsyncAsciiTransport
201
+
202
+ - `static open(opts: RtuTransportOptions | AsciiTransportOptions): Promise<AsyncRtuTransport | AsyncAsciiTransport>` - Open the serial port
203
+ - `close(): Promise<void>` - Close the connection
204
+ - `reconnect(): Promise<void>` - Re-establish the connection
205
+ - `createClient(opts: CreateClientOptions): AsyncSerialModbusClient` - Create a logical client instance bound to a specific unit ID (required)
206
+ - `setRequestTimeout(ms: number): void` - Set a global request timeout (in milliseconds)
207
+ - `clearRequestTimeout(): void` - Clear the global request timeout
208
+ - `pendingRequests: boolean` - (Getter) Returns whether there are requests currently in flight
209
+
210
+ ### AsyncTcpModbusClient / AsyncSerialModbusClient
211
+
212
+ These logical clients contain all the Modbus function code methods:
153
213
 
154
- - `connect(opts: TcpClientOptions)` - Connect to a Modbus TCP server
155
- - `close()` - Close the connection
156
214
  - `readCoils(opts)` - FC01: Read Coils
157
215
  - `readDiscreteInputs(opts)` - FC02: Read Discrete Inputs
158
216
  - `readHoldingRegisters(opts)` - FC03: Read Holding Registers
@@ -169,40 +227,21 @@ main().catch(console.error);
169
227
  - `diagnostics(opts)` - FC08: Diagnostics
170
228
  - `readDeviceIdentification(opts)` - FC43/14: Read Device Identification
171
229
 
172
- ### AsyncSerialModbusClient
173
-
174
- Same methods as `AsyncTcpModbusClient`, with different connection options:
175
-
176
- - `connectRtu(opts: SerialClientOptions)` - Connect using Modbus RTU
177
- - `connectAscii(opts: SerialClientOptions)` - Connect using Modbus ASCII
178
-
179
230
  ### AsyncTcpModbusServer
180
231
 
181
- - `bind(opts, handlers)` - Create and start a TCP server
182
- - `shutdown()` - Stop the server
232
+ - `static bind(opts, handlers): Promise<AsyncTcpModbusServer>` - Create and start a TCP server
233
+ - `shutdown(): Promise<void>` - Stop the server
183
234
 
184
- ### AsyncTcpGateway
235
+ ### AsyncSerialModbusServer
185
236
 
186
- - `bind(opts, config)` - Create and start a gateway
187
- - `shutdown()` - Stop the gateway
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
188
240
 
189
- ## Error Handling
190
-
191
- All errors are thrown as JavaScript Error objects with descriptive messages:
241
+ ### AsyncTcpGateway
192
242
 
193
- ```javascript
194
- try {
195
- await client.readHoldingRegisters({ address: 0, count: 10 });
196
- } catch (err) {
197
- if (err.message.includes('MODBUS_EXCEPTION')) {
198
- console.error('Modbus exception:', err.message);
199
- } else if (err.message.includes('MODBUS_TIMEOUT')) {
200
- console.error('Request timed out');
201
- } else {
202
- console.error('Error:', err.message);
203
- }
204
- }
205
- ```
243
+ - `static bind(opts, config): Promise<AsyncTcpGateway>` - Create and start a gateway
244
+ - `shutdown(): Promise<void>` - Stop the gateway
206
245
 
207
246
  ## Supported platforms
208
247
 
@@ -211,7 +250,7 @@ Pre-built binaries are published for:
211
250
  - Linux x64 (glibc), Linux arm64 (glibc)
212
251
  - macOS x64, macOS arm64
213
252
  - Windows x64 (MSVC)
214
- - WebAssembly `modbus-rs-wasm`
253
+ - WebAssembly [modbus-rs-wasm](https://www.npmjs.com/package/modbus-rs-wasm)
215
254
 
216
255
  Other targets can be built locally via `cargo build -p mbus-ffi --features nodejs,full`
217
256
  followed by `npm run build`.
package/index.d.ts CHANGED
@@ -1,21 +1,43 @@
1
1
  /* auto-generated by NAPI-RS */
2
2
  /* eslint-disable */
3
- /** Async Modbus Serial client supporting RTU and ASCII transports. */
4
- export declare class AsyncSerialModbusClient {
5
- /** Creates and connects a new Serial RTU client. */
6
- static connectRtu(opts: SerialClientOptions): Promise<AsyncSerialModbusClient>
7
- /** Creates and connects a new Serial ASCII client. */
8
- static connectAscii(opts: SerialClientOptions): Promise<AsyncSerialModbusClient>
3
+ /** Physical serial port in ASCII mode. Factory for device clients. */
4
+ export declare class AsyncAsciiTransport {
5
+ /** Opens the serial port in ASCII mode. */
6
+ static open(opts: AsciiTransportOptions): Promise<AsyncAsciiTransport>
7
+ /** Creates a device client bound to the specified unit ID. */
8
+ createClient(opts: CreateClientOptions): AsyncSerialModbusClient
9
+ /** Sets the per-request timeout in milliseconds. */
10
+ setRequestTimeout(timeoutMs: number): void
11
+ /** Clears the per-request timeout. */
12
+ clearRequestTimeout(): void
13
+ /** Returns whether there are pending requests. */
14
+ get pendingRequests(): boolean
15
+ /** Closes the transport. */
16
+ close(): Promise<void>
17
+ /** Reconnects the transport after a disconnect. */
18
+ reconnect(): Promise<void>
19
+ }
20
+
21
+ /** Physical serial port in RTU mode. Factory for device clients. */
22
+ export declare class AsyncRtuTransport {
23
+ /** Opens the serial port in RTU mode. */
24
+ static open(opts: RtuTransportOptions): Promise<AsyncRtuTransport>
25
+ /** Creates a device client bound to the specified unit ID. */
26
+ createClient(opts: CreateClientOptions): AsyncSerialModbusClient
9
27
  /** Sets the per-request timeout in milliseconds. */
10
28
  setRequestTimeout(timeoutMs: number): void
11
29
  /** Clears the per-request timeout. */
12
30
  clearRequestTimeout(): void
13
31
  /** Returns whether there are pending requests. */
14
32
  get pendingRequests(): boolean
15
- /** Closes the client connection. */
33
+ /** Closes the transport. */
16
34
  close(): Promise<void>
17
- /** Reconnects the client after a disconnect. */
35
+ /** Reconnects the transport after a disconnect. */
18
36
  reconnect(): Promise<void>
37
+ }
38
+
39
+ /** Lightweight device client sharing the serial transport. */
40
+ export declare class AsyncSerialModbusClient {
19
41
  /** Reads holding registers (FC03). */
20
42
  readHoldingRegisters(opts: ReadRegistersOptions): Promise<number[]>
21
43
  /** Reads input registers (FC04). */
@@ -50,10 +72,8 @@ export declare class AsyncSerialModbusClient {
50
72
 
51
73
  /** Async Modbus Serial server supporting RTU and ASCII transports. */
52
74
  export declare class AsyncSerialModbusServer {
53
- /** Binds and starts a new Serial RTU server. */
54
- static bindRtu(opts: SerialServerOptions, handlers: object): AsyncSerialModbusServer
55
- /** Binds and starts a new Serial ASCII server. */
56
- static bindAscii(opts: SerialServerOptions, handlers: object): AsyncSerialModbusServer
75
+ static bindRtu(opts: SerialServerOptions, handlers: ServerHandlers): Promise<AsyncSerialModbusServer>
76
+ static bindAscii(opts: SerialServerOptions, handlers: ServerHandlers): Promise<AsyncSerialModbusServer>
57
77
  /** Stops the server. */
58
78
  shutdown(): Promise<void>
59
79
  }
@@ -76,30 +96,8 @@ export declare class AsyncTcpGateway {
76
96
  shutdown(): Promise<void>
77
97
  }
78
98
 
79
- /**
80
- * Async Modbus TCP client.
81
- *
82
- * All methods are async and return Promises. The client must be connected
83
- * before issuing Modbus requests.
84
- */
99
+ /** Lightweight device client sharing the TCP transport. */
85
100
  export declare class AsyncTcpModbusClient {
86
- /**
87
- * Creates and connects a new TCP client.
88
- *
89
- * @param opts - Connection options including host, port, unitId, and optional timeout.
90
- * @returns A connected client instance.
91
- */
92
- static connect(opts: TcpClientOptions): Promise<AsyncTcpModbusClient>
93
- /** Sets the per-request timeout in milliseconds. */
94
- setRequestTimeout(timeoutMs: number): void
95
- /** Clears the per-request timeout. */
96
- clearRequestTimeout(): void
97
- /** Returns whether there are pending requests. */
98
- get pendingRequests(): boolean
99
- /** Closes the client connection. */
100
- close(): Promise<void>
101
- /** Reconnects the client after a disconnect. */
102
- reconnect(): Promise<void>
103
101
  /** Reads holding registers (FC03). */
104
102
  readHoldingRegisters(opts: ReadRegistersOptions): Promise<number[]>
105
103
  /** Reads input registers (FC04). */
@@ -145,11 +143,57 @@ export declare class AsyncTcpModbusServer {
145
143
  * @param handlers - Object containing handler functions for each Modbus operation.
146
144
  * @returns A running server instance.
147
145
  */
148
- static bind(opts: TcpServerOptions, handlers: object): AsyncTcpModbusServer
146
+ static bind(opts: TcpServerOptions, handlers: ServerHandlers): Promise<AsyncTcpModbusServer>
149
147
  /** Stops the server. */
150
148
  shutdown(): Promise<void>
151
149
  }
152
150
 
151
+ /** Physical TCP socket connection to a Modbus device or gateway. */
152
+ export declare class AsyncTcpTransport {
153
+ /** Connects to a Modbus TCP device or gateway. */
154
+ static connect(opts: TcpTransportOptions): Promise<AsyncTcpTransport>
155
+ /** Creates a device client bound to the specified unit ID. */
156
+ createClient(opts: CreateClientOptions): AsyncTcpModbusClient
157
+ /** Sets the per-request timeout in milliseconds. */
158
+ setRequestTimeout(timeoutMs: number): void
159
+ /** Clears the per-request timeout. */
160
+ clearRequestTimeout(): void
161
+ /** Returns whether there are pending requests. */
162
+ get pendingRequests(): boolean
163
+ /** Closes the connection. */
164
+ close(): Promise<void>
165
+ /** Reconnects the transport after a disconnect. */
166
+ reconnect(): Promise<void>
167
+ }
168
+
169
+ /** Connection options for the serial ASCII transport. */
170
+ export interface AsciiTransportOptions {
171
+ /** Serial port path (e.g., "/dev/ttyUSB0", "COM3"). */
172
+ portPath: string
173
+ /** Baud rate (e.g., 9600, 19200, 38400, 57600, 115200). */
174
+ baudRate: number
175
+ /** Data bits (5, 6, 7, or 8). */
176
+ dataBits?: number
177
+ /** Parity ("none", "even", "odd"). */
178
+ parity?: string
179
+ /** Stop bits (1 or 2). */
180
+ stopBits?: number
181
+ /** Response timeout in milliseconds. */
182
+ responseTimeoutMs?: number
183
+ /** Per-request timeout in milliseconds. */
184
+ requestTimeoutMs?: number
185
+ /** Number of retry attempts on failure (0 = none). Default: 0. */
186
+ retryAttempts?: number
187
+ /** Backoff strategy: "immediate", "fixed", or "exponential". Default: "immediate". */
188
+ retryBackoffStrategy?: string
189
+ }
190
+
191
+ /** Options for creating a device client. */
192
+ export interface CreateClientOptions {
193
+ /** Modbus unit ID (1-247). */
194
+ unitId: number
195
+ }
196
+
153
197
  /** Device identification object. */
154
198
  export interface DeviceIdentificationObject {
155
199
  /** Object ID. */
@@ -177,7 +221,7 @@ export interface DiagnosticsOptions {
177
221
  /** Data words for the request. */
178
222
  data: Array<number>
179
223
  /** Optional abort signal to cancel the request. */
180
- signal?: object
224
+ signal?: AbortSignal
181
225
  }
182
226
 
183
227
  /** Handler request for diagnostics. */
@@ -205,8 +249,6 @@ export interface DownstreamConfig {
205
249
 
206
250
  /** Response from reading FIFO queue. */
207
251
  export interface FifoQueueResponse {
208
- /** Number of values in the queue. */
209
- count: number
210
252
  /** Queue values. */
211
253
  values: Array<number>
212
254
  }
@@ -221,6 +263,13 @@ export interface FileRecordReadRequest {
221
263
  recordLength: number
222
264
  }
223
265
 
266
+ /** A single file record read sub-request on the server side. */
267
+ export interface FileRecordReadServerSubRequest {
268
+ fileNumber: number
269
+ recordNumber: number
270
+ recordLength: number
271
+ }
272
+
224
273
  /** A single file record write sub-request. */
225
274
  export interface FileRecordWriteRequest {
226
275
  /** File number (1-65535). */
@@ -231,6 +280,13 @@ export interface FileRecordWriteRequest {
231
280
  recordData: Array<number>
232
281
  }
233
282
 
283
+ /** A single file record write sub-request on the server side. */
284
+ export interface FileRecordWriteSubRequest {
285
+ fileNumber: number
286
+ recordNumber: number
287
+ recordData: Array<number>
288
+ }
289
+
234
290
  /** Gateway bind options. */
235
291
  export interface GatewayBindOptions {
236
292
  /** Bind host address (e.g., "0.0.0.0"). */
@@ -247,6 +303,24 @@ export interface GatewayConfig {
247
303
  routes: Array<RouteEntry>
248
304
  }
249
305
 
306
+ /** Stable error code: The connection was closed. */
307
+ export const MODBUS_ERROR_CODE_CONNECTION_CLOSED: string
308
+
309
+ /** Stable error code: Modbus protocol exception received. */
310
+ export const MODBUS_ERROR_CODE_EXCEPTION: string
311
+
312
+ /** Stable error code: Internal library error. */
313
+ export const MODBUS_ERROR_CODE_INTERNAL: string
314
+
315
+ /** Stable error code: Invalid argument passed to the API. */
316
+ export const MODBUS_ERROR_CODE_INVALID_ARGUMENT: string
317
+
318
+ /** Stable error code: Request timed out. */
319
+ export const MODBUS_ERROR_CODE_TIMEOUT: string
320
+
321
+ /** Stable error code: Transport/framing error. */
322
+ export const MODBUS_ERROR_CODE_TRANSPORT: string
323
+
250
324
  /** Options for reading coils or discrete inputs. */
251
325
  export interface ReadBitsOptions {
252
326
  /** Starting address. */
@@ -254,7 +328,7 @@ export interface ReadBitsOptions {
254
328
  /** Number of bits to read. */
255
329
  quantity: number
256
330
  /** Optional abort signal to cancel the request. */
257
- signal?: object
331
+ signal?: AbortSignal
258
332
  }
259
333
 
260
334
  /** Handler request for reading coils. */
@@ -271,7 +345,7 @@ export interface ReadDeviceIdentificationOptions {
271
345
  /** Starting object ID. */
272
346
  objectId: number
273
347
  /** Optional abort signal to cancel the request. */
274
- signal?: object
348
+ signal?: AbortSignal
275
349
  }
276
350
 
277
351
  /** Handler request for reading discrete inputs. */
@@ -281,12 +355,17 @@ export interface ReadDiscreteInputsRequest {
281
355
  quantity: number
282
356
  }
283
357
 
358
+ /** Handler request for reading exception status. */
359
+ export interface ReadExceptionStatusRequest {
360
+ unitId: number
361
+ }
362
+
284
363
  /** Options for reading FIFO queue. */
285
364
  export interface ReadFifoQueueOptions {
286
365
  /** FIFO pointer address. */
287
366
  address: number
288
367
  /** Optional abort signal to cancel the request. */
289
- signal?: object
368
+ signal?: AbortSignal
290
369
  }
291
370
 
292
371
  /** Handler request for reading FIFO queue. */
@@ -300,7 +379,13 @@ export interface ReadFileRecordOptions {
300
379
  /** Array of sub-requests. */
301
380
  requests: Array<FileRecordReadRequest>
302
381
  /** Optional abort signal to cancel the request. */
303
- signal?: object
382
+ signal?: AbortSignal
383
+ }
384
+
385
+ /** Handler request for reading file records. */
386
+ export interface ReadFileRecordRequest {
387
+ unitId: number
388
+ requests: Array<FileRecordReadServerSubRequest>
304
389
  }
305
390
 
306
391
  /** Handler request for reading holding registers. */
@@ -324,7 +409,7 @@ export interface ReadRegistersOptions {
324
409
  /** Number of registers to read. */
325
410
  quantity: number
326
411
  /** Optional abort signal to cancel the request. */
327
- signal?: object
412
+ signal?: AbortSignal
328
413
  }
329
414
 
330
415
  /** Options for read/write multiple registers (FC23). */
@@ -338,7 +423,16 @@ export interface ReadWriteMultipleRegistersOptions {
338
423
  /** Values to write. */
339
424
  writeValues: Array<number>
340
425
  /** Optional abort signal to cancel the request. */
341
- signal?: object
426
+ signal?: AbortSignal
427
+ }
428
+
429
+ /** Handler request for read/write multiple registers (FC23). */
430
+ export interface ReadWriteMultipleRegistersRequest {
431
+ unitId: number
432
+ readAddress: number
433
+ readQuantity: number
434
+ writeAddress: number
435
+ writeValues: Array<number>
342
436
  }
343
437
 
344
438
  /** Route entry mapping unit ID to a downstream channel. */
@@ -349,8 +443,8 @@ export interface RouteEntry {
349
443
  channel: number
350
444
  }
351
445
 
352
- /** Connection options for the serial client. */
353
- export interface SerialClientOptions {
446
+ /** Connection options for the serial RTU transport. */
447
+ export interface RtuTransportOptions {
354
448
  /** Serial port path (e.g., "/dev/ttyUSB0", "COM3"). */
355
449
  portPath: string
356
450
  /** Baud rate (e.g., 9600, 19200, 38400, 57600, 115200). */
@@ -361,12 +455,14 @@ export interface SerialClientOptions {
361
455
  parity?: string
362
456
  /** Stop bits (1 or 2). */
363
457
  stopBits?: number
364
- /** Modbus unit ID (1-247). */
365
- unitId: number
366
458
  /** Response timeout in milliseconds. */
367
459
  responseTimeoutMs?: number
368
460
  /** Per-request timeout in milliseconds. */
369
461
  requestTimeoutMs?: number
462
+ /** Number of retry attempts on failure (0 = none). Default: 0. */
463
+ retryAttempts?: number
464
+ /** Backoff strategy: "immediate", "fixed", or "exponential". Default: "immediate". */
465
+ retryBackoffStrategy?: string
370
466
  }
371
467
 
372
468
  /** Server bind options for serial port. */
@@ -399,18 +495,6 @@ export interface ServerExceptionResponse {
399
495
  exception?: number
400
496
  }
401
497
 
402
- /** Connection options for the TCP client. */
403
- export interface TcpClientOptions {
404
- /** Target host address (IP or hostname). */
405
- host: string
406
- /** Target TCP port (typically 502). */
407
- port: number
408
- /** Modbus unit ID (1-247). */
409
- unitId: number
410
- /** Per-request timeout in milliseconds (optional). */
411
- timeoutMs?: number
412
- }
413
-
414
498
  /** Server bind options. */
415
499
  export interface TcpServerOptions {
416
500
  /** Bind host address (e.g., "0.0.0.0"). */
@@ -421,12 +505,32 @@ export interface TcpServerOptions {
421
505
  unitId: number
422
506
  }
423
507
 
508
+ /** Connection options for the TCP transport. */
509
+ export interface TcpTransportOptions {
510
+ /** Target host address (IP or hostname). */
511
+ host: string
512
+ /** Target TCP port (typically 502). */
513
+ port: number
514
+ /** Per-request timeout in milliseconds (optional). */
515
+ requestTimeoutMs?: number
516
+ /** Number of retry attempts on failure (0 = none). Default: 0. */
517
+ retryAttempts?: number
518
+ /** Backoff strategy: "immediate", "fixed", or "exponential". Default: "immediate". */
519
+ retryBackoffStrategy?: string
520
+ }
521
+
424
522
  /** Options for writing file records. */
425
523
  export interface WriteFileRecordOptions {
426
524
  /** Array of sub-requests. */
427
525
  requests: Array<FileRecordWriteRequest>
428
526
  /** Optional abort signal to cancel the request. */
429
- signal?: object
527
+ signal?: AbortSignal
528
+ }
529
+
530
+ /** Handler request for writing file records. */
531
+ export interface WriteFileRecordRequest {
532
+ unitId: number
533
+ requests: Array<FileRecordWriteSubRequest>
430
534
  }
431
535
 
432
536
  /** Options for writing multiple coils. */
@@ -436,7 +540,7 @@ export interface WriteMultipleCoilsOptions {
436
540
  /** Values to write. */
437
541
  values: Array<boolean>
438
542
  /** Optional abort signal to cancel the request. */
439
- signal?: object
543
+ signal?: AbortSignal
440
544
  }
441
545
 
442
546
  /** Handler request for writing multiple coils. */
@@ -453,7 +557,7 @@ export interface WriteMultipleRegistersOptions {
453
557
  /** Values to write. */
454
558
  values: Array<number>
455
559
  /** Optional abort signal to cancel the request. */
456
- signal?: object
560
+ signal?: AbortSignal
457
561
  }
458
562
 
459
563
  /** Handler request for writing multiple registers. */
@@ -470,7 +574,7 @@ export interface WriteSingleCoilOptions {
470
574
  /** Value to write. */
471
575
  value: boolean
472
576
  /** Optional abort signal to cancel the request. */
473
- signal?: object
577
+ signal?: AbortSignal
474
578
  }
475
579
 
476
580
  /** Handler request for writing a single coil. */
@@ -487,7 +591,7 @@ export interface WriteSingleRegisterOptions {
487
591
  /** Value to write. */
488
592
  value: number
489
593
  /** Optional abort signal to cancel the request. */
490
- signal?: object
594
+ signal?: AbortSignal
491
595
  }
492
596
 
493
597
  /** Handler request for writing a single register. */
@@ -496,3 +600,35 @@ export interface WriteSingleRegisterRequest {
496
600
  address: number
497
601
  value: number
498
602
  }
603
+
604
+ export interface ModbusException {
605
+ exception: number;
606
+ }
607
+
608
+ export interface ServerHandlers {
609
+ onReadCoils?: (req: ReadCoilsRequest) => boolean[] | ModbusException | Promise<boolean[] | ModbusException>;
610
+ onReadDiscreteInputs?: (req: ReadDiscreteInputsRequest) => boolean[] | ModbusException | Promise<boolean[] | ModbusException>;
611
+ onReadHoldingRegisters?: (req: ReadHoldingRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
612
+ onReadInputRegisters?: (req: ReadInputRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
613
+ onWriteSingleCoil?: (req: WriteSingleCoilRequest) => void | ModbusException | Promise<void | ModbusException>;
614
+ onWriteSingleRegister?: (req: WriteSingleRegisterRequest) => void | ModbusException | Promise<void | ModbusException>;
615
+ onReadExceptionStatus?: (req: ReadExceptionStatusRequest) => number | ModbusException | Promise<number | ModbusException>;
616
+ onDiagnostics?: (req: DiagnosticsRequest) => ServerDiagnosticsResponse | ModbusException | Promise<ServerDiagnosticsResponse | ModbusException>;
617
+ onWriteMultipleCoils?: (req: WriteMultipleCoilsRequest) => void | ModbusException | Promise<void | ModbusException>;
618
+ onWriteMultipleRegisters?: (req: WriteMultipleRegistersRequest) => void | ModbusException | Promise<void | ModbusException>;
619
+ onReadFileRecord?: (req: ReadFileRecordRequest) => number[][] | ModbusException | Promise<number[][] | ModbusException>;
620
+ onWriteFileRecord?: (req: WriteFileRecordRequest) => void | ModbusException | Promise<void | ModbusException>;
621
+ onReadWriteMultipleRegisters?: (req: ReadWriteMultipleRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
622
+ onReadFifoQueue?: (req: ReadFifoQueueRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
623
+ }
624
+
625
+ export declare const ModbusErrorCode: {
626
+ readonly EXCEPTION: 'MODBUS_EXCEPTION';
627
+ readonly TIMEOUT: 'MODBUS_TIMEOUT';
628
+ readonly TRANSPORT: 'MODBUS_TRANSPORT';
629
+ readonly INVALID_ARGUMENT: 'MODBUS_INVALID_ARGUMENT';
630
+ readonly CONNECTION_CLOSED: 'MODBUS_CONNECTION_CLOSED';
631
+ readonly INTERNAL: 'MODBUS_INTERNAL';
632
+ };
633
+
634
+ export declare function getModbusErrorCode(err: Error): string | undefined;
package/index.js CHANGED
@@ -77,8 +77,8 @@ function requireNative() {
77
77
  try {
78
78
  const binding = require('modbus-rs-android-arm64')
79
79
  const bindingPackageVersion = require('modbus-rs-android-arm64/package.json').version
80
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
82
  }
83
83
  return binding
84
84
  } catch (e) {
@@ -93,8 +93,8 @@ function requireNative() {
93
93
  try {
94
94
  const binding = require('modbus-rs-android-arm-eabi')
95
95
  const bindingPackageVersion = require('modbus-rs-android-arm-eabi/package.json').version
96
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
98
  }
99
99
  return binding
100
100
  } catch (e) {
@@ -114,8 +114,8 @@ function requireNative() {
114
114
  try {
115
115
  const binding = require('modbus-rs-win32-x64-gnu')
116
116
  const bindingPackageVersion = require('modbus-rs-win32-x64-gnu/package.json').version
117
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
119
  }
120
120
  return binding
121
121
  } catch (e) {
@@ -130,8 +130,8 @@ function requireNative() {
130
130
  try {
131
131
  const binding = require('modbus-rs-win32-x64-msvc')
132
132
  const bindingPackageVersion = require('modbus-rs-win32-x64-msvc/package.json').version
133
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
133
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
135
  }
136
136
  return binding
137
137
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('modbus-rs-win32-ia32-msvc')
149
149
  const bindingPackageVersion = require('modbus-rs-win32-ia32-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -163,8 +163,8 @@ function requireNative() {
163
163
  try {
164
164
  const binding = require('modbus-rs-win32-arm64-msvc')
165
165
  const bindingPackageVersion = require('modbus-rs-win32-arm64-msvc/package.json').version
166
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
166
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
168
  }
169
169
  return binding
170
170
  } catch (e) {
@@ -182,8 +182,8 @@ function requireNative() {
182
182
  try {
183
183
  const binding = require('modbus-rs-darwin-universal')
184
184
  const bindingPackageVersion = require('modbus-rs-darwin-universal/package.json').version
185
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
187
  }
188
188
  return binding
189
189
  } catch (e) {
@@ -198,8 +198,8 @@ function requireNative() {
198
198
  try {
199
199
  const binding = require('modbus-rs-darwin-x64')
200
200
  const bindingPackageVersion = require('modbus-rs-darwin-x64/package.json').version
201
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
203
  }
204
204
  return binding
205
205
  } catch (e) {
@@ -214,8 +214,8 @@ function requireNative() {
214
214
  try {
215
215
  const binding = require('modbus-rs-darwin-arm64')
216
216
  const bindingPackageVersion = require('modbus-rs-darwin-arm64/package.json').version
217
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
219
  }
220
220
  return binding
221
221
  } catch (e) {
@@ -234,8 +234,8 @@ function requireNative() {
234
234
  try {
235
235
  const binding = require('modbus-rs-freebsd-x64')
236
236
  const bindingPackageVersion = require('modbus-rs-freebsd-x64/package.json').version
237
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
239
  }
240
240
  return binding
241
241
  } catch (e) {
@@ -250,8 +250,8 @@ function requireNative() {
250
250
  try {
251
251
  const binding = require('modbus-rs-freebsd-arm64')
252
252
  const bindingPackageVersion = require('modbus-rs-freebsd-arm64/package.json').version
253
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
255
  }
256
256
  return binding
257
257
  } catch (e) {
@@ -271,8 +271,8 @@ function requireNative() {
271
271
  try {
272
272
  const binding = require('modbus-rs-linux-x64-musl')
273
273
  const bindingPackageVersion = require('modbus-rs-linux-x64-musl/package.json').version
274
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
276
  }
277
277
  return binding
278
278
  } catch (e) {
@@ -287,8 +287,8 @@ function requireNative() {
287
287
  try {
288
288
  const binding = require('modbus-rs-linux-x64-gnu')
289
289
  const bindingPackageVersion = require('modbus-rs-linux-x64-gnu/package.json').version
290
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
292
  }
293
293
  return binding
294
294
  } catch (e) {
@@ -305,8 +305,8 @@ function requireNative() {
305
305
  try {
306
306
  const binding = require('modbus-rs-linux-arm64-musl')
307
307
  const bindingPackageVersion = require('modbus-rs-linux-arm64-musl/package.json').version
308
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
310
  }
311
311
  return binding
312
312
  } catch (e) {
@@ -321,8 +321,8 @@ function requireNative() {
321
321
  try {
322
322
  const binding = require('modbus-rs-linux-arm64-gnu')
323
323
  const bindingPackageVersion = require('modbus-rs-linux-arm64-gnu/package.json').version
324
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
326
  }
327
327
  return binding
328
328
  } catch (e) {
@@ -339,8 +339,8 @@ function requireNative() {
339
339
  try {
340
340
  const binding = require('modbus-rs-linux-arm-musleabihf')
341
341
  const bindingPackageVersion = require('modbus-rs-linux-arm-musleabihf/package.json').version
342
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
344
  }
345
345
  return binding
346
346
  } catch (e) {
@@ -355,8 +355,8 @@ function requireNative() {
355
355
  try {
356
356
  const binding = require('modbus-rs-linux-arm-gnueabihf')
357
357
  const bindingPackageVersion = require('modbus-rs-linux-arm-gnueabihf/package.json').version
358
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
360
  }
361
361
  return binding
362
362
  } catch (e) {
@@ -373,8 +373,8 @@ function requireNative() {
373
373
  try {
374
374
  const binding = require('modbus-rs-linux-loong64-musl')
375
375
  const bindingPackageVersion = require('modbus-rs-linux-loong64-musl/package.json').version
376
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
378
  }
379
379
  return binding
380
380
  } catch (e) {
@@ -389,8 +389,8 @@ function requireNative() {
389
389
  try {
390
390
  const binding = require('modbus-rs-linux-loong64-gnu')
391
391
  const bindingPackageVersion = require('modbus-rs-linux-loong64-gnu/package.json').version
392
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
394
  }
395
395
  return binding
396
396
  } catch (e) {
@@ -407,8 +407,8 @@ function requireNative() {
407
407
  try {
408
408
  const binding = require('modbus-rs-linux-riscv64-musl')
409
409
  const bindingPackageVersion = require('modbus-rs-linux-riscv64-musl/package.json').version
410
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
412
  }
413
413
  return binding
414
414
  } catch (e) {
@@ -423,8 +423,8 @@ function requireNative() {
423
423
  try {
424
424
  const binding = require('modbus-rs-linux-riscv64-gnu')
425
425
  const bindingPackageVersion = require('modbus-rs-linux-riscv64-gnu/package.json').version
426
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
428
  }
429
429
  return binding
430
430
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('modbus-rs-linux-ppc64-gnu')
442
442
  const bindingPackageVersion = require('modbus-rs-linux-ppc64-gnu/package.json').version
443
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('modbus-rs-linux-s390x-gnu')
458
458
  const bindingPackageVersion = require('modbus-rs-linux-s390x-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -476,8 +476,8 @@ function requireNative() {
476
476
  try {
477
477
  const binding = require('modbus-rs-openharmony-arm64')
478
478
  const bindingPackageVersion = require('modbus-rs-openharmony-arm64/package.json').version
479
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
481
  }
482
482
  return binding
483
483
  } catch (e) {
@@ -492,8 +492,8 @@ function requireNative() {
492
492
  try {
493
493
  const binding = require('modbus-rs-openharmony-x64')
494
494
  const bindingPackageVersion = require('modbus-rs-openharmony-x64/package.json').version
495
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
497
  }
498
498
  return binding
499
499
  } catch (e) {
@@ -508,8 +508,8 @@ function requireNative() {
508
508
  try {
509
509
  const binding = require('modbus-rs-openharmony-arm')
510
510
  const bindingPackageVersion = require('modbus-rs-openharmony-arm/package.json').version
511
- if (bindingPackageVersion !== '0.13.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
- throw new Error(`Native binding package version mismatch, expected 0.13.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.15.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.15.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
@@ -576,8 +576,34 @@ if (!nativeBinding) {
576
576
  }
577
577
 
578
578
  module.exports = nativeBinding
579
+ module.exports.AsyncAsciiTransport = nativeBinding.AsyncAsciiTransport
580
+ module.exports.AsyncRtuTransport = nativeBinding.AsyncRtuTransport
579
581
  module.exports.AsyncSerialModbusClient = nativeBinding.AsyncSerialModbusClient
580
582
  module.exports.AsyncSerialModbusServer = nativeBinding.AsyncSerialModbusServer
581
583
  module.exports.AsyncTcpGateway = nativeBinding.AsyncTcpGateway
582
584
  module.exports.AsyncTcpModbusClient = nativeBinding.AsyncTcpModbusClient
583
585
  module.exports.AsyncTcpModbusServer = nativeBinding.AsyncTcpModbusServer
586
+ module.exports.AsyncTcpTransport = nativeBinding.AsyncTcpTransport
587
+ module.exports.MODBUS_ERROR_CODE_CONNECTION_CLOSED = nativeBinding.MODBUS_ERROR_CODE_CONNECTION_CLOSED
588
+ module.exports.MODBUS_ERROR_CODE_EXCEPTION = nativeBinding.MODBUS_ERROR_CODE_EXCEPTION
589
+ module.exports.MODBUS_ERROR_CODE_INTERNAL = nativeBinding.MODBUS_ERROR_CODE_INTERNAL
590
+ module.exports.MODBUS_ERROR_CODE_INVALID_ARGUMENT = nativeBinding.MODBUS_ERROR_CODE_INVALID_ARGUMENT
591
+ module.exports.MODBUS_ERROR_CODE_TIMEOUT = nativeBinding.MODBUS_ERROR_CODE_TIMEOUT
592
+ module.exports.MODBUS_ERROR_CODE_TRANSPORT = nativeBinding.MODBUS_ERROR_CODE_TRANSPORT
593
+
594
+ // Error code constants
595
+ module.exports.ModbusErrorCode = {
596
+ EXCEPTION: 'MODBUS_EXCEPTION',
597
+ TIMEOUT: 'MODBUS_TIMEOUT',
598
+ TRANSPORT: 'MODBUS_TRANSPORT',
599
+ INVALID_ARGUMENT: 'MODBUS_INVALID_ARGUMENT',
600
+ CONNECTION_CLOSED: 'MODBUS_CONNECTION_CLOSED',
601
+ INTERNAL: 'MODBUS_INTERNAL',
602
+ }
603
+
604
+ // Helper to extract the code from a Modbus error message
605
+ module.exports.getModbusErrorCode = function getModbusErrorCode(err) {
606
+ if (!err || typeof err.message !== 'string') return undefined
607
+ const m = err.message.match(/^\[([A-Z_]+)(?::[^\]]*)?\]/)
608
+ return m ? m[1] : undefined
609
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "modbus-rs",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "High-performance Modbus TCP/RTU/ASCII client, server and gateway for Node.js, powered by Rust",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -8,8 +8,8 @@
8
8
  ".": "./index.js"
9
9
  },
10
10
  "scripts": {
11
- "build": "napi build --platform --release --features nodejs,full --js index.js --dts index.d.ts --output-dir . --manifest-path ../Cargo.toml",
12
- "build:debug": "napi build --platform --features nodejs,full --js index.js --dts index.d.ts --output-dir . --manifest-path ../Cargo.toml",
11
+ "build": "napi build --platform --release --features nodejs-full --js index.js --dts index.d.ts --output-dir . --manifest-path ../Cargo.toml && node scripts/postbuild.js",
12
+ "build:debug": "napi build --platform --features nodejs-full --js index.js --dts index.d.ts --output-dir . --manifest-path ../Cargo.toml && node scripts/postbuild.js",
13
13
  "test": "node --test __test__/*.test.mjs",
14
14
  "prepublishOnly": "napi prepublish -t npm",
15
15
  "artifacts": "napi artifacts",
@@ -64,10 +64,10 @@
64
64
  ],
65
65
  "dependencies": {},
66
66
  "optionalDependencies": {
67
- "modbus-rs-win32-x64-msvc": "0.9.0",
68
- "modbus-rs-linux-x64-gnu": "0.9.0",
69
- "modbus-rs-darwin-x64": "0.9.0",
70
- "modbus-rs-darwin-arm64": "0.9.0",
71
- "modbus-rs-linux-arm64-gnu": "0.9.0"
67
+ "modbus-rs-win32-x64-msvc": "0.15.0",
68
+ "modbus-rs-linux-x64-gnu": "0.15.0",
69
+ "modbus-rs-darwin-x64": "0.15.0",
70
+ "modbus-rs-darwin-arm64": "0.15.0",
71
+ "modbus-rs-linux-arm64-gnu": "0.15.0"
72
72
  }
73
73
  }