npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serialport-binding-webserialapi

v1.0.3

Published

Web Serial API binding for Node Serialport

Downloads

519

Readme

Web Serial API binding for Node.js serialport

import SerialPort from '@serialport/stream';
import WSABinding from 'serialport-binding-webserialapi';

SerialPort.Binding = WSABinding;

SerialPort.list().then(portsList => {
    console.log(JSON.stringify(portsList));
}, err => {
    console.error(JSON.stringify(err));
});

Installation

This is a Node.js module available through the npm registry. Before installing, download and install Node.js. If this is a brand new project, make sure to create a package.json first with the npm init command. Installation is done using the npm install command:

$ npm install serialport-binding-webserialapi

As this module provides Web Serial API bindings for node.js serialport, applications using it will require @serialport/stream module installed as well. This is provided for You by Node.js dependency mechanism.

Browser support

On 29th Dec 2020, Web Serial API is supported in modern Chromium based browsers, that is Chrome, Edge, Opera and of course Chromium.

Special activities needed to enable Web Serial API support

  • Chromium based browsers require enabling Experimental Web Platform Features Chrome & Chromium: chrome://flags/#enable-experimental-web-platform-features Opera: opera://flags/#enable-experimental-web-platform-features Edge: edge://flags/#enable-experimental-web-platform-features
  • On Linux, snap based Chromium installation requires connecting Chromium snap to USB mapped serial ports sudo snap connect chromium:raw-usb

Features

  • Enables to use Node.js serialport module based apps in browser almost directly
  • Supports base read/write operations and setting/getting serial port flags
  • Supports USB Vendor & Product ID based port selection limitation

Docs

Usage of Web Serial API is straightforward, as it does not expose new functionality, merely binds the well documented Node Serialport to the Web Serial API available in browsers. See the example below for usage and referenced documentation.

Issuses & Limitations

  • update command is not implemented, as there is no support in Web Serial API for baud rate change for open port
  • flush is not implemented, no specific function in Web Serial API; need to check if possible thru the underlying stream objects
  • drain is not implemented, no specific function in Web Serial API; need to check if possible thru the underlying stream objects or write function Promise
  • as of 1.0.3 release date, getPorts() in Web Serial API as implemented in Chromium does not return list of serial ports allowed by user, this means at every start of application user will have to select serial port in a popup shown by the browser
  • tested only in Chromium 87.0.4280.88
  • Web Serial API specification is a living document and it's implementation is experimental; things might break any moment

Example

This is a very simple TypeScript example of a browser serial terminal application using jQuery, Serialport and Web Serial API

// very simple serial terminal
import $ from 'jquery';
import './vendor';
import SerialPort from '@serialport/stream';
import WSABinding from 'serialport-binding-webserialapi';
import stripAnsi from 'strip-ansi';

// terminal settings
// does not echo
var term_speed = 115200;

// serial port binding to Web Serial API
SerialPort.Binding = WSABinding;

// construct simple webpage
var myPort = undefined;
var actref = $(document.createElement("button"));
var twinref = $(document.createElement("textarea"));
var bref = $("body");
bref.html('');
bref.append(actref);
bref.append(twinref);
bref.children().css("display", "block").css("font-family", "monospace");
twinref.prop( "disabled", true );
twinref.attr("rows", "24");
twinref.attr("cols", "80");
actref.text('Start serial terminal');
var ta = $('textarea');
window.setInterval(() => { 
    var tal = ta.val().length;
    ta.scrollTop(ta[0].scrollHeight);
    ta[0].setSelectionRange(tal, tal);
}, 100);

// handle terminal startup - must be user initiated!
actref.click(() => {
    myPort = new SerialPort('wsa://default', {
        baudRate: term_speed,
        autoOpen: true
    });
    myPort.on('data', data => {
        for(var i = 0; i<data.length; i++) {
            if (data[i] == 8) { // simplistic
                ta.val(ta.val().slice(0,-1));
            }
        }
        var re = /[\0-\x1F\x7F-\x9F\xAD]/;
        ta.val(ta.val() + stripAnsi(data.toString()).replace(re,''));
    });
    myPort.on('open', () => {
        ta.prop( "disabled", false );
        ta.focus();
    })
})

// handle terminal text input
function twrite(c:string) {
    myPort.write(c, err => {
        if (err) {
            console.log('Error on write: ', err.message);                    
        }
    });
}
twinref.on('keydown', e => {
    var tval = undefined;
    if (e.which == 13) {
        tval = "\r\n";
    } else if (e.which == 8) {
        tval = "\b";
    } else if (e.which == 9) {
        tval = "\t";
    } else {
        return;
    }
    twrite(tval);
    e.preventDefault();  
});
twinref.on('keyup', e => {
    e.preventDefault();
});
twinref.on('keypress', e => {
    twrite(String.fromCharCode(e.which));
    e.preventDefault();
});

People

The original author is Marcin Galczynski

License

MIT