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

virtual-serialport

v4.0.1

Published

A drop-in virtual replacement for node-serialport's SerialPort object

Downloads

160

Readme

virtual-serialport

Do you use node-serialport, but don't have your device connected for development or testing?

virtual-serialport provides a virtual drop-in replacement for an actual SerialPort object.

Compatibility notes

We'll try to keep up to date with the latest node-serialport stable version. For compatibility with older versions of node-serialport you can install an older version of this library.

Examples

var SerialPort = require('node-serialport').SerialPort;
if (process.env.NODE_ENV == 'development') {
  SerialPort = require('virtual-serialport');
}

var sp = new SerialPort('/dev/ttyUSB0', { baudrate: 57600 }); // still works if NODE_ENV is set to development!

sp.on('open', function (err) {

  sp.on("data", function(data) {
    console.log("From Arduino: " + data);
  });

  if (process.env.NODE_ENV == 'development') {
    sp.on("dataToDevice", function(data) {
      sp.writeToComputer(data + " " + data + "!");
    });
  }

  sp.write("BLOOP"); // "From Arduino: BLOOP BLOOP!"
});

Usage

var VirtualSerialPort = require('virtual-serialport');
var sp = new VirtualSerialPort(path, [opts={}]);

instantiates a virtual SerialPort object. Currently does nothing with the parameters.

var sp = new VirtualSerialPort("/dev/ttyUSB0");
// No device has to actually exist at /dev/ttyUSB0 :)

Computer to device communication

sp.on("data", function(data) {
	console.log("Computer says, " + data);
});

sp.writeToComputer("BLEEP!"); // "Computer says, BLEEP!"

Device to computer communication

sp.on("dataToDevice", function(data) {
	console.log("Arduino says, " + data);
});

sp.write("BLOOP!"); // "Arduino says, BLOOP!"

node-serialport methods/events:

sp.open(callback)

Simulates the port opening. It returns the callback execution and emits the open event on process.nextTick()

Writes data to the virtual device. Equivalent to sp.emit("dataToDevice", data).

sp.write(data)

Writes data to the virtual device. Equivalent to sp.emit("dataToDevice", data).

sp.isOpen()

Returns boolean indicating wether the port is open or not. It returns the value of sp.open which you may set manually.

sp.pause()

This method is for API compatibility. It actually does nothing here.

sp.resume()

This method is for API compatibility. It actually does nothing here.

sp.flush(callback)

This method is for API compatibility. It actually does nothing and returns the callback call.

sp.drain(callback)

This method is for API compatibility. It actually does nothing and returns the callback call.

sp.close(callback)

This method is for API compatibility. It actually does nothing and returns the callback call.

sp.on("open", function(err) { ... } )

Runs function once SerialPort is ready, as you would with an actual SerialPort instance.

sp.on("data", function(data) { ... })

Act on data sent to the computer, as you would with an actual SerialPort instance.

Non node-serialport methods/events:

sp.writeToComputer(data);

Writes data to computer. Equivalent to sp.emit("data", data)

sp.on("dataToDevice", function(data) { ... })

Act on data sent to the device.

Parsers

node-serialport is listed as an optional dependency. If some version of node-serialport is installed, virtual-serialport will load it's parsers.

For more information about parsers, please refer to the specific version of node-serialport docs.

TODO

  • move to automated testing (assertions and more)
  • better match voodootikigod's node-serialport api