I am developing a RFID attendance counting system for my classroom. I have interfaced MFRC-522 RFID module with Arduino Uno. The Arduino Uno is connected to a PC via serial port. A C# program reads the serial port to get the RFID tag number and performs matching.
Now, the C# program can successfully read the whole 13-digit number of the card only when it is the first scan. But in the next scans for the same card, the 13-digit number breaks into several parts. The console output of two consecutive tests are shown below:
Press any key to continue...
Data Received: 160975869190 Matched
Data Received: 1 Not Matched
Data Received: 6097 Not Matched
Data Received: 5869 Not Matched
Data Received: 190 Not Matched
I can not figure out why the card number can be read at the first scan but the number breaks into several parts in the next scans? The both Arduino and C# codes are included below:
Arduino Code:
void setup()
{
SPI.begin();
rfid.init();
Serial.begin(9600);
}
void loop()
{
if(rfid.isCard())
{
RoutineRFID();
}
}
void RoutineRFID()
{
if(rfid.readCardSerial())
{
Serial.print(rfid.serNum[0]);
Serial.print(rfid.serNum[1]);
Serial.print(rfid.serNum[2]);
Serial.print(rfid.serNum[3]);
Serial.print(rfid.serNum[4]);
Serial.flush();
}
while(!Serial.available())
{
}
String studentName = Serial.readString();
lcd.print(studentName);
delay(3000);
}
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace Test_Serial
{
class Program
{
static void Main(string[] args)
{
try{
SerialPort myPort = new SerialPort();
myPort.BaudRate = 9600;
myPort.PortName = "COM6";
myPort.Open();
myPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
myPort.Close();
}
catch (Exception ex)
{
Console.WriteLine("Com port Disconnected!");
}
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.WriteLine(indata);
if (String.Equals(indata, "160975869190"))
{
Console.WriteLine("Matched");
sp.WriteLine("Matched");
}
else
{
Console.WriteLine("Not Matched");
sp.WriteLine("Not Matched");
}
Console.WriteLine();
}
}
}