virtual-serialport
v4.0.1
Published
A drop-in virtual replacement for node-serialport's SerialPort object
Downloads
149
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