Git Product home page Git Product logo

browser-serial's Introduction

browser-serial

contributions welcome DOI

API for communicating with serial ports in the browser. This package is currently under active development and contributions are more than welcomed!

About

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.

Installation

npm install browser-serial

Usage:

Import the package

import { BrowserSerial } from "browser-serial";
const serial = new BrowserSerial();

Connect/disconnect to the serial port on a user-action

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 from the port

// 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;
  }
}

Write data to the port

cmdInput = document.getElementById("cmd-input");

cmdInput.addEventListener("change", (e) => serial.write(e.target.value));

Acknowledgments

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.

browser-serial's People

Contributors

sbooeshaghi avatar manzt avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.