Serial.availabe()循环仅在第一次工作

时间:2019-04-08 06:58:01

标签: arduino rfid

我正在研究RFID。我正在通过串行监视器发送数据,并且Serial.available在第一次之后无法识别数据,它在重新打开串行端口后只能正常工作一次。

我已经尝试了我可以在代码中做的所有可能的更改。我希望在运行此代码时,在串行监视器中输入数据后,它应该显示“扫描mifare卡”,并且在扫描卡后,它应该执行读取或写入操作。

#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("*");
}

0 个答案:

没有答案