I'm trying to communicate with my Arduiono UNO via USB serial port, more precisely I'm trying to read a string like 125,0, I know it's full of article out there about this, but belive me I've tryied more or less everything, now I got a NODEJS server on a Raspberry Pi that looks like:
var express = require('express');
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server);
var SerialPort = require("serialport")
var serialPort = new SerialPort("/dev/ttyACM1", { baudrate: 115200 });
server.listen(8080);
app.use(express.static('public'));
var brightness1 = 0;
var brightness2 = 0;
var string;
io.sockets.on('connection', function (socket) {
socket.on('led', function (data) {
brightness1 = data.value1;
brightness2 = data.value2;
string = brightness1 +","+brightness2;
var buf = new Buffer(1);
buf.write(string);
serialPort.write(buf);
console.log(buf);
console.log(string);
io.sockets.emit('led', {value1: brightness1, value2: brightness2});
});
So based on that, I'm sending a string over the serial and the console log showed above gives me that:
<Buffer 32> //console.log(buf);
23,0 //console.log(string);
Wich I think it should be ok because I'm sending what I need a 32 and a 0, also if i don't know what it means the
Arduino side I used the sketch provided here and retouch it a bit based on my needs:
void setup()
{
Serial.begin(115200);
}
void loop()
{
while(!Serial.available()); //wait until a byte was received
Serial.println(Serial.read());
int a = Serial.parseInt();
int b = Serial.parseInt();
Serial.println(a);
Serial.println(b);
//analogWrite(3, a);
}
but the result of my Serial.print() are really incongruents, something like: 5--2224, which is totally nosense, my question so is WHY? Where I'm getting wrong? I read a lot around, am I missing something?
3 Answers 3
This is a common (and my favorite) approach:
- Choose an "end of message" character. CR or LF can be good choices, especially if you want to test your program with a terminal emulator.
- On the Arduino, buffer all the received bytes until you see the end of message.
- When you see the end of message, handle the buffer to a function that will parse and process it.
I would avoid parseInt()
for anything but the simplest tests, because
it is both blocking and sensitive to the timing of the input.
For an example, you can take a look at this
Simple Arduino command line
interpreter
(look at loop()
). It's non blocking, so you can add background
processing to perform while waiting for input characters.
I fully agree here with edgar Bonet.
I only want to add a link to a library I have written that does a line by line (based on end message CR and/or LF)
SerialStringReader
This library contains examples.
Note that for many reasons the library uses null terminated strinsg (also known as char arrays) and not the Arduino String class
How to correctly read a string from Arduino serial port?
You can use the function Serial.readString()
. It will return a string that you typed from the serial monitor.
-
No, really. This way you will only get what is in the buffer now. If you read faster than the other end writes you will get just a pieceFrancescoMM– FrancescoMM2018年09月08日 08:13:32 +00:00Commented Sep 8, 2018 at 8:13
<Buffer 32>
.