Git Product home page Git Product logo

virtual-serialport's Introduction

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.read()

Reads data from the virtual device. (The last data written by sp.writeToComputer(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.

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

Act on when readable data ready to be read by 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) and sp.emit("readable") Also set data next read by sp.read()

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

virtual-serialport's People

Contributors

reyiyo avatar sleexyz avatar matiasdecarli avatar morganrallen avatar roccomuso avatar qdot avatar siburny avatar mikedstrong avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.