0

I am sending data over USB Serial to my Uno in a specified format and printing the values after they are read

void loop() {
    if(Serial.available()){
        while(Serial.available()){
            int pos = Serial.readStringUntil(':').toInt();
            Serial.println(pos);
            int r = Serial.readStringUntil(',').toInt();
            Serial.println(r);
            int g = Serial.readStringUntil(',').toInt();
            Serial.println(g);
            int b = Serial.readStringUntil(';').toInt();
            Serial.println(b);
        }
    }
}

This is how the data is being sent

0:255,255,255;1:255,255,255;2:255,255,255;3:255,255,255;4:255,255,255;5:255,255,255;6:255,255,255;7:255,255,255;8:255,255,255;9:255,255,255;10:255,255,255;

format is {position}:{Red value},{Green value},{Blue value};

When I send this string to the device through the serial monitor it prints wrong values

0
255
255
255
1
55 //should be 255
255
255
2
55 //should be 255
255
255
3
55 //should be 255
255
255
4
55 //should be 255
255
255
5
255
255
255
6
255
255
255
7
255
255
255
8
255
255
255
9
255
255
255
10
255
255
255

Why is the data wrong?

Is it just the serial monitor that isnt showing it right but the values are actually right?

  • it probably will not help, but four times parseInt() would do. parsing ends on non-digit character and skips leading non-digit characters – Juraj Jun 27 '18 at 14:48
  • do you send the data as one line? – Juraj Jun 27 '18 at 14:50
  • @Juraj yes one line – tyczj Jun 27 '18 at 14:51
  • @Juraj parseInt actually seems to help I just have to add in some extra logic so it does not keep trying to read after the last number – tyczj Jun 27 '18 at 15:59
  • 1
    I don't think your problem is with your code. I ran it (at 9600, 115200, and 230400 baud) and and got proper-looking results. I used the Arduino built-in terminal in line mode - copy/pasting your sent-data example into the terminal input line and clicking 'Send'. – JRobert Jun 28 '18 at 0:53
0

It looks like you are parsing the incoming data for a string of 4 numbers, pos+r+g+b. To avoid data loss as well as improve speed (if you need it), it may be better to send the data as bytes:

: 0x01 0xFF 0xFF 0xFF : 0x02 0xFF 0xFF 0xFF : 0x03 0xFF 0xFF 0xFF ... etc

This can be done with Arduino serial monitor via a hex to ASCII converter. For example https://www.branah.com/ascii-converter

Type in hex (missing 0x, where 0x3a=':'): 3a01FFFFFF3a02FFFFFF3a03FFFFFF3a04FFFFFFFF

Get ASCII: :ÿÿÿ:ÿÿÿ:ÿÿÿ:ÿÿÿ

Copy this into the Arduino serial monitor

Also, try to do all of the serial prints in one go, don't interleave reading and writing unless you absolutely have to. Sometimes bytes get dropped that way, too.

byte pos, r, g, b;

void setup(){
// serial begin
}

void loop() {
    if(Serial.available()){
        while(Serial.available()){
            Serial.readStringUntil(':');
            pos = Serial.read();
            r   = Serial.read();
            g   = Serial.read();
            b   = Serial.read();
            Serial.write(pos);
            Serial.write(r);
            Serial.write(g);
            Serial.write(b);
         }
    }
}

If you need a more elegant way to do the serial write, you will find plenty of answers already on stack exchange (too many to suggest only one).

  • Technically I dont need the delimiters, I was just using them as a way to know what I was reading by separating the values – tyczj Jun 28 '18 at 12:49
  • @MichaelT, how do you send bytes from Serial Monitor to be read by read()? I don't think the Serial Monitor converts hex to bytes – Juraj Jun 29 '18 at 9:23
  • The Arduino serial monitor allows character input. You can use other programs either to convert byte to char (and copy/paste into the IDE), or do the entire serial reading and writing (like Qt, matlab, mathematica). – MichaelT Jun 29 '18 at 11:32
  • but the question implies Serial Monitor as input and output. your answer is good and true, only add some intro about sending and receiving with a computer program – Juraj Jun 29 '18 at 14:03
  • I have given an example of how to send hex with Arduino serial monitor. If it works for you please accept/upvote. – MichaelT Jun 29 '18 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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