gpt4 book ai didi

node.js - Arduino 无线和 Node.js

转载 作者:搜寻专家 更新时间:2023-11-01 00:09:40 25 4
gpt4 key购买 nike

我正在开发一个 Arduino 项目来控制电机和读取传感器。我决定使用使用 Node.js 作为媒介 channel 的 Web View ,使用任一库(SerialPort 和 SerialPort2)从串行端口读取/写入浏览器。

当我使用电线将 Arduino 直接连接到 USB 设备时,两者都工作正常,但是当我通过我的无线适配器将 Arduino 连接到 USB 设备时,Node.js 似乎无法读取任何内容** ( APC220 )即使我可以使用 Arduino 串行监视器读取它收到的所有内容。

我检查了所有可能的原因;我检查了我用于 Arduino 与无线串口和 APC220 以及桥接器(USB 转串口转换器)通信的波特率。它们都具有相同的设置:9600 波特率,无奇偶校验/流量控制,数据位:8,停止位:1。

行为如下。它毫无问题地连接到 COM port,然后我尝试打印错误,但似乎没有任何一个 SerialPort 库可以识别。然后没有读取事件(数据),这意味着它(Node.js)没有与串行端口交互,即使它是打开的。

注意:我知道我可以使用另一个 Arduino 作为 USB 端口和无线适配器之间的媒介,但我想了解这个问题并干净利落地解决它,而无需此类解决方法。

可能是什么问题?

服务器 [node.js]:

var SerialPort  = require('serialport2').SerialPort;
var portName = 'COM15';

var io = require('socket.io').listen(8000); // Server listens for socket.io communication at port 8000
io.set('log level', 1); // Disables debugging. This is optional. You may remove it if desired.

var sp = new SerialPort(); // Instantiate the serial port.
sp.open(portName, { // portName is instatiated to be COM3, replace as necessary
baudRate: 9600, // This is synchronised to what was set for the Arduino code
dataBits: 8, // This is the default for Arduino serial communication
parity: 'none', // This is the default for Arduino serial communication
stopBits: 1, // This is the default for Arduino serial communication
flowControl: false // This is the default for Arduino serial communication
});

io.sockets.on('connection', function (socket) {
// If socket.io receives message from the client browser then
// this call back will be executed.
socket.on('message', function (msg) {
console.log(msg);
});
// If a web browser disconnects from Socket.IO then this callback is called.
socket.on('disconnect', function () {
console.log('disconnected');
});
});

var cleanData = ''; // This stores the clean data
var readData = ''; // This stores the buffer
sp.on('data', function (data) { // Call back when data is received
readData = data.toString(); // Append data to buffer.
// If the letters '[' and ']' are found on the buffer then isolate what's in the middle
// as clean data. Then clear the buffer.
console.log(readData); // **Here you should be able to print the data if you receive any**
if (readData.indexOf(']') >= 0 && readData.indexOf('[') >= 0) {
cleanData = readData.substring(readData.indexOf('[') + 1, readData.indexOf(']'));
readData = '';
console.log("-- "+cleanData);
io.sockets.emit('message', cleanData);
}else if(readData.indexOf('[') >= 0){
cleanData = readData.substring(readData.indexOf('[') + 1, readData.length);
readData = '';
}else if(readData.indexOf(']') >= 0){
cleanData += readData.substring(0, readData.indexOf(']'));
readData = '';
console.log("-- "+cleanData);
io.sockets.emit('message', cleanData);
}else{
cleanData += readData;
readData = '';
}
//console.log(readData);
//io.sockets.emit('message', readData);
});

最佳答案

当监视器运行时,没有其他程序可以读取串口。

如果您不同时打开两者,那么事情就比较棘手了。我的建议是监视电线。即:安装 Wireshark并查看串行连接/USB 总线上的数据。

您可能还想检查 APC220 和 Arduino 的串行端口在串行/USB 转换器方面有何不同。另一个想法是在 Linux 下分析这个问题,因为可以更深入地了解芯片组/USB 事件的低级差异。当然,如果您没有 Linux 经验,这很难做到,但也许您认识一些 Linux 爱好者。

关于node.js - Arduino 无线和 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17059981/

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