Go Down

Topic: RFID Question (Read 525 times) previous topic - next topic

rogue_wraith

Code: [Select]

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?

Cybernetician

Code: [Select]


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;
    }
}
From Idea To Invention

rogue_wraith

Code: [Select]

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.

Cybernetician

take snapshot of your result and post without this it is really difficult
From Idea To Invention

rogue_wraith

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.

dxw00d

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

rogue_wraith

Thanks.  Attached.

Cybernetician

Code: [Select]
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
From Idea To Invention

PaulS

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.

Quote
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.

Cybernetician

#9
Nov 16, 2012, 02:35 pm Last Edit: Nov 16, 2012, 02:37 pm by Cybernetician Reason: 1
Quote
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.


??????

Quote
NULL terminated.


yep ok. i miss this point
From Idea To Invention

Go Up