Arduino Mega Serial1 vs SoftwareSerial data reading in loop

Hello,
I heve problem with serial data reading in loop
If I use loop like this

void loop()
{

if (rfid_Serial.available() > 0 )
  {
    for (int i = 0; i < 14; i++)
    {
      byte data =  rfid_Serial.read();
      rfid_data[i] = data;// read and save data
    }
}
}

where rfid_Serial is SowtwareSerial port, everything is working correctly

But If I use Serial1 instead of SoftwareSerial, I recieve only 255 values and sometimes some other value. If I pus delay into the loop, everything works fine.

void loop()
{

if (Serial1.available() > 0 )
  {
    for (int i = 0; i < 14; i++)
    {
      delay(200);
      byte data =  Serial1.read();
      rfid_data[i] = data;// read and save data
    }
}
}

Do You knnow some better way, how to read serial port in loop, without delay?

Thank You

The way you are reading the serial data should be right.

however, it could be possible that the serial port only received 1 bytes of data and you want to read 14, with the delay you give the serial connection enough time to deliver all it's data.

it's better to check if there is data, then read 1 byte, save it to the array, increment an int that holds the array length and then add an if where you check if the array is filled

untested example:

int length = 0;

void loop(){
    if (Serial1.available() > 0 ){
        rfid_data[length] = Serial1.read();
        length ++; // increment length

        if(length == 14){
            // handle data!

            length = 0; // get ready for next package
        }
    }
}

Don’t use delay() or any other form of timing when receiving data.

The examples in serial input basics are simple, reliable, and don’t block other Arduino activitiy.

…R

laurencemommers : That is, what I thougt. But i was supprised, that it works, when I use SoftwareSerial, but it doesn´t when I use hardware serial port. Robin2: It was only example i´m not using delay in my code.

THANK YOU