2

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
  • You sized your buffer with room for one single byte: it only gets the '2' of "23,0". The ASCII code for '2' is 0x32 (in hex). That's the meaning of <Buffer 32>. Oct 23, 2016 at 16:24
  • @Edgar thanks for your reply! So basically my problem is on server side, so I just need to increase the buffer size to 7? Arduino side is all good right?
    – FabioEnne
    Oct 23, 2016 at 16:55
  • There is a problem server side, but then I am not sure how well the Arduino side would perform, especially if you don't control the timing of the sent bytes. Oct 23, 2016 at 17:01

3 Answers 3

2

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.

0

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

0

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.

1
  • 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 piece Sep 8, 2018 at 8:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.