pure javascript serial port implementation for node.js, electron and nw.js.
npm i --save serialport-js
const serialjs = require('serialport-js');
const init = async () => {
const delimiter = '\n';
const ports = await serialjs.find();
if (ports.length) {
let port = await serialjs.open(ports[0].port, delimiter);
port.on('data', (data) => {
console.log(data);
});
port.on('error', (error) => {
console.error(error);
});
port.send('foo bar');
}
};
init();
Type: Promise<Object[]|Error>
Async function that returns a promise. When resolved it contains a list of the registered serial devices.
Id: String
Type: Promise<Object|Error>
Async function that returns a promise.
When resolved it contains the found serial device or null
when
not found.
Type: Promise<EventEmitter|Error>
Opens a Duplex connection to the serial device. Returns the Port(event.EventEmitter) Object
Type: String
The data that has been read out of the serial connection.
The error that occured.
Is emitted when the connection is closed.
Sends the data to the serial device.
Closes the connection to the serial device.
When closed, the event closed
is emitted.
- isOpen
- serialPort
#node-webkit, nw.js, and seperate node.js thread examples *The only difference with this is that the user must have node installed. It will spawn a node proxy using their local node version and run the pure js serialport implementation. This is great for consumer facing products as there is no need for compilers or dev tools to install the module with your app, users just need node.
var serialjs=require('serialport-js').node(); //thats the only difference
//the rest of the implementation is exactly the same.
serialjs.find(serialDevicesPopulated);
function serialDevicesPopulated(ports){
//ports arg is a refrence to serialjs.ports
console.log(
ports
);
if(!ports[0])
return;
serialjs.open(ports[0].port,start,'\n');
}
function start(port){
port.on(
'data',
gotData
);
//if this doesn't show up the port may need a few milliseconds to open
port.send('howdy doody doo');
}
function gotData(data){
console.log(data);
}