Serial Communication garbage while reading a char

Hello,
I'm trying to understand how the serial communication btw computer and Arduino (mega) is actually working and I have some difficulties.
I thought that data on serial was collected to a buffer on the arduino side, where it was removed when using serial.read()
I wrote a small program to test that.

What I don't understand is that it seems to get some garbage caracters in the buffer
Could someone with a little more knowledge than me explain what is going on?
Thanks.

PS: the ultimate purpose would be to read the first char in the buffer, do some treatment, and then pass the remaining part of the buffer to the grind of an existing library (serialcommands, to name it)

PPs: the serial bauds are the same on both sides of communication... and I used both 9600 and 57600 bps for test. The garbage differ but still exists.

uint8_t toto=5;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  delay(2000);                     //wait as it seems that garbage could come from serial init
  Serial.println(toto);         //print some value, no garbage here
}

void loop() {

    char * buff;int i=0;
    char * caractere;
    buff="";
    delay(500);
    while (Serial.available()>0)
    {

        Serial.println(i);                     //print the loop number
        caractere = Serial.read();     //read one char
        Serial.print(caractere);        //print that char, that's where garbage seems to come
            buff[i]=caractere;
            i++;
    }
    if (strlen(buff)>0)
    {Serial.print(strlen(buff));     //seems to never happen
    }
}

please don't post images of Text. It's bad for the climate change to move all those bytes around and those of us with poor eye-sight can't read it easily...

(read How to get the best out of this forum )


read about cStrings. You don't have memory allocated for your buffer. Seems you also need to read about pointers...

Bad move trying to store into a string literal. Especially a string literal of length 0!

Change to:

    char buff[200];

    buff[i++]=caractere;
    buff[i] = '\0';  // Null terminator.  Without it, strlen() can't give you a correct answer.

the code is copying a character from the serial input and setting a pointer to its value.

the Serial.print() is using the character value as an address of a string in memory.

i believe you want to Serial.print buff after receiving the last character and appending a '\0'

Oh, god. That's even worse than writing into a string literal!

hence the recommendation

Thanks,
OK, it seems I haven't understand the pointers in C/C++, and I thought I had it...

always time to learn

as you seem to speak French, two tutorials I wrote could help:

Merci/thanks. I'll read it. I'm mixing the adress/value of the pointers, I really don't understand the * marking yet.

So here is a new endeavour:

uint8_t toto=5;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  delay(2000);
  Serial.println(toto);
}

void loop() {

    char buff[200];int i=0;
    char* caractere;  //memory address of a char
    //buff="";
    delay(500);
    while (Serial.available()>0)
    {
        Serial.print("Boucle [");
        Serial.print(i);              //writes the loop number
        Serial.println("]");
        *caractere = Serial.read();
        Serial.println(*caractere);   //writes the value pointed by caractere
            buff[i] = *caractere;
            i++;
    }
    buff[i]='\0';                     //writes an end of Cstring (repeatedly)
    if (strlen(buff)>0)
    {Serial.print(strlen(buff));Serial.println(buff);
    }
}

![image|286x251](upload://bmmwdr9QYxpNKRO4RS4Qc2EeG1C.png)

Is the size defined for buff[xx] only a memory allocation?

The execution trace seems to be correct
image

that's a pointer but .... you just want a char to store the byte that comes from the Serial line.

    char caractere;  //memory for one char
  ...
   caractere = Serial.read();

go to the preferences and activate all the compilation warnings. You'll get some hints...

Regarding the pointer/variable difference, I noticed that very often the libraries are using pointers (char * something) to pass values around (especially for function parameters)

Is there a particular interest for this? Shouldn't I do the same?

how about


    while (Serial.available())
    {
        buff[i] = Serial.read();
        if ('\n' == buff [i])  {
            buff[i] = '\0';
            i = 0;
            Serial.print (buff);
        }
        else
            i++;
    }

thanks,
If I understand well, the if executes only when receptionning the end of line from serial port, write the end of string to buff, and writes buff to serial

Thanks, I'll try it

yes, it recognizes when a complete line of input is received