modbus-rs-wasm 0.14.2 → 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.
package/README.md CHANGED
@@ -100,7 +100,8 @@ Ready-to-run HTML examples demonstrating both Modbus client and server functiona
100
100
  - **[wasm_client/serial_smoke.html](https://github.com/Raghava-Ch/modbus-rs/blob/main/mbus-ffi/wasm/examples/wasm_client/serial_smoke.html)**: Web Serial Modbus RTU client example.
101
101
  - **[wasm_server/network_smoke.html](https://github.com/Raghava-Ch/modbus-rs/blob/main/mbus-ffi/wasm/examples/wasm_server/network_smoke.html)**: WebSocket Modbus TCP server example.
102
102
  - **[wasm_server/serial_smoke.html](https://github.com/Raghava-Ch/modbus-rs/blob/main/mbus-ffi/wasm/examples/wasm_server/serial_smoke.html)**: Web Serial Modbus RTU server example.
103
- - **[real world example](https://github.com/Raghava-Ch/modbus-lab)**: Comes with demo web client and tauri based desktop client and server simulators with source code.
103
+ - **[Demo Application](https://modbus-lab.vercel.app)**: Live web application demonstrating modbus-rs-wasm in action. [modbus-lab.vercel.app](https://modbus-lab.vercel.app)
104
+ - **[Real-World Example Source Code](https://github.com/Raghava-Ch/modbus-lab)**: Repository containing the source code for the demo web client and Tauri-based desktop client/server simulators.
104
105
 
105
106
  To run the examples locally:
106
107
  ```bash
@@ -135,16 +136,15 @@ document.getElementById('connect-btn').addEventListener('click', async () => {
135
136
  stopBits: 1,
136
137
  parity: 'even',
137
138
  responseTimeoutMs: 1000,
138
- retryAttempts: 3,
139
- tickIntervalMs: 20
139
+ retryAttempts: 3
140
140
  });
141
141
 
142
142
  // 3. Spawn a client for a specific slave (Unit ID)
143
- const client = transport.create_client({ unitId: 10 });
143
+ const client = transport.createClient({ unitId: 10 });
144
144
 
145
145
  // 4. Read coils
146
- const coils = await client.read_coils(0, 8);
147
- console.log('Coils:', coils); // Uint8Array
146
+ const coils = await client.readCoils({ address: 0, quantity: 8 });
147
+ console.log('Coils:', coils); // boolean[]
148
148
 
149
149
  } catch (err) {
150
150
  console.error('Serial error:', err);
@@ -160,18 +160,17 @@ import { WasmTcpTransport } from 'modbus-rs-wasm';
160
160
 
161
161
  async function readRegisters() {
162
162
  // 1. Connect to a WebSocket proxy that forwards to a Modbus TCP device
163
- const transport = new WasmTcpTransport('ws://localhost:8502', {
163
+ const transport = await WasmTcpTransport.connect('ws://localhost:8502', {
164
164
  responseTimeoutMs: 5000,
165
- retryAttempts: 3,
166
- tickIntervalMs: 20
165
+ retryAttempts: 3
167
166
  });
168
167
 
169
168
  // 2. Spawn a client attached to Unit ID 1
170
- const client = transport.create_client({ unitId: 1 });
169
+ const client = transport.createClient({ unitId: 1 });
171
170
 
172
171
  try {
173
172
  // 3. Read 10 holding registers starting at address 0
174
- const registers = await client.read_holding_registers(0, 10);
173
+ const registers = await client.readHoldingRegisters({ address: 0, quantity: 10 });
175
174
  console.log('Holding registers:', registers); // Uint16Array
176
175
  } catch (error) {
177
176
  console.error('Failed to read registers:', error);
@@ -181,36 +180,40 @@ async function readRegisters() {
181
180
 
182
181
  ### Modbus server demo with modbus-rs-wasm
183
182
 
184
- The `modbus-rs-wasm` package also provides building blocks for server simulation inside the browser. You can instantiate a `WasmTcpServer` or `WasmSerialServer` by providing a configuration and a JavaScript callback to process incoming requests.
183
+ The `modbus-rs-wasm` package also provides building blocks for server simulation inside the browser. You can bind a `WasmTcpServer` or `WasmSerialServer` by providing configurations and an object implementing `ServerHandlers` callback methods.
185
184
 
186
185
  Here is a quick example of setting up a simulated server via a WebSocket TCP gateway:
187
186
 
188
187
  ```javascript
189
- import { WasmTcpGatewayConfig, WasmTcpServer } from 'modbus-rs-wasm';
188
+ import { WasmTcpServer } from 'modbus-rs-wasm';
190
189
 
191
190
  async function simulateServer() {
192
- // 1. Configure the server (e.g., pointing to a WebSocket gateway)
193
- const config = new WasmTcpGatewayConfig("ws://localhost:8080");
194
-
195
- // 2. Create the server and define the request handler
196
- const server = new WasmTcpServer(config, async (request) => {
197
- console.log("Received Modbus request:", request);
198
-
199
- // Simulate processing the request
200
- // The handler can be fully asynchronous and return a Promise
201
- return {
202
- // Return appropriate response fields based on the request
203
- success: true,
204
- data: [100, 200, 300]
205
- };
191
+ // 1. Define the request handlers
192
+ const handlers = {
193
+ onReadHoldingRegisters: async (request) => {
194
+ console.log("Received Modbus request:", request);
195
+
196
+ // Callbacks can return values synchronously or as Promises
197
+ return [100, 200, 300];
198
+ }
199
+ };
200
+
201
+ // 2. Bind the server
202
+ const server = await WasmTcpServer.bind(
203
+ {
204
+ wsUrl: "ws://localhost:8080",
205
+ unitId: 1
206
+ },
207
+ handlers
208
+ );
209
+
210
+ // 3. Start the event loop (runs asynchronously in background)
211
+ server.serve().catch(err => {
212
+ console.error("Server crashed:", err);
206
213
  });
207
-
208
- // 3. Start the server
209
- server.start();
210
214
 
211
- // Observe the server status
212
- console.log("Server running:", server.is_running());
213
- console.log("Server status:", server.status_snapshot());
215
+ // To stop the server later:
216
+ // await server.shutdown();
214
217
  }
215
218
  ```
216
219
 
@@ -219,34 +222,41 @@ async function simulateServer() {
219
222
  You can also create a simulated serial server (RTU or ASCII) and attach a browser `SerialPort` to it using the Web Serial API:
220
223
 
221
224
  ```javascript
222
- import { WasmSerialServerConfig, WasmSerialServer } from 'modbus-rs-wasm';
225
+ import { WasmSerialServer } from 'modbus-rs-wasm';
223
226
 
224
227
  async function simulateSerialServer() {
225
228
  // 1. Request a serial port from the user (requires user gesture)
226
229
  const port = await navigator.serial.requestPort();
227
230
 
228
- // 2. Configure the server for RTU or ASCII mode
229
- const config = WasmSerialServerConfig.rtu(); // or .ascii()
230
-
231
- // 3. Create the server with a request handler callback
232
- const server = new WasmSerialServer(config, async (request) => {
233
- console.log("Received Modbus request via Serial:", request);
234
- return {
235
- success: true,
236
- data: [100, 200, 300]
237
- };
231
+ // 2. Define the request handlers
232
+ const handlers = {
233
+ onReadHoldingRegisters: (request) => {
234
+ console.log("Received Modbus request via Serial:", request);
235
+ return [100, 200, 300];
236
+ }
237
+ };
238
+
239
+ // 3. Bind the server for RTU mode
240
+ const server = await WasmSerialServer.bindRtu(
241
+ {
242
+ serialPort: port,
243
+ unitId: 1,
244
+ baudRate: 19200
245
+ },
246
+ handlers
247
+ );
248
+
249
+ // 4. Start the event loop
250
+ server.serve().catch(err => {
251
+ console.error("Serial Server crashed:", err);
238
252
  });
239
-
240
- // 4. Attach the browser serial port
241
- server.attach_serial_port(port);
242
-
243
- // 5. Start the server
244
- server.start();
245
-
246
- console.log("Serial Server running:", server.is_running());
247
253
  }
248
254
  ```
249
255
 
256
+ ## Migration Guide
257
+
258
+ Detailed step-by-step migration guides are available in the [Migration Guides](https://github.com/Raghava-Ch/modbus-rs/tree/main/documentation/migrations) directory.
259
+
250
260
  ## License
251
261
 
252
262
  GPL-3.0-only — see [LICENSE](./LICENSE). A commercial license is available for proprietary use.