Node.js – nodejs serialport module or bluetooth-serial-port module, which one

bluetoothnode.jsserial-port

I have a couple of nodejs examples for serial communication. One example is using the serialport module (below). I have a paired bluetooth device which is set up as rfcomm0. I can communicate with it over the command line with echo data > /dev/rfcomm0 and receive a response, so it seems to work. The problem is that it doesn't work through nodejs. The example below throws a "could not load bindings file" error when I do nodejs SerialToJson.js /dev/rfcomm0. The alternative is to use the Bluetooth-serial-port module instead but that too cannot be installed through npm because a compatible version cannot be found for the version of node I'm using. I have an idea of how to troubleshoot each problem but I don't know which to pursue, can the serialport module be used with rfcomm (serial port emulation) or is the Bluetooth-serial-port module better suited?

    /*
    SerialToJson.js
    a node.js app to read serial strings, convert them to
    JSON objects, and send them to webSocket clients
    requires:
        * node.js (http://nodejs.org/)
        * express.js (http://expressjs.com/)
        * socket.io (http://socket.io/#how-to-use)
        * serialport.js (https://github.com/voodootikigod/node-serialport)

    To call it type:
        node SerialToJSON.js portname

    where portname is the path to the serial port you want to open.

    created 1 Nov 2012
    modified 7 Nov 2012
    by Tom Igoe

*/

var serialport = require("serialport"),             // include the serialport library
    SerialPort  = serialport.SerialPort,            // make a local instance of serial
    app = require('express')(),                     // start Express framework
    server = require('http').createServer(app),     // start an HTTP server
    io = require('socket.io').listen(server);       // filter the server using socket.io

var portName = process.argv[2];                     // third word of the command line should be serial port name
console.log("opening serial port: " + portName);    // print out the port you're listening on

server.listen(8080);                                // listen for incoming requests on the server
console.log("Listening for new clients on port 8080");
var connected = false;

// open the serial port. Change the name to the name of your port, just like in Processing and Arduino:
var myPort = new SerialPort(portName, { 
    // look for return and newline at the end of each data packet:
    parser: serialport.parsers.readline("\r\n") 
});

// respond to web GET requests with the index.html page:
app.get('/', function (request, response) {
  response.sendfile(__dirname + '/index.html');
});


// listen for new socket.io connections:
io.sockets.on('connection', function (socket) {
    // if the client connects:
    if (!connected) {
        // clear out any old data from the serial bufffer:
        myPort.flush();
        // send a byte to the serial port to ask for data:
        myPort.write('c');
        console.log('user connected');
        connected = true;
    }

    // if the client disconnects:
    socket.on('disconnect', function () {
        myPort.write('x');
        console.log('user disconnected');
        connected = false;
    });

    // listen for new serial data:  
    myPort.on('data', function (data) {
        // Convert the string into a JSON object:
        var serialData = JSON.parse(data);
        // for debugging, you should see this in the terminal window:
        console.log(data);
        // send a serial event to the web client with the data:
        socket.emit('serialEvent', serialData);
    });
});

Best Answer

Good to know it's working. Serialport module works for me too.

With module serialport, you need another module to connect with the bluetooth device, or you need to manually connect with rfcomm in terminal. The big difference in functionality is that bluetooth-serial-port doesn't require you to connect with rfcomm. This module can scan bluetooth devices and connect with them. After you connect, it has the same functionality as serialport.

So if your application/module only needs to connect with bluetooth devices and you want scanning functionality, it's worth to at least try bluetooth-serial-port.

There are a few examples in the npm module/readme, so it won't take much time to just test it.

EDIT:

There is a new version released, which is very stable! :D https://npmjs.org/package/bluetooth-serial-port

Related Topic