Arduino串行端口提供不良的数据?

我试图从networking(Socket.IO)从我的Arduino接收数据。 所以我会解释下面的代码。

Arduino的:

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

这将伏特转换成温度。 当我打开串行显示器时,我可以看到输出结果如何。

 228 28 28 28 28 29 28 

但是我在Node中创build了一个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 /string。

确保你的波特率是9600是最安全的。 var sp = new SerialPort(comPort,{parser:serialport.parsers.readline(“\ r”),baudrate:9600,});

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

我不使用io.socket例程,你可以看看我的git中的一个工作示例与Arduino和节点代码 。