3

From the arduino documentation, the Serial.read() function is supposed to return a single character at a time from incoming serial messages. So if I program the arduino with the following:

#include "SPI.h"
void setup() {
 Serial.begin(115200);
}
void loop() {
 if (Serial.available() > 0) {
 char data1 = Serial.read();
 Serial.print(data1);
 }
}

And I open the serial monitor and send "Hello, World!" I would expect the received message to be the individual characters printed one at time but instead I get back the whole message. So what gives? What am I not understanding?

asked Oct 5, 2016 at 21:07
4
  • 1
    ... That you're printing characters until no more are available. Commented Oct 5, 2016 at 21:16
  • But shouldn't they be printed one at a time? Commented Oct 5, 2016 at 21:18
  • They are being printed one at a time. But how fast can you read? Commented Oct 5, 2016 at 21:19
  • Oh I see. I stuck a delay() in there and It makes more sense now. I am not that smart, apparently. Commented Oct 5, 2016 at 21:25

1 Answer 1

1

You only think it is a string, it actually returns character by character. You can know by adding something like a delay after you Serial.print(). What the program actually does is that it receives one character each time you type one and it prints it, then the void loop repeats.

answered Oct 5, 2016 at 21:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.