2014-02-28 4 views
2

내 Arduino에서 웹 (Socket.IO)에서 데이터를 받으려고합니다. 그래서 아래 코드를 설명 할 것입니다.Arduino 직렬 포트가 잘못된 데이터를 제공합니까?

의 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: 

당신이 출력을 볼 수 있듯이이 같은 것입니다 : 그것의 내 INT을 깨는처럼

28 
2 
8 
2 
8 
28 

가 보이는/항상 문자열.

답변

1

전송 속도가 설정되어 있는지 확인하십시오. 9600이 가장 안전합니다. var sp = 새 SerialPort (comPort, { 파서 : serialport.parsers.readline ("\ r"), 전송 속도 : 9600, });

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

나는 io.socket 루틴을 사용하지 않는, 당신은 Arduino and node code 와 작업, 예를 들어 내 자식 볼 수 있습니다.

+0

안녕하세요! 도움을 주셔서 감사합니다 그것은 내 문제를 해결하지 않았지만 당신은 올바른 방향으로 나를 잡았고 그래서 나는 당신의 대답을 받아 들일 것입니다. 내 결과 : http://github.com/Nathansakoetoe/Arduino-Temperature/blob/master/arduino.js – Nathan

+0

도움이 될 수있어서 기쁘게 생각합니다. 여러 가지 방법으로 여러 가지 관점에서 도움을 얻을 수 있습니다. – alexmac

관련 문제