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

duinocom

v1.0.0

Published

A Simple Arduino Serial Data Reader & Writer

Downloads

6

Readme

DuinoCom

Overview

DuinoCom is a simple JavaScript class designed to facilitate communication with Arduino or similar microcontrollers via a serial port in a web browser environment. This package utilizes the Web Serial API to establish a connection and exchange data with the serial port.

Browser Support

This npm package is intended for use in web browsers. Please refer to Can I use Web Serial API for information on browser compatibility.

Installation

You can install DuinoCom via npm:

npm install duinocom

Usage

Importing the Package

import DuinoCom from 'duinocom';

Creating an Instance

To create an instance of DuinoCom, you need to specify the baud rate:

const duino = new DuinoCom(9600);

Connecting to the Serial Port

Before sending or receiving data, you need to establish a connection to the serial port:

await duino.connect();

Sending Data

You can send data to the connected serial port using the send() method. By default, the data is expected to be a string:

await duino.send("Hello Arduino!");

If you need to send a byte array, you can specify the isByteArray parameter as true:

const byteArray = [0x48, 0x65, 0x6C, 0x6C, 0x6F]; // "Hello" in ASCII
await duino.send(byteArray, true);

Receiving Data

To listen for incoming data from the serial port, you can use the listen() method. You can provide a callback function to process the received data:

duino.listen((data) => {
  console.log("Received data:", data);
});

The listen() method expects the incoming data to be terminated by a newline (\n). Once a newline is detected, the received data is passed to the provided callback function.

Disconnecting

The connection to the serial port is automatically closed when the port is disconnected. However, you can also manually close the connection:

duino.serialPort.close();

Error Handling

The package includes basic error handling for connection, sending, and receiving data from the serial port. Errors are logged to the console with detailed error messages.

Example

Here's a complete example demonstrating the usage of DuinoCom:

import DuinoCom from 'duinocom';

const duino = new DuinoCom(9600);

async function main() {
  try {
    await duino.connect();

    // Send data to Arduino
    await duino.send("Hello Arduino!");

    // Listen for incoming data
    duino.listen((data) => {
      console.log("Received data:", data);
    });
  } catch (error) {
    console.error("An error occurred:", error);
  }
}

// Trigger main function on button click
document.getElementById("startButton").addEventListener("click", main);

Note: To run the example, add a button with the id "startButton" to your HTML and execute the script in a web browser environment.

Experimental

Please note that this package is still experimental and should be used with caution.

License

This package is licensed under the ISC License. See the LICENSE file for more details.

Repository

The source code for DuinoCom is hosted on GitHub. Feel free to open issues or contribute to the project!