gpt4 book ai didi

javascript - Arduino 串行端口提供错误数据?

转载 作者:行者123 更新时间:2023-11-30 05:38:41 25 4
gpt4 key购买 nike

我正在尝试通过网络 (Socket.IO) 从我的 Arduino 接收数据。所以我将在下面解释代码。

阿杜伊诺:

 int temperatureC = (voltage - 0.5) * 100;  
Serial.print(temperatureC - 2);
Serial.print(" ");

这会将伏特转换为温度。当我打开串行显示器时,我可以看到我想要的输出。

228 
28
28
28
28
29
28

但是我在 Node 中创建了一个 SerialPort,它的输出有点奇怪。我以这种方式接收数据:

serialPort.on("open", function () {
console.log('open');

io.sockets.on('connection', function (socket) {
serialPort.on('data', function(data) {
console.log('data received: ' + data);
socket.emit('temps', { temp: data });
});
});
});

但是输出是:

data received: 28
debug - websocket writing 5:::{"name":"temps","args":[{"temp":50}]}
data received:
debug - websocket writing 5:::{"name":"temps","args":[{"temp":32}]}
data received: 2
debug - websocket writing 5:::{"name":"temps","args":[{"temp":50}]}
data received: 8
debug - websocket writing 5:::{"name":"temps","args":[{"temp":56}]}
data received: 28
debug - websocket writing 5:::{"name":"temps","args":[{"temp":50}]}
data received: 28
debug - websocket writing 5:::{"name":"temps","args":[{"temp":50}]}
data received:

如您所见,输出类似于:

28
2
8
2
8
28

看起来它一直在破坏我的 int/strings。

最佳答案

确保设置了波特率,9600 是最安全的。 var sp = new SerialPort(comPort, { 解析器:serialport.parsers.readline("\r"), 波特率:9600, });

sp.on('data', function (arduinoData) {
// data example
var ArduinoString = arduinoData.toString();
}

我不使用 io.socket 例程,您可以查看我的 git 以获得使用 Arduino and node code 的工作示例.

关于javascript - Arduino 串行端口提供错误数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22107867/

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