static void barreader_method()
{
barreader.OpenPort(barport, 19200); //opens the port connected to the rfid reader
byte TagType; //tag type
byte[] TagSerial = new byte[4]; //tag serial in reverse order
byte ReturnCode = 0; //return code sent from the rfid reader
string bartagno; //tag no as a string
while (true)
{
bartagno = "";
while (!barreader.CMD_SelectTag(out TagType, out TagSerial, out ReturnCode)) /*wait until a tag is present in the rf field, if present return the tag number.*/
{
}
for (int i = 0; i < 4; i++)
{
bartagno += TagSerial[i].ToString("X2");
}
barprocess(bartagno); //barprocess method
Thread.Sleep(1200); //this is to prevent multiple reads
}
}
If the bartagno variable gets the same value within a minute, i don’t want to execute the barprocess method but to continue the infinite loop. Otherwise barprocess method will be executed. How can i achieve this? I don’t even know where to start. I tried different datetime, loop combinations with no success.
———————————————————progress added below———————————————–
Multiple cards can be read within a minute, so comparing only with the previous one won’t help unfortunately. I tried to use an arraylist instead building on Kelly Ethridge’s answer (thanks). But if we get readings once every ten seconds for example, this will be useless. We still have to delete items older than 1 minute.
static void barreader_method()
{
barreader.OpenPort(barport, 19200);
byte TagType;
byte[] TagSerial = new byte[4];
byte ReturnCode = 0;
string bartagno;
ArrayList previoustagnos = new ArrayList();
DateTime lastreaddt = DateTime.MinValue;
while (true)
{
bartagno = "";
while (!barreader.CMD_SelectTag(out TagType, out TagSerial, out ReturnCode))
{
}
for (int i = 0; i < 4; i++)
{
bartagno += TagSerial[i].ToString("X2");
}
if (DateTime.Now - lastreaddt > TimeSpan.FromMinutes(1))
{
previoustagnos.Clear();
barprocess(bartagno);
previoustagnos.Add(bartagno);
lastreaddt = DateTime.Now;
}
else
{
previoustagnos.Sort();
if (previoustagnos.BinarySearch(bartagno) < 0)
{
barprocess(bartagno);
previoustagnos.Add(bartagno);
lastreaddt = DateTime.Now;
}
}
Thread.Sleep(1200);
}
}
Thanks a lot Dan and Kelly. Without your help, i wouldn’t be able to solve this.