API for communicating with serial ports in the browser. This package is currently under active development and contributions are more than welcomed!
This API builds off of the WICG Serial specification that is currently in an "experimental" phase in Chromium browsers and must be enabled in your browser's Experimental Web Platform Features to use. Copy and paste the following URL to enable it in your browser:
chrome://flags/#enable-experimental-web-platform-features
opera://flags/#enable-experimental-web-platform-features
edge://flags/#enable-experimental-web-platform-features
To learn more about the status of this feature, see the WebSerial API Chrome Platform Status.
npm install browser-serial
Import the package
import { BrowserSerial } from "browser-serial";
const serial = new BrowserSerial();
For example, connect/disconnect to the port when the user clicks a button.
const serial = new BrowserSerial();
connectButton = document.getElementById("connect-button");
disconnectButton = document.getElementById("disconnect-button");
connectButton.addEventListener("click", () => serial.connect());
disconnectButton.addEventListener("click", () => serial.disconnect());
// read data continuously, readLoop takes a callback
serial.readLoop(console.log)
// read data line by line as it comes in
for await (let { value, done } of serial.readLineGenerator()) {
console.log(value)
if (done === "true") {
break;
}
}
cmdInput = document.getElementById("cmd-input");
cmdInput.addEventListener("change", (e) => serial.write(e.target.value));
This code was motivated by https://github.com/GoogleChromeLabs/serial-terminal and builds on the work of those who contributed to the WICG Serial specification, https://github.com/wicg/serial/graphs/contributors.