gpt4 book ai didi

node.js - nodejs serialport 模块或 bluetooth-serial-port 模块,哪个?

转载 作者:搜寻专家 更新时间:2023-10-31 23:10:07 25 4
gpt4 key购买 nike

我有几个用于串行通信的 nodejs 示例。一个例子是使用串口模块(如下)。我有一个配对的蓝牙设备,它设置为 rfcomm0。我可以通过命令行使用 echo data >/dev/rfcomm0 与它通信并收到响应,所以它似乎可以工作。问题是它不能通过 nodejs 工作。下面的示例在我执行 nodejs SerialToJson.js/dev/rfcomm0 时抛出“无法加载绑定(bind)文件”错误。另一种方法是改用蓝牙串口模块,但也不能通过 npm 安装,因为找不到与我正在使用的 Node 版本兼容的版本。我知道如何解决每个问题,但我不知道该追求哪个,串行端口模块可以与 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 模块也适用于我。

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

因此,如果您的应用程序/模块只需要与蓝牙设备连接并且您想要扫描功能,那么至少值得尝试一下 bluetooth-serial-port。

npm 模块/readme 中有几个示例,因此仅测试它不会花费太多时间。

编辑:

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

关于node.js - nodejs serialport 模块或 bluetooth-serial-port 模块,哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com