Pages: [1]   Go Down
Author Topic: RFID Question  (Read 459 times)
0 Members and 1 Guest are viewing this topic.
Horizon City, TX
Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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;
    }
}
Logged

From Idea To Invention

Horizon City, TX
Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

From Idea To Invention

Horizon City, TX
Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Horizon City, TX
Offline Offline
Newbie
*
Karma: 1
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks.  Attached.


* Picture1.jpg (43.31 KB, 639x864 - viewed 23 times.)
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

From Idea To Invention

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: November 16, 2012, 08:37:29 am by Cybernetician » Logged

From Idea To Invention

Pages: [1]   Go Up
Jump to: