modbus-rs-wasm 0.13.0 → 0.14.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Browser-native WebAssembly bindings for [modbus-rs](https://github.com/Raghava-Ch/modbus-rs), enabling Modbus TCP (via WebSockets) and Modbus RTU/ASCII (via Web Serial) directly in the browser.
4
4
 
5
- > **Note:** If you are building a Node.js backend application, use the native [`modbus-rs`](https://www.npmjs.com/package/modbus-rs) package instead.
5
+ > **Note:** This package is designed for browser-native environments (using WebSockets and Web Serial). If you are building a Node.js backend application, use the native [`modbus-rs`](https://www.npmjs.com/package/modbus-rs) package instead. Running this package in pure Node.js requires `--experimental-wasm-modules` and is not officially supported.
6
6
 
7
7
  ## Installation
8
8
 
@@ -10,6 +10,29 @@ Browser-native WebAssembly bindings for [modbus-rs](https://github.com/Raghava-C
10
10
  npm install modbus-rs-wasm
11
11
  ```
12
12
 
13
+ ## Integration & Frameworks (Vite, Svelte, React, etc.)
14
+
15
+ No custom resolver aliases or configuration workarounds are required starting in `v0.14.0`. Standard package entry points are resolved automatically based on your builder/bundler targets.
16
+
17
+ ### Web (Direct / HTML / Vanilla JS)
18
+ When loading the package in browser environments without a bundler, import the web entry point and await the initialization promise:
19
+
20
+ ```javascript
21
+ import init, { WasmModbusClient } from 'modbus-rs-wasm/dist/web/modbus-rs.js';
22
+
23
+ await init();
24
+ const client = new WasmModbusClient('ws://localhost:8502', 1, 5000, 3, 20);
25
+ ```
26
+
27
+ ### Bundlers & Frameworks (Vite, SvelteKit, Next.js, etc.)
28
+ When using modern bundlers, the root import automatically maps to the bundler target:
29
+
30
+ ```javascript
31
+ import { WasmModbusClient } from 'modbus-rs-wasm';
32
+ ```
33
+ *(Make sure your bundler is configured to load WebAssembly, e.g., using `vite-plugin-wasm` and `vite-plugin-top-level-await` in Vite).*
34
+
35
+
13
36
  ## Quick Start (Modbus TCP via WebSocket Gateway)
14
37
 
15
38
  First, run the gateway [modbus-gateway](https://github.com/Raghava-Ch/modbus-gateway).
@@ -1,10 +1,35 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
 
4
+ export interface WasmSerialTransportOptions {
5
+ mode?: "rtu" | "ascii";
6
+ baudRate: number;
7
+ dataBits?: 5 | 6 | 7 | 8;
8
+ stopBits?: 1 | 2;
9
+ parity?: "none" | "even" | "odd";
10
+ responseTimeoutMs?: number;
11
+ retryAttempts?: number;
12
+ tickIntervalMs?: number;
13
+ }
14
+
15
+
16
+
17
+ export interface WasmTcpTransportOptions {
18
+ responseTimeoutMs?: number;
19
+ retryAttempts?: number;
20
+ tickIntervalMs?: number;
21
+ }
22
+ export interface WasmCreateClientOptions {
23
+ unitId?: number;
24
+ }
25
+
26
+
27
+
4
28
  /**
5
- * Browser-facing Modbus client that communicates over a WebSocket transport.
29
+ * Browser-facing Modbus client bound to a specific unit ID.
6
30
  */
7
31
  export class WasmModbusClient {
32
+ private constructor();
8
33
  free(): void;
9
34
  [Symbol.dispose](): void;
10
35
  /**
@@ -28,13 +53,11 @@ export class WasmModbusClient {
28
53
  */
29
54
  get_comm_event_log(): Promise<any>;
30
55
  /**
31
- * Returns `true` if there are in-flight Modbus requests waiting for
32
- * response/timeout resolution.
56
+ * Returns `true` if there are in-flight Modbus requests.
33
57
  */
34
58
  has_pending_requests(): boolean;
35
59
  /**
36
- * Returns `true` when the underlying WebSocket is open and the transport
37
- * considers itself connected.
60
+ * Returns `true` when the underlying WebSocket is open.
38
61
  */
39
62
  is_connected(): boolean;
40
63
  /**
@@ -44,17 +67,6 @@ export class WasmModbusClient {
44
67
  * Returns a `Promise` resolving with `true` or rejects on error.
45
68
  */
46
69
  mask_write_register(address: number, and_mask: number, or_mask: number): Promise<any>;
47
- /**
48
- * Create a new Modbus master client and immediately start the background tick loop.
49
- *
50
- * # Arguments
51
- * - `ws_url` — WebSocket URL of the Modbus/TCP gateway (e.g. `"ws://192.168.1.1:8502"`).
52
- * - `unit_id` — Modbus unit ID / slave address of the target device (1–247).
53
- * - `response_timeout_ms` — How long (ms) to wait before retrying or failing a request.
54
- * - `retry_attempts` — Number of retries before reporting an error to JS.
55
- * - `tick_interval_ms` — How often (ms) the tick loop calls `poll()`. 20 ms is a safe default.
56
- */
57
- constructor(ws_url: string, unit_id: number, response_timeout_ms: number, retry_attempts: number, tick_interval_ms: number);
58
70
  /**
59
71
  * Read `quantity` coils starting at `address`.
60
72
  *
@@ -142,11 +154,6 @@ export class WasmModbusClient {
142
154
  * Returns a `Promise` resolving with a `Uint16Array` (the values read) or rejects on error.
143
155
  */
144
156
  read_write_multiple_registers(read_address: number, read_quantity: number, write_address: number, _write_quantity: number, values: Uint16Array): Promise<any>;
145
- /**
146
- * Drop all pending in-flight requests and attempt to reconnect the WebSocket.
147
- * Outstanding Promises for dropped requests will be rejected with `"ConnectionLost"`.
148
- */
149
- reconnect(): boolean;
150
157
  /**
151
158
  * Report Server ID (FC 17).
152
159
  *
@@ -188,9 +195,10 @@ export class WasmModbusClient {
188
195
  }
189
196
 
190
197
  /**
191
- * Browser-facing Modbus client that communicates over Web Serial RTU or ASCII.
198
+ * Browser-facing Modbus client bound to a specific serial unit ID.
192
199
  */
193
200
  export class WasmSerialModbusClient {
201
+ private constructor();
194
202
  free(): void;
195
203
  [Symbol.dispose](): void;
196
204
  /**
@@ -214,12 +222,11 @@ export class WasmSerialModbusClient {
214
222
  */
215
223
  get_comm_event_log(): Promise<any>;
216
224
  /**
217
- * Returns `true` if there are in-flight Modbus requests waiting for
218
- * response/timeout resolution.
225
+ * Returns `true` if there are in-flight Modbus requests.
219
226
  */
220
227
  has_pending_requests(): boolean;
221
228
  /**
222
- * Returns `true` when the serial port is open and the transport considers itself connected.
229
+ * Returns `true` when the serial port is open.
223
230
  */
224
231
  is_connected(): boolean;
225
232
  /**
@@ -229,13 +236,6 @@ export class WasmSerialModbusClient {
229
236
  * Returns a `Promise` resolving with `true` or rejects on error.
230
237
  */
231
238
  mask_write_register(address: number, and_mask: number, or_mask: number): Promise<any>;
232
- /**
233
- * Creates a Modbus serial client over browser Web Serial.
234
- *
235
- * `mode` accepts "rtu" or "ascii" (case-insensitive).
236
- * `parity` accepts "none", "even", or "odd".
237
- */
238
- constructor(port_handle: WasmSerialPortHandle, unit_id: number, mode: string, baud_rate: number, data_bits: number, stop_bits: number, parity: string, response_timeout_ms: number, retry_attempts: number, tick_interval_ms: number);
239
239
  /**
240
240
  * Read `quantity` coils starting at `address`.
241
241
  *
@@ -320,11 +320,6 @@ export class WasmSerialModbusClient {
320
320
  * Returns a `Promise` resolving with a `Uint16Array` (the values read) or rejects on error.
321
321
  */
322
322
  read_write_multiple_registers(read_address: number, read_quantity: number, write_address: number, _write_quantity: number, values: Uint16Array): Promise<any>;
323
- /**
324
- * Drop all pending in-flight requests and attempt to reopen the serial port.
325
- * Outstanding Promises for dropped requests will be rejected with `"ConnectionLost"`.
326
- */
327
- reconnect(): boolean;
328
323
  /**
329
324
  * Report Server ID (FC 17).
330
325
  *
@@ -461,6 +456,41 @@ export class WasmSerialServerConfig {
461
456
  static rtu(): WasmSerialServerConfig;
462
457
  }
463
458
 
459
+ /**
460
+ * Connection and polling manager for browser Modbus Serial clients.
461
+ */
462
+ export class WasmSerialTransport {
463
+ free(): void;
464
+ [Symbol.dispose](): void;
465
+ /**
466
+ * Closes the serial port connection and rejects all pending requests.
467
+ */
468
+ close(): void;
469
+ /**
470
+ * Creates a device client bound to the specified unit ID.
471
+ */
472
+ createClient(options?: WasmCreateClientOptions | null): WasmSerialModbusClient;
473
+ /**
474
+ * Returns `true` if there are in-flight Modbus requests.
475
+ */
476
+ has_pending_requests(): boolean;
477
+ /**
478
+ * Returns `true` when the serial port is open and connected.
479
+ */
480
+ is_connected(): boolean;
481
+ /**
482
+ * Creates a new Serial (RTU) transport using the provided `port_handle`.
483
+ *
484
+ * The transport claims the underlying serial stream and manages the
485
+ * read/write loop, allowing multiple clients to multiplex requests over it.
486
+ */
487
+ constructor(port_handle: WasmSerialPortHandle, options?: WasmSerialTransportOptions | null);
488
+ /**
489
+ * Drop all pending in-flight requests and attempt to reopen the serial port.
490
+ */
491
+ reconnect(): boolean;
492
+ }
493
+
464
494
  /**
465
495
  * High-level design descriptor for phased server binding rollout.
466
496
  */
@@ -637,6 +667,41 @@ export class WasmTcpServer {
637
667
  ws_url(): string;
638
668
  }
639
669
 
670
+ /**
671
+ * Connection and background polling manager for browser Modbus TCP clients.
672
+ */
673
+ export class WasmTcpTransport {
674
+ free(): void;
675
+ [Symbol.dispose](): void;
676
+ /**
677
+ * Closes the connection and rejects all pending requests.
678
+ */
679
+ close(): void;
680
+ /**
681
+ * Creates a device client bound to the specified unit ID.
682
+ */
683
+ createClient(options?: WasmCreateClientOptions | null): WasmModbusClient;
684
+ /**
685
+ * Returns `true` if there are in-flight Modbus requests.
686
+ */
687
+ has_pending_requests(): boolean;
688
+ /**
689
+ * Returns `true` when the underlying WebSocket is open and connected.
690
+ */
691
+ is_connected(): boolean;
692
+ /**
693
+ * Creates a new TCP/WebSocket transport.
694
+ *
695
+ * This establishes a connection to the provided `ws_url` and handles
696
+ * the underlying read/write loops.
697
+ */
698
+ constructor(ws_url: string, options?: WasmTcpTransportOptions | null);
699
+ /**
700
+ * Drop all pending in-flight requests and attempt to reconnect.
701
+ */
702
+ reconnect(): boolean;
703
+ }
704
+
640
705
  /**
641
706
  * Requests a browser serial port from `navigator.serial.requestPort()`.
642
707
  *
@@ -5,5 +5,5 @@ import { __wbg_set_wasm } from "./modbus-rs_bg.js";
5
5
  __wbg_set_wasm(wasm);
6
6
  wasm.__wbindgen_start();
7
7
  export {
8
- WasmModbusClient, WasmSerialModbusClient, WasmSerialPortHandle, WasmSerialServer, WasmSerialServerConfig, WasmServerBindingPlan, WasmServerStatusSnapshot, WasmServerTransportKind, WasmTcpGatewayConfig, WasmTcpServer, request_serial_port
8
+ WasmModbusClient, WasmSerialModbusClient, WasmSerialPortHandle, WasmSerialServer, WasmSerialServerConfig, WasmSerialTransport, WasmServerBindingPlan, WasmServerStatusSnapshot, WasmServerTransportKind, WasmTcpGatewayConfig, WasmTcpServer, WasmTcpTransport, request_serial_port
9
9
  } from "./modbus-rs_bg.js";