Serial.read() to fill Character Array got garbage

Hi

I´m trying to get data ( char ) from Serial.read(); and store it on a array.
I tried a lot of codes over the forum but with no success, always get garbage or nothing on the LCD.
Lcd, is ok, displayed the test array "arduino" at the correct place.
My arduino mini pro is ok, working fine with same serial device where data are collected and sending data to a 1602 lcd.
Not relevant, I suppose, for this try I used a M55 siemens LCD.

I don't see what is wrong with this code.
my try:

      char serIn[32];
      delay (200);
      int i=0;
      char str[8] ="arduino";
if (Serial.available())
{   
  for(i;i<31;i++)
        {
              serIn[i] = Serial.read();
         }
              
}
 lcd.str_lcd(serIn,4, 3, 1, BLACK, -1);
 lcd.str_lcd(str,5, 4, 1, BLACK, -1);

Any help is welcome.

if (Serial.available())
{   
  for(i;i<31;i++)
        {
              serIn[i] = Serial.read();
         }
              
}

So, Serial.available() tells you that there is one byte available. So, you go and read 31 bytes. Does that seem reasonable?

Hi

My goal is to read 32 char from serial and store them on a array, you say "31 bytes", so my code is wrong.
Data connection from my device are 9600,8,N,1, so what is your recommendation to get it right ?

The Arduino can repeat the Serial.read() instruction far far faster than the data arrives.

If you want 32 chars then the simple solution is

if (Serial.available() >= 32) {

}

If you want a more complex (or more mature) solution look at this demo

...R

for(i;i<31;i++)

31, if you're very lucky.

31, if you're very lucky.

Well, there is an int i = 0; statement a few lines before the (poor) code, so there is not much luck involved.

Hi.

Thanks to all you for help.

The issue was not my code ( poor one ) but M55 LCD lib.
I tested it, the 1st one, and some variations, all working with 1602 lcd and LiquidCrystal, but for some reason, parsing serial data from any device to the arduino and store it on array and displaying the array on the lcd I get garbage all time.
So.. not compatible with wire lib, other issue... etc result : I got no success with it.

arssant:
The issue was not my code ( poor one ) but M55 LCD lib.
I tested it, the 1st one, and some variations, all working with 1602 lcd and LiquidCrystal, but for some reason, parsing serial data from any device to the arduino and store it on array and displaying the array on the lcd I get garbage all time.
So.. not compatible with wire lib, other issue... etc result : I got no success with it.

I can't figure from this if you still have a question or what it is?

...R

Robin2:
I can't figure from this if you still have a question or what it is?

...R

Hi.

No question, just updated my initial post with new test.
As I can´t say solved... don´t know how to change status of my initial request.

arssant:
No question, just updated my initial post with new test.
As I can´t say solved... don´t know how to change status of my initial request.

I don't understand this either.

Your initial Post has not been changed (as far as I can see) and it should not be. Leave it as it is so the rest of the Thread makes sense. If you want to provide an update put it in a new post in this Thread.

You can modify your intial post and change the title if you want to add the word SOLVED to it - but I think you are not yet ready for that.

...R

Saludos.

Prueba este sketch pero necesitas una condicion para reiniciar index y volver a capturar informacion

char serIn[32];
int index;
char str[8] ="arduino";

void setup(){
  Serial.begin(115200);
}

void loop(){
  if(Serial.available()>0 && index<32){
    serIn[index]= (char)Serial.read();
    index++;
    if(index>=32){
      Serial.println(serIn);
      //lcd.str_lcd(serIn,4, 3, 1, BLACK, -1);
      //lcd.str_lcd(str,5, 4, 1, BLACK, -1)
    }
  }
}

max_saeta:
Saludos.

Prueba este sketch pero necesitas una condicion para reiniciar index y volver a capturar informacion

I'm guessing "Saludos" is equivalent to "Hello" but I don't understand the rest. This is the English languae section. There are sections for other languages.

...R