modbus-rs-wasm 0.12.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 +68 -0
- package/dist/bundler/modbus-rs.d.ts +645 -0
- package/dist/bundler/modbus-rs.js +9 -0
- package/dist/bundler/modbus-rs_bg.wasm +0 -0
- package/dist/web/modbus-rs.d.ts +800 -0
- package/dist/web/modbus-rs.js +2040 -0
- package/dist/web/modbus-rs_bg.wasm +0 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# modbus-rs-wasm
|
|
2
|
+
|
|
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
|
+
|
|
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.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install modbus-rs-wasm
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start (Modbus TCP via WebSocket Gateway)
|
|
14
|
+
|
|
15
|
+
First, run the gateway [modbus-gateway](https://github.com/Raghava-Ch/modbus-gateway).
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { WasmModbusClient } from 'modbus-rs-wasm';
|
|
19
|
+
|
|
20
|
+
async function readRegisters() {
|
|
21
|
+
// Connect to a WebSocket proxy that forwards to a Modbus TCP device
|
|
22
|
+
// (e.g. ws_url, unit_id, response_timeout_ms, retries, tick_interval_ms)
|
|
23
|
+
const client = new WasmModbusClient('ws://localhost:8502', 1, 5000, 3, 20);
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
// Read 10 holding registers starting at address 0
|
|
27
|
+
const registers = await client.read_holding_registers(0, 10);
|
|
28
|
+
console.log('Holding registers:', registers); // Uint16Array
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('Failed to read registers:', error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick Start (Modbus RTU via Web Serial)
|
|
36
|
+
|
|
37
|
+
*Web Serial requires a Chromium-based browser (Chrome, Edge, Opera) and must be initiated by a user gesture (e.g., button click).*
|
|
38
|
+
|
|
39
|
+
If you are using modbus-rs on Node.js, you can use the native [`modbus-rs`](https://www.npmjs.com/package/modbus-rs) package instead.
|
|
40
|
+
|
|
41
|
+
If you have limitation on serial port using browser, then you may be interested in connecting serial port over ws/tcp so you can use the gateway application [modbus-gateway](https://github.com/Raghava-Ch/modbus-gateway).
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { request_serial_port, WasmSerialModbusClient } from 'modbus-rs-wasm';
|
|
45
|
+
|
|
46
|
+
document.getElementById('connect-btn').addEventListener('click', async () => {
|
|
47
|
+
try {
|
|
48
|
+
// 1. Prompt user to select a serial port
|
|
49
|
+
const portHandle = await request_serial_port();
|
|
50
|
+
|
|
51
|
+
// 2. Connect client (handle, unit_id, mode, baud, data_bits, stop_bits, parity, timeout, retries, tick)
|
|
52
|
+
const client = new WasmSerialModbusClient(
|
|
53
|
+
portHandle, 1, 'rtu', 19200, 8, 1, 'even', 1000, 3, 20
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// 3. Read coils
|
|
57
|
+
const coils = await client.read_coils(0, 8);
|
|
58
|
+
console.log('Coils:', coils); // Uint8Array
|
|
59
|
+
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error('Serial error:', err);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
GPL-3.0-only — see [LICENSE](./LICENSE). A commercial license is available for proprietary use.
|