0

i wanna get data from rfid tags using arduino and rfid-rc522 reader i can get tags data through arduino serial monitor but i can't receive the correct data with node.js all i get is the same numbers from all the tags (each tag should have it own number ) . what i get from arduino monitor which is correct :https://i.stack.imgur.com/3ukb7.png

and what i get from node.js : https://i.stack.imgur.com/wCXXN.png

my node.js code is :

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

    serialport.on('data', function(data ){



            console.log(data[0]   );






    });
});;

any help to solve this would be appreciated .

Igor Pejic
  • 3,658
  • 1
  • 14
  • 32

1 Answers1

0

As nodejs is event based, you will get the string slice in block because the 'data' event is triggered every time a byte get received. Use node-serialport parsers to concat data received with previous until data have some 'x' char that denotes end of message.

var sp = new SerialPort("/dev/tty-usbserial1", {
  parser: serialport.parsers.readline("\n") // '\n' can be any character of your choice
});

hope that helps

lazychino
  • 41
  • 3
  • didn't work at all , my code :"var serialport = require("serialport"); var SerialPort = serialport.SerialPort; // localize object constructor var sp = new SerialPort("COM4", { parser: serialport.parsers.readline("\n") }); " – user3810508 Sep 08 '14 at 00:04
  • your arduino code have to send a newline character at the end of the message for the parser to work. Also make sure that GRD between the computer and arduino is connected that gave me headaches (if your arduino is connected via USB that is not the problem) – lazychino Sep 10 '14 at 02:43