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

我有几个nodejs串行通信的例子。 一个例子是使用串口模块(下面)。 我有一个配对的蓝牙设备,它被设置为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); }); }); 

很高兴知道它的工作。 串口模块也适用于我。

使用模块串口,需要另一个模块与蓝牙设备连接,或者需要在terminal上手动连接rfcomm。 在function上的巨大差异是蓝牙串口不需要你连接rfcomm。 该模块可以扫描蓝牙设备并与之连接。 连接后,它具有与串口相同的function。

所以如果你的应用程序/模块只需要连接蓝牙设备,并且你想扫描function,那么至less应该试一下蓝牙串口。

在npm module / readme中有几个例子,所以不需要太多的时间来testing它。

编辑:

有一个新版本发布,非常稳定! :D https://npmjs.org/package/bluetooth-serial-port