1

I am working on RFID. I am sending data through serial monitor and the Serial.available is not identifying the data after the first time, its working perfectly fine only once after reopening the comport.

I have tried all the possible changes I could do in the code. I want that when I run this code, then after entering the data in serial monitor it should show "Scan the mifare card" and after scanning the card it should perform read or write action.

#include <SPI.h>//include the SPI bus library
#include <MFRC522.h>//include the RFID reader library

#define SS_PIN 10  //slave select pin
#define RST_PIN 9 //reset pin
MFRC522 mfrc522(SS_PIN, RST_PIN);        // instatiate a MFRC522 reader object.
MFRC522::MIFARE_Key key;//create a MIFARE_Key struct named 'key', which will hold the card information


void setup()
 {
        Serial.begin(9600);        // Initialize serial communications with the PC
        SPI.begin();               // Init SPI bus
        mfrc522.PCD_Init();     
        //while (! Serial); // Wait untilSerial is ready - Leonardo
        //Serial.println("Enter data");
 }

String incomingStr;
byte blockcontent[16];
int count=0;
String arr2[6];
int arr3[6];
int u2=0;
String arr[16];
int arr1[16];
int u=0;
int y;
char action;
//int newc=0;

void loop()
{
 // Serial.println("bahar"); 
  if (Serial.available()) 
    {
      doReceive();
      int newc=0;       
      while(newc==0)
       {
         if ( ! mfrc522.PICC_IsNewCardPresent()) 
            {
               if(count==0)
                   {
                       Serial.println("Scan your mifare card %");
                       count++;
                   }
            }
        else
            {
               newc=1;
            }
       }

     if ( ! mfrc522.PICC_ReadCardSerial())
       {
         return;
       }
     if(action=='%')
       {
         doWrite();
       }
     else if(action=='$')
       {
         doRead();
       }
     else if(action=='^')
       {
         for (byte i = 0; i < mfrc522.uid.size; ++i) 
           { // read id (in parts)
              Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
              Serial.print(mfrc522.uid.uidByte[i], HEX);
           }
         Serial.print("*");
       }
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();   
  Serial.flush(); 
   }
}



void doReceive()
{
   incomingStr = Serial.readString();
   int sto=incomingStr.indexOf(')');
   action=incomingStr.charAt(sto+1);
   y=sto+2;
   for(int j=0;j<sto;j++)
    {
      if(incomingStr.charAt(j)!= ' ')
       {
         //Serial.print(incomingStr.charAt(j));
         //arr[u]=arr[u].concat(incomingStr.charAt(j));
         arr2[u2]+=incomingStr.charAt(j); 
       }
     else
       {
         //Serial.println(arr[u]);
         u2++;
       }
    } 
  for(u2=0;u2<6;u2++)
    {
      arr3[u2]=arr2[u2].toInt();
      key.keyByte[u2]=arr3[u2];
      //Serial.println(key.keyByte[u2]);
    }
 }
void doWrite()
{
  int block=1;
  for(int i=0;i<incomingStr.length();i=i+16)
  {
    if(block == 3 || block ==7 || block ==11 || block ==15 || block ==19 || block ==23 || block ==27 || block ==31 || block ==35 || block ==39 || block ==43 || block ==47 || block ==51 || block ==55 || block ==59 || block ==63)
      {
        block=block+1;
      }
    for(int u=0;u<16;u++)
      {  
        blockcontent[u]=0;
        //Serial.println(arr1[u]);
      }
    String s1=incomingStr.substring(y+i,y+16+i);
    s1.getBytes((char *) blockcontent, 30);
    writeBlock(block, blockcontent);  
    block=block+1;  
  }
  Serial.println("Data written successfully*");
}
void doRead()
{
  int block=1;
  for(int i=0;i<4;i++)
    { 
      if(block == 3 || block ==7 || block ==11 || block ==15 || block ==19 || block ==23 || block ==27 || block ==31 || block ==35 || block ==39 || block ==43 || block ==47 || block ==51 || block ==55 || block ==59 || block ==63)
        {
          block=block+1;
        } 
      byte readbackblock[18];
      readBlock(block, readbackblock);//read the block back  
      //Serial.print("Block contains: ");
      for (int j=0 ; j<16 ; j++)//print the block contents
        {
           Serial.write (readbackblock[j]);//Serial.write() transmits the ASCII numbers as human readable characters to serial monitor 
        }

     //Serial.println();
     block=block+1;
    }
  Serial.println("*");
 } 
  • 1
    I have tried all the possible changes I could do in the code. First of all, you could make it reabable by proper indenting. – DataFiddler Apr 8 at 8:55
  • arr2[u2]+=incomingStr.charAt(j); I don't understand this. But I doubt, it's doing want you want – DataFiddler Apr 8 at 9:02
  • 1
    @user55609 please use proper indentation wile programming. It help other to understand your code easily. – Vaibhav Apr 8 at 9:20
  • add some test point in you code after every module execution, that will help you to get the error. – Vaibhav Apr 8 at 9:57
  • what string you sent from serial monitor? – Vaibhav Apr 8 at 11:55
1

First of all try to add these debugging prints here to see if this while loop is becoming infinite:

  while(newc==0)
   {
     if ( ! mfrc522.PICC_IsNewCardPresent()) 
        {
           if(count==0)
               {
                   Serial.println("Scan your mifare card %");
                   count++;
                   **Serial.println("test0")*
               }

        }
    else
        {
           newc=1;
           **Serial.println ("newc=1")**
        }
   }
0

arr2 have size 6. Have you made sure that here

...    
for(int j=0;j<sto;j++)
{
  if(incomingStr.charAt(j)!= ' ')
   {
     arr2[u2]+=incomingStr.charAt(j); 
   }
 else
   {
     u2++;
   }
}
...

you are not writing beyond the bounds of that array?

Moreover, you never explicitly set the initial value of u2 before starting this cycle - whatever was left in global variable u2 from the previous uses will serve as the staring value for this cycle.

Judging by cursory examination of the rest of your code, only the very first call to doReceive will begin this cycle with u2 == 0. After that every time you call doReceive, you will start this cycle with u2 == 6, definitely overflowing the array and destroying the content of adjacent memory locations. The behavior is undefined.

I guess it is a "global variables strike again" kind of situation. Why on Earth is u2 made a global variable???

It is quite possible that you can "fix" your code by simply adding u2 = 0; before the above cycle. But even after that the code will remain in a rather precarious state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.