4

シリアル通信用の nodejs の例がいくつかあります。1 つの例は、serialport モジュール (下記) の使用です。rfcomm0としてセットアップされたペアリングされたBluetoothデバイスがあります。コマンドラインで通信してecho data > /dev/rfcomm0応答を受け取ることができるので、うまくいっているようです。問題は、nodejs では機能しないことです。以下の例では、実行時に「バインディング ファイルを読み込めませんでした」というエラーがスローされますnodejs SerialToJson.js /dev/rfcomm0。代わりに Bluetooth-serial-port モジュールを使用することもできますが、使用しているノードのバージョンと互換性のあるバージョンが見つからないため、npm からインストールすることはできません。各問題のトラブルシューティング方法はわかっていますが、どちらを追求すればよいかわかりません。serialport モジュールを rfcomm (シリアル ポート エミュレーション) で使用できますか、それとも Bluetooth-serial-port モジュールの方が適していますか?

    /*
    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);
    });
});
4

1 に答える 1

5

それが機能していることを知ってうれしいです。シリアルポートモジュールも私にとってはうまくいきます。

モジュール serialport では、Bluetooth デバイスに接続するために別のモジュールが必要になるか、ターミナルで rfcomm に手動で接続する必要があります。機能上の大きな違いは、bluetooth-serial-port では rfcomm に接続する必要がないことです。このモジュールは、Bluetooth デバイスをスキャンして接続できます。接続後は、シリアルポートと同じ機能を備えています。

したがって、アプリケーション/モジュールが Bluetooth デバイスに接続する必要があるだけで、スキャン機能が必要な場合は、少なくとも bluetooth-serial-port を試す価値があります。

npm モジュール/readme にいくつかの例があるので、テストするだけならそれほど時間はかかりません。

編集:

非常に安定した新しいバージョンがリリースされました!:D https://npmjs.org/package/bluetooth-serial-port

于 2013-04-08T07:21:46.687 に答える