1

I am trying to communication between Arduino and Nodejs. But problem is when I communicate between I got correct reading as well as some garbage reading in between correct reading. Still cant understand how to resolve this problem ?

Here is the Nodejs part which is used to read data from COM port

      var SerialPort = require("serialport").SerialPort;
      var serialport = new SerialPort("COM23",{baudrate:9600});
      serialport.on('open', function(){

           serialport.on('data', function(data){
           console.log(data[0]);
           });
      });

Here is my simple Arduino code

       int led = 13;
       void setup() {                
           Serial.begin(9600);
           pinMode(led, OUTPUT);     
       }
       void loop() {

           digitalWrite(led, HIGH); 
           Serial.write(1);
           delay(1000);  
           digitalWrite(led, LOW);
           Serial.write(0);
           delay(1000);               
      }

1 Answer 1

2

Limit the size of the data transferred, also define a parser for the serial transmission.

take a look to the serial port documentation https://github.com/voodootikigod/node-serialport#parsers

You have two options "raw" and "readline".

Out of the box, node-serialport provides two parsers one that simply emits the raw buffer as a data event and the other which provides familiar "readline" style parsing. To use the readline parser, you must provide a delimiter as such '\n'

here is an example for node.js.

   var sp = new SerialPort('/dev/tty.usbmodem14111', {
   //sp.parsers.readline('\r')
   //serialport.parsers.raw
       baudrate: 9600,
       parser: serialport.parsers.readline('\n')
   }); 

in the Arduino side, use the Serial function println();

    Serial.println("your message to the node server");

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.