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

serial-node

v0.0.11

Published

Serial Port using Node.js

Downloads

19

Readme

Serial-node

Serial-node is a module for Node.js to control serial ports. (For now only for windows, soon to Linux and MacOS.)

Installation

Via NPM:

npm install serial-node 
https://www.npmjs.com/package/serial-node

Functions

list()

This function list the available ports on your computer, returns an array of ports.

Example:

const SerialPort = require('serial-node');
const serialList = new SerialPort().list();

serialList.forEach(console.log); 

Note: if there is no available port, the returned array is equal 0.

use(port,values)

This function is to set up and use a port. The parameter 'port' is required and is the port name.

Example:

const SerialPort = require('serial-node');
const serial = new SerialPort();
serial.use('COM3');
values (optional)

The parameter 'values' is to set the parameters of a serial port.

  • baud, defaults to 9600. Must be a number you specify.
  • databits, defaults to 8. Must be one of: 8, 7, 6, or 5.
  • parity, defaults to none. Must be one of: none, even, mark, odd, or space.
  • stopbits, defaults to 1. Must be one of: 1,1.5, or 2.
  • timeout, defaults to off. Must be one of: on or off.
  • xon, defaults to off. Must be one of: on or off.
  • odsr, defaults to off. Must be one of: on or off.
  • octs, defaults to off. Must be one of: on or off.
  • dtr, defaults to off. Must be one of: on or off.
  • rts, defaults to on. Must be one of: on or off.
  • idsr, defaults to off. Must be one of: on or off.
  • callbackUse , function callback when Use is completed.
  • callbackWrite , function callback when Write is completed.
  • callbackRead , function callback when Read is completed.
  • callbackList, function callback when List is completed.

write(value)

This function is to write the serial port.

Example:

const SerialPort= require('serial-node');
const serial = new SerialPort();
serial.use('COM3');
serial.write('hi!');

Note: encoding is ASCII.

read([looping])

This function is to read the serial port, returns the value(split by '\n' or '\0' of '\r').

Example 1:

const SerialPort= require('serial-node');
const serial = new SerialPort();
serial.use('COM3'); 
const read = serial.read();
console.log(read);

Example 2: (Using looping feature) When the lopping parameter is true the callbackUse is called until the stop function is called.

const SerialPort= require('serial-node');
const serial = new SerialPort();

serial.use('COM3', { 
  callbackRead: (args) => {
    const read = args.value.trim() || '';
    console.log(read);
   }
 }
);

serial.read(true); // reading serial in looping

stop()

This function is to stop reading if was set to looping

Example:

const SerialPort= require('serial-node');
const serial = new SerialPort();

serial.use('COM3', { 
  callbackRead: (args) => {
    const read = args.value.trim() || '';
    console.log(read);
   }
 }
);

serial.read(true); // reading serial in looping

// wait for 5 seconds to stop reading
setTimeout(() => {
    serial.stop();
}, 5000);

Events

Please, take a look into callbacks on Use function options. Example:


const SerialPort= require('serial-node');
const serial = new SerialPort();

// setting use with config arguments and callback functions
serial.use('COM3', {
    baud: '115200',
    callbackUse: (args) => {
        if (args.state) {
            // reading serial in looping
            serial.read(true);
 
            // wait for 5 seconds to stop reading
            setTimeout(() => {
                serial.stop();
            }, 5000);
        }
    },
    callbackRead: (args) => {
        // my rules to accept data when the serial data was like: @6:0!
        var read = args.value.trim() || '';
        if (read.startsWith('@') && read.endsWith('!') && read.length == 5) {
            // show the useful data
            console.log(read);
        }
    },
    callbackWrite: (args) => {
        // nothing todo for now
    },
    callbackList: (args) => {
        // nothing todo for now
    }
});