serialport-js
v1.1.0
Published
Serialport js - node.js serial duplex connection.
Downloads
19
Maintainers
Readme
serialport-js
pure javascript serial port implementation for node.js, electron and nw.js.
Install
npm i --save serialport-js
Usage
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();
Methods
find()
Type: Promise<Object[]|Error>
Async function that returns a promise. When resolved it contains a list of the registered serial devices.
findById(id)
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.
open(path, delimiter = '\r\n')
Type: Promise<EventEmitter|Error>
Opens a Duplex connection to the serial device. Returns the Port(event.EventEmitter) Object
Port Events
data
Type: String
The data that has been read out of the serial connection.
error
The error that occured.
closed
Is emitted when the connection is closed.
Port Methods
port.send(data)
Sends the data to the serial device.
port.close();
Closes the connection to the serial device.
When closed, the event closed
is emitted.
Port Variables
- 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);
}