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
}
}
如果 bartagno 变量在一分钟内获得相同的值,我不想执行 barprocess 方法而是继续无限循环。否则会执行 barprocess 方法。我怎样才能做到这一点?我什至不知道从哪里开始。我尝试了不同的日期时间、循环组合但没有成功。
-------------------------------------------------- --------下面添加了进度---------------------------------------- --------
可以在一分钟内读取多张卡片,因此不幸的是,仅与前一张比较无济于事。我尝试使用数组列表而不是基于 Kelly Ethridge 的回答(谢谢)。但是,例如,如果我们每 10 秒读取一次读数,这将毫无用处。我们仍然必须删除超过 1 分钟的项目。
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);
}
}