Getting started
Quick start
List ports, open one, stream data, talk to a modem and survive unplugs.
import { SerialPort } from "tauri-plugin-serialplugin-api";
const ports = await SerialPort.available_ports(); // { "/dev/ttyUSB0": { type: "USB", vid, pid, … }, … }
const port = new SerialPort({ path: Object.keys(ports)[0], baudRate: 115200 });
await port.open();
const handle = await port.watch({
onData: (text) => console.log("rx", text),
onDisconnect: (reason) => console.log("gone:", reason),
});
await port.write("hello\n");
await handle.unwatch();
await port.close();
Binary data: writeBinary(new Uint8Array([…])) and
watch(handlers, { decode: false }) (or readBinary()).
Talking to a modem
const r = await port.sendAt("AT+CSQ"); // queued, waits for OK / ERROR
console.log(r.status, r.solicitedBody); // "ok", ["+CSQ: 27,0"]
See AT commands, exchange and CMUX.
Surviving unplugs
port.enableAutoReconnect({ interval: 3000, maxAttempts: null });
// after onDisconnect the port is reopened and the watch restored
See Auto-reconnect.