Understanding Serial.read()

Hey guys,

I am trying to send a String via the Serial Monitor to the Arduino and I got stuck in there somehow.

My question is: Is there any difference between creating a list of chars manually or by receiving chars as a Serial input?

As an example:

char str1[] = {"HELLO"};

and

void loop(void) {

 char str[5];
 if (Serial.available()>0){
  int index = 0;

  while (Serial.available()>0){
   char tmp = (char)Serial.read();
   str[index] = tmp;
   index++;
  }
 str[index] = '\0';
 }
}

Now, if I send 'HELLO' (without ' ') via the Serial Monitor to the Arduino, shouldn't 'str1' and 'str' be identically the same?

Is there any difference between both of them memory-wise? I feel like I am missing something in there.

Hopefully someone of you can help me, I would really appreciate that :slightly_frowning_face:

Thank you guys very much,

Tobi

while (Serial.available()>0){How long do you expect that condition to be true?
So why set the array index to zero in the loop?

Have a look at Robin2's serial handling basics thread.

I am trying to send a String via the Serial Monitor to the Arduino

You can not make the Serial Monitor app create an instance of the String class. You can not send an instance of a class via the serial port.

Now, if I send 'HELLO' (without ' ') via the Serial Monitor to the Arduino, shouldn't 'str1' and 'str' be identically the same?

Maybe. We have no idea what str1 is, or what is actually in str, so we can't tell you.

One thing that you don't seem to understand is that the Arduino can read serial data far faster than it can be received. So, when you send "HELLO", nothing will happen for a while. Then, the 'H' will arrive, by itself. The Arduino will read that, and see that there is no more data, so str will contain "H".

Later, the 'E' will arrive. The Arduino will read that, and see that there is no more data, so str will contain "E".

Ditto for the 'L's and the 'O'.

See Robin2's post on serial input basics.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

AWOL:
How long do you expect that condition to be true?
So why set the array index to zero in the loop?

I'm sending a fixed number of characters (for this example just 5).

AWOL:
Have a look at Robin2's serial handling basics thread.

Thank you very much, I will do that! :slight_smile:

TobiW:
I'm sending a fixed number of characters (for this example just 5).

And how long (in milli or microseconds) does it take to transmit one character?
How many times will loop() execute in that time?

Also note that Serial Monitor is probably sending one or two characters of line endings.

If all goes well the two character arrays will contain the same string of characters but they will not be at the same address so a simple comparison of their addresses, like "if (str == str1)", will be false. To compare strings you have to call a function that compares each character in turn: "if (strcmp(str, str1) == 0)".

One thing you're missing is that "hello" is five plus one characters. You need to allocate space for the terminating nul character.

Thank you guys for all your replies, I really appreciate that!

I'm reading into Robin2's advices right now.

Also I didn't think about the timing issues yet.

My main problem is, that I cannot "work" with the serial sent char array. But I can directly send the correct String that I typed in back to the Serial Monitor (Serial.print()). So something must work correctly. However I somehow cannot use the Chars inside the array that I receive.

It is really strange...

TobiW:
However I somehow cannot use the Chars inside the array that I receive.

As you have not posted the program that demonstrates that .......

...R

Thank you so much guys!
According to your Tutorial everything went absolutely how it should.

I think what I was missing was the line ending.
So basically all I did was, I changed the program to add the '\0' to the string if the code detects the newline from the serial input.

Great tutorial! Thank you for that :slight_smile: