Reputation: 486
I am trying to read the serialport data coming from Arduino to Raspberry PI but it doesn't display anything. I have verified that the data is arriving in the serialport. The same script works fine in windows 7. Also I have tried two different approaches of reading the serial data but none of them work. The node.js version I am using is v0.9.9.
Any help will be greatly appreciated.
var sys = require('sys');
var portName = '/dev/ttyACM0' ;
/////////////////////////////////////////////////
//Approach 1
/////////////////////////////////////////////////
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort(portName, {
baudrate: 9600
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function(data) {
//console.log('data received: ' + data);
sys.puts("here: "+data);
});
serialPort.on('error', function(message) {
console.log('error: ' + message);
});
});
/////////////////////////////////////////////////
//Approach 2
/////////////////////////////////////////////////
var serialport = require("serialport");
var SerialPort = serialport.SerialPort; // localize object constructor
var sp = new SerialPort(portName, {
parser: serialport.parsers.raw
});
sp.on("data", function (data) {
sys.puts("here: "+data);
});
Upvotes: 0
Views: 2481
Reputation: 3070
What kind of data are you sending with your Arduino? The best approach would be to jsonize all your data and parse them once in node.
Here is an example for Arduino I'm using, you can modify it to suit your needs:
void sendJson(){
String json;
json = "{\"accel\":{\"x\":";
json = json + getXYZ(0);
json = json + ",\"y\":";
json = json + getXYZ(1);
json = json + ",\"z\":";
json = json + getXYZ(2);
json = json + "},\"gyro\":{\"yaw\":";
json = json + getYPR(0);
json = json + ",\"pitch\":";
json = json + getYPR(1);
json = json + ",\"roll\":";
json = json + getYPR(2);
json = json + "}}";
Serial.println(json);
}
Then in node, it's quite easy:
serialPort.on("data", function (data) {
json = JSON.parse(data.replace(/ /g,""));
AccelX = json.accel.x;
// and so on.
});
Hope it helps!
Upvotes: 0
Reputation: 1398
You could see if it is a linux permission thing.
Have you installed it on you linux machine?
nmp install serialport
Could you answer a question for me. Why are you using javascript to read from a serial port?
Can't install node-serialport on linux
Upvotes: 1