Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs串口模块还是蓝牙串口模块,哪一个?_Node.js_Bluetooth_Serial Port - Fatal编程技术网

Node.js nodejs串口模块还是蓝牙串口模块,哪一个?

Node.js nodejs串口模块还是蓝牙串口模块,哪一个?,node.js,bluetooth,serial-port,Node.js,Bluetooth,Serial Port,我有几个用于串行通信的nodejs示例。一个示例是使用serialport模块(如下所示)。我有一个配对的蓝牙设备,设置为rfcomm0。我可以使用echo data>/dev/rfcomm0通过命令行与它通信并接收响应,因此它似乎可以工作。问题是它不能通过nodejs工作。下面的示例在执行nodejs SerialToJson.js/dev/rfcomm0操作时抛出“无法加载绑定文件”错误。另一种方法是使用蓝牙串行端口模块,但也不能通过npm安装,因为找不到与我使用的节点版本兼容的版本。我知道

我有几个用于串行通信的nodejs示例。一个示例是使用serialport模块(如下所示)。我有一个配对的蓝牙设备,设置为rfcomm0。我可以使用
echo data>/dev/rfcomm0
通过命令行与它通信并接收响应,因此它似乎可以工作。问题是它不能通过nodejs工作。下面的示例在执行nodejs SerialToJson.js/dev/rfcomm0操作时抛出“无法加载绑定文件”错误。另一种方法是使用蓝牙串行端口模块,但也不能通过npm安装,因为找不到与我使用的节点版本兼容的版本。我知道如何解决每个问题,但我不知道该解决哪一个问题,串行端口模块可以与rfcomm(串行端口仿真)一起使用,还是蓝牙串行端口模块更适合

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

很高兴知道它起作用了。Serialport模块也适用于我

使用模块serialport,您需要另一个模块来连接蓝牙设备,或者您需要手动连接终端中的rfcomm。 功能上的最大区别在于蓝牙串行端口不需要您连接rfcomm。该模块可以扫描蓝牙设备并与之连接。连接后,它具有与serialport相同的功能

因此,如果您的应用程序/模块只需要连接蓝牙设备,并且您需要扫描功能,那么至少值得尝试蓝牙串行端口

npm模块/自述文件中有一些示例,因此不需要花太多时间来测试它

编辑:

发布了一个新版本,非常稳定!:D

我也在做这个。目前我正在使用蓝牙串行端口,它适合我。我已经有了节点版本0.8.19。我会在一周/几天后给你回电。“你有什么进展吗?”埃里克斯梅肯很抱歉回复得太晚了。我编译了最新的NodeJ,常规的serialport模块现在可以工作了。仍然不知道蓝牙串行端口模块是否有任何优势,因此我们仍然希望您提供任何建议,但常规串行端口模块到目前为止似乎可以正常工作。