RFID Question

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if(Serial.available())
  {
    int index = 0;
    char rfidArray[15];
    while(Serial.available())
    {
      rfidArray[index] = Serial.read();
      index++;
    }
    Serial.println(rfidArray);
    
    for(int x = 0 ; x < 15 ; x++)
    {
      rfidArray[x] = 0;
    }
  }
}

I'm having a problem with that code. For some reason, every time the board resets, the first RFID read is a garbage string. I'd paste the output, but apparently that's not possible from the serial monitor.

All other reads are good, until I reset it again. Then, another garbage string before good results.

Is there something I can fix to prevent this, or do I need to code around it?

int index = 0;
char rfidArray[15];
void loop()
{
  if(Serial.available())
  {
    index = 0;
    while(Serial.available())
    {
      rfidArray[index] = Serial.read();
      index++;
    }
    Serial.println(rfidArray);
  }
  for(int x = 0 ; x < 15 ; x++)
    {
      rfidArray[x] = 0;
    }
}
char rfidArray[15];

void setup()
{
  Serial.begin(9600);
  
  clearRFID();
}
void loop()
{
  if(Serial.available())
  {
    clearRFID();
        
    int index = 0;
    while(Serial.available())
    {
      rfidArray[index] = Serial.read();
      index++;
    }
    Serial.println(rfidArray);
    
    clearRFID();
  }
}

void clearRFID()
{
  for(int x = 0 ; x < 15 ; x++)
  {
    rfidArray[x] = 0;
  }
}

Still trying to figure this out. Took your idea, Cybernetician, and pulled the array out. Even now though, with resetting the rfidArray repeatedly, it still produces one garbage line prior to reading properly.

take snapshot of your result and post without this it is really difficult

I have no idea how to insert an image that isn't hosted onto this site, but I'll work on it (or hosting the image).

Now, though, it seems to be slightly better (with no change to the code - weird). It just adds an extra space to the front of the first read on a reset, but then reads the code just fine.

You can just attach a jpeg, using the 'Additional Options...' link under the text entry box.

Thanks. Attached.

if(Serial.available())
  {
    //clearRFID();      No need to execute this funtion two time
        
    int index = 0;
    while(Serial.available())
    {
      rfidArray[index] = Serial.read();
      index++;
    }
    Serial.println(rfidArray);
    
    clearRFID();
  }

Congrats on your output Enjoy

I was pretty sure that it was a dude with an all lower case name that Grumpy_Mike was posting about in the Website and Forum category, but now I'm not so sure.

Congrats on your output Enjoy

That's half-assed code, at best. Serial.print() expects, when the argument is a char array, for that array to be NULL terminated. That code does NOT NULL terminate the array.

That code does not keep collecting data until the whole packet has arrived. It simply assumes that the whole packet is there, and NOTHING but the packet. Those are both poor assumptions to be making.

I was pretty sure that it was a dude with an all lower case name that Grumpy_Mike was posting about in the Website and Forum category, but now I'm not so sure.

??????

NULL terminated.

yep ok. i miss this point