mbus-ffi
WASM/JS bindings for the modbus-rs stack.
Position In Workspace
mbus-ffi is an implementation crate inside this workspace.
For browser/WASM application code, import and use mbus-ffi directly.
modbus-rs does not provide a top-level wasm feature and does not re-export
mbus-ffi WASM-facing types.
This crate exposes browser-friendly Modbus clients over:
- WebSocket (Modbus TCP gateway):
WasmModbusClient - Web Serial (RTU/ASCII):
request_serial_port,WasmSerialPortHandle,WasmSerialModbusClient
All APIs are Promise-based and are designed for browser runtimes (wasm32).
Status
Implemented and usable for browser integration and smoke testing.
What This Crate Exports
When compiled for wasm32, mbus-ffi exports:
WasmModbusClient(WebSocket transport)request_serial_port()WasmSerialPortHandleWasmSerialModbusClient(Web Serial transport)
These symbols are conditionally compiled behind target_arch = "wasm32" so native builds are unaffected.
Feature Flags
mbus-ffi uses modular feature flags:
wasm: enables WASM bindings and browser transports (mbus-network/wasm,mbus-serial/wasm)coilsregistersdiscrete-inputsfifofile-recorddiagnosticsfull: enables all Modbus service features above
Typical web builds use --features wasm,full.
Build WASM Package
From mbus-ffi:
Generated JS/WASM package is written to mbus-ffi/pkg.
Quick Start (WebSocket)
import init from "./pkg/mbus_ffi.js";
await ;
const client = ;
const regs = await client.;
console.log;
Quick Start (Web Serial)
import init from "./pkg/mbus_ffi.js";
await ;
// Must be called from a user gesture (e.g. button click)
const portHandle = await ;
const client = ;
const ok = await client.;
console.log;
Supported Modbus Operations
Both WASM clients expose the same service surface:
- Coils: read single/multiple, write single/multiple
- Registers: read holding/input, write single/multiple, mask write, read-write multiple
- Discrete inputs: read single/multiple
- FIFO queue: read
- File record: read/write
- Diagnostics: exception status, diagnostics, comm event counter/log, report server id, read device identification
Example Smoke Pages
Use the browser examples under mbus-ffi/examples:
network_smoke.html(WebSocket/TCP path)serial_smoke.html(Web Serial path, full serial API smoke runner)
Serve the examples over localhost after building pkg, for example:
Then open:
http://localhost:8089/examples/network_smoke.htmlhttp://localhost:8089/examples/serial_smoke.html
Running WASM Tests
The E2E WASM tests live in mbus-ffi/tests/wasm_e2e.rs and run in browser mode.
Run the full browser feature test suite:
;
Fast compile check:
Run browser tests (Chrome headless):
Browser Requirements (Serial)
Web Serial requires:
- Chromium-based browser
- Secure context (HTTPS) or localhost
- User gesture for
request_serial_port()
Notes
- Promise rejection errors are surfaced as stringified internal errors.
WasmSerialModbusClientuses serial-safe pipeline behavior internally.- Native (non-wasm32) consumers should use the core Rust crates directly.
License
Licensed under the repository root LICENSE.