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

serialmessages

v0.0.3

Published

Handles variable length messages over UART

Downloads

3

Readme

SerialMessages

SerialMessages is a module that sits between node and a serial port in order to asynchronously and synchronously respond to messages over UART

Installation

Type the following into your commandline if you have npm installed

npm install serialmessages

Usage

This is how you instantiate up the serial message handler

var messageHandler = require("serialmessages")("/dev/tty.usbserial-FTE6C8SO");
messageHandler.serialPort.on("data", function(){});
var Q = require("q"); // (optional) for synchronous messages

To create message handlers:

// Add message with ID 0x61 (ASCII 'a'). 
var testHandler = messageHandler.addHandler({
  id:       0x61,
  name:     "'a' handler", 
  length:   4, 
});

var testHandler2 = messageHandler.addHandler({
  id:       0x62,
  name:     "'b' handler", 
  length:   5, 
});

To perform asynchronous handling:

testHandler1.on("message", function(event) {
    console.log("Received message 'a' asynchronously with payload "+event.data);    
});
testHandler2.on("message", function(event) {
    console.log("Received message 'b' asynchronously with payload "+event.data);    
});

To perform synchronous handling:

Q.when(messageHandler.open())
.then(testHandler.receive())
.then(function(event) {
    console.log("Received handler a synchronously: "+event.data);
})
.catch(function(error) {
    console.log("ERROR! "+error);
})
.finally(function() {
    console.log("Done.");
});

example.js

Here is a larger example, with simultaneous asynchronous/synchronous handlers:

#!/usr/bin/env node
"use strict";
var Q = require("q");

// Open a new message handler
var serialIdentifier = process.argv[2] || "/dev/tty.usbserial-A7027DGF";
var messageHandler = require("serialmessages")(serialIdentifier);

// echo all received characters back to command line
messageHandler.serialPort.on("data", echo(messageHandler.serialPort));

// Add message with ID 0x61 (ASCII 'a'). 
var testHandler = messageHandler.addHandler({
  id:       ascii('a'),
  name:     "'a' handler", 
  length:   4, 
}).on("message", logMessageHandler).on("message", sendNewLine);

// Add message with ID 0x62 (ASCII 'b')
var testHandler2 = messageHandler.addHandler({
  id:       ascii('b'), 
  name:     "'b' handler",
  length:   5, 
}).on("message", logMessageHandler).on("message", sendNewLine);

// Add message with ID 0x65 (ascii 'e')
var testHandler3 = messageHandler.addHandler({
  id:       ascii('e'), 
  name:     "exit handler",
  length:   3, 
}).on("message", sendNewLine);

// Add to message handler, if the payload is "xit", then send a newline, then
// exit the program.
testHandler3.on("message", function(event) {
  if (event.data.toString() === "xit") {
    Q.fcall(messageHandler.sendMessage("Exiting...\r\n"))
    .delay(10)
    .then(process.exit);

  }
});




//Example sequence of events:
//1. ensure message handler is open
//2. send "test" message
//3. report how many characters were sent 
//4. receive 'a' message
//5. console log a message back to user
//6. receive 'b' message.
//7. console log b message back to user
//8. log "finished"

Q.when(messageHandler.open())                                                //1
  .then(messageHandler.sendMessage("test\r\n"))                              //2
  .then(function handleSentMessage(messageLength) {                          //3
    console.log("sent message which was "+messageLength+" characters long.");
  })
  .then(testHandler.receive())                                               //4
  .then(function handleFirstMessage(event) {                                 //5
    console.log("Finally received expected message 1: "+event.data);               
  })
  .then(testHandler2.receive())                                              //6
  .then(function handleSecondMessage(event) {                                //7
    console.log("Finally received expected message 2: "+event.data);            
  })  
  .finally(function logFinished() {                                          //8
    console.log("Finished!");
  })
  .catch(function(error){console.log(error);});


/*************************** Misc. Functions Below ***************************/

/**
 * Log received messages to command line, and notify user via bell.
 * @param  {Buffer} event Event received over serial
 */
function logMessageHandler(event) {
  console.log("(" + event.messageHandler.name + ": " + event.data + ")\u0007");
}

/**
 * Used to echo received data back over serial.
 * @param  {Buffer} serialPort SerialPort to echo to
 */
function echo(serialPort) {
  /**
   * Echo to serial port
   * @param  {Buffer} data Data to send over port
   */
  return function echoToPort(data) {
    var i;
    for (i = 0; i < data.length; i += 1) {
      var char = data[i];
      if (char === "\r".charCodeAt(0)) {
        serialPort.write("\r\n", function(){});
      } else if (char === 0x7f) {
        serialPort.write("\b \b", function(){});
      } else {
        serialPort.write(String.fromCharCode(char),function(){});
      }
    }
  };
}
/**
 * Send New Line
 * Sends a line break over serial, for debugging purposes
 * @return {Promise} Promise to send new newline character
 */
function sendNewLine() {
  return Q.fcall(messageHandler.sendMessage("\r\n")); 
}

function ascii (char){
  return char.charCodeAt(0);
}

####Example Output example.js output

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

0.0.1 - Added

License

WTFPL Licensed by WTFPL