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...
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);
}
}

Is the size defined for buff[xx] only a memory allocation?
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?
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