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方法。我怎样才能实现这一目标?我甚至都不知道从哪里开始。我尝试了不同的日期时间,循环组合但没有成功。
----------------------------------------------- ----------下面添加的进展------------------------------------- ----------
可以在一分钟内读取多张卡片,因此不幸的是,只能与前一张卡片进行比较。我尝试使用arraylist而不是基于Kelly Ethridge的回答(谢谢)。但是,如果我们每隔十秒获得一次读数,那么这将是无用的。我们仍然需要删除超过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);
}
}
答案 0 :(得分:1)
您需要跟踪上次调用 barprocess 以及之前 bartango 的内容。
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
string previousbartango;
var lastTimeCalled = DateTime.MinValue;
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");
}
var spanSinceLastCalled = DateTime.Now - lastTimeCalled;
if (spanSinceLastCalled > TimeSpan.FromMinutes(1) || bartango != previousbartango)
{
barprocess(bartagno); //barprocess method
previousbartango = bartango;
lastTimeCalled = DateTime.Now;
}
Thread.Sleep(1200); //this is to prevent multiple reads
}
}
这是空气码,但我认为你明白了。
答案 1 :(得分:1)
这取决于您希望在一分钟内看到多少个标签。
想到一种丑陋的方式是创建一个新变量
List<Tuple<DateTime, String>> recentTags = new List<Tuple<DateTime, String>>()
每当您看到一个bartagno时,请搜索此列表以查看它是否已经在此处。如果是,请跳过它。如果没有,请将其添加到列表中:
recentTags.Add(Tuple.Create(DateTime.Now, bartagno));
偶尔(通过主循环可能需要5到10次),您需要从此列表中删除旧记录。
recentTags.RemoveAll(e => e.Item1....
(crud - 我不记得“e.Item1过去超过1分钟”的语法)
答案 2 :(得分:0)
这不像存储上次处理的值和日期时间那么简单。如果读取在时间限制内,则在调用barprocess()方法之前测试相同性?
... String lastBarTagNo =“”; DateTime lastTagProcessDateTime = DateTime.MinValue; ... 而(真){ ... //读取标签并构建新的bartagno if(DateTime.Now - lastTagProcessDateTime&lt; TimeSpan.FromMinutes(1))&amp;&amp; lastBarTagNo.Equals(bartagno){ barprocess(); } }
(抱歉格式化 - 在智能手机上执行此操作)
答案 3 :(得分:0)
static void barreader_method()
{
barreader.OpenPort(barport, 19200);
byte TagType;
byte[] TagSerial = new byte[4];
byte ReturnCode = 0;
string bartagno;
List<Tuple<DateTime, String>> previoustags = new List<Tuple<DateTime, String>>();
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))
{
previoustags.Clear();
barprocess(bartagno);
previoustags.Add(Tuple.Create(DateTime.Now, bartagno));
lastreaddt = DateTime.Now;
}
else
{
if (!previoustags.Exists(a => a.Item2.Equals(bartagno)))
{
barprocess(bartagno);
previoustags.Add(Tuple.Create(DateTime.Now, bartagno));
lastreaddt = DateTime.Now;
}
}
previoustags.RemoveAll(a => a.Item1.CompareTo(DateTime.Now - TimeSpan.FromMinutes(1)) < 0);
Thread.Sleep(1200);
}
}
非常感谢Dan和Kelly。没有你的帮助,我将无法解决这个问题。