A drop-in virtual replacement for node-serialport's SerialPort object
npm install virtual-serialportDo 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.
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.
``javascript
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!"
});
`
`javascript`
var VirtualSerialPort = require('virtual-serialport');
var sp = new VirtualSerialPort(path, [opts={}]);
instantiates a virtual SerialPort object. Currently does nothing with the parameters.
`javascript`
var sp = new VirtualSerialPort("/dev/ttyUSB0");
// No device has to actually exist at /dev/ttyUSB0 :)
`javascript
sp.on("data", function(data) {
console.log("Computer says, " + data);
});
sp.writeToComputer("BLEEP!"); // "Computer says, BLEEP!"
`
`javascript
sp.on("dataToDevice", function(data) {
console.log("Arduino says, " + data);
});
sp.write("BLOOP!"); // "Arduino says, BLOOP!"
`
#### sp.open(callback)
Simulates the port opening. It returns the callback execution and emits the open event onprocess.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.
#### 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.
node-serialport is listed as an optional dependency. If some version of node-serialport isvirtual-serialport
installed, will load it's parsers.
For more information about parsers, please refer to the specific version of node-serialport` docs.