我正在尝试通过网络(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。