I am using an ESP32 to read data from a MIFARE Card Reader on Serial1.
// inside void loop()
if (Serial1.available() > 10)
{
// Serial.println();
// Serial.print("Before: ");
// Serial.println(Serial1.available()); // number of bytes available to read
Serial.println("Reading RFID Card...");
char input[9];
String str = Serial1.readStringUntil((char)'B');
// Serial.println(str);
rf_id = str.substring(3, 11);
// Serial.println((String)"RFID HEX: '" + rf_id + "' - size: " + rf_id.length());
rf_id.toCharArray(input, 9);
// Serial.println(input);
int val = StrToHex(input);
// Serial.println(val);
rf_id = String(val);
while (rf_id.length() < 10)
{
rf_id = "0" + rf_id;
}
// Serial.println((String)"RFID DEC: '" + rf_id + "' - size: " + rf_id.length());
// Serial.print("After: ");
// Serial.println(Serial1.available()); // number of bytes available to read
Serial1.flush();
}
The reader sends:
-49E69599
Block 1 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
All I need is the HEX code 49E69599. I then transform it into a 10 digit DEC number and store into into a String.
Whenever I read a card this is the output:
Reading RFID Card...
RFID: 1239848345 was sent over UDP.
Reading RFID Card...
This means that the Serial1 is still available even though I've used Serial1.flush(); .
- I would like to speed up the process of reading cards, because right now it's kind of random; sometimes it works pretty fast, and sometimes I have to keep the card in front of the reader for a whole second to get a read.
- I would also like to discard everything in the Serial1 buffer after the 'B' character.
Basically this: String str = Serial1.readStringUntil((char)'B'); then discard the Serial1 buffer.
Serial1.flush() basically does nothing, because keeping it doesn't discard the buffer as the "flush" part of it implies, nor does removing it improves anything at all.
Thank you.