@ridge18/web-serial-monitor

0.1.3 • Public • Published

WebSerialMonitor

JavaScript library to interact with serial devices using the Web Serial API in the browser. The library allow to send data and receive data in text or binary modes.

Please note that this library requires the WebSerial browser API. Check https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API for browser compatibility.

Installation

Use npm to install this library in a nodejs project:

npm install @ridge18/web-serial-monitor

Usage

Init in text mode (defaut)

import SerialMonitor from '@ridge18/web-serial-monitor';

const serial = new SerialMonitor({mode: "text"});

Init in text mode with line parser (line break: '\n')

import SerialMonitor from '@ridge18/web-serial-monitor';

const serial = new SerialMonitor({mode: "text", parseLines: true});

Init in binary mode

import SerialMonitor from '@ridge18/web-serial-monitor';

const serial = new SerialMonitor({mode: "byte", hex: true});

Connect to a device

// serial is an instance of a extended EventTarget class
// you can listen to events emited by it.
const handleSerialEvent = (ev) => {
    console.log(ev.detail);
}
serial.addEventListener('serial-connected', handleSerialEvent);

serial.addEventListener('serial-disconnected', handleSerialEvent);

serial.addEventListener('serial-error', handleSerialEvent);

serial.addEventListener('serial-data', handleSerialEvent);

// This will open a browser dialog which prompts the user to select a serial device.
// If no device is selected, then a serial-error event is fired.
serial.connect(57600).then(() => { // connect at 57600 bauds rate.
	serial.send("Hello serial\n");
}).catch(() => {
	console.log("Something went wrong...");
}); 

serial.disconnect();

Test

This test use a simple node web server to load the library directly into the browser, no module bundler needed.

npm run test

License

The project is licensed under the GNU General Public License v3 (GPL-3)

Package Sidebar

Install

npm i @ridge18/web-serial-monitor

Weekly Downloads

4

Version

0.1.3

License

GPL-3.0

Unpacked Size

14.3 kB

Total Files

5

Last publish

Collaborators

  • ridge18