如何让我的计数器增加到超过1? EventHandler C#

时间:2016-03-22 06:41:17

标签: c# scope rfid

所以我使用Impinj OctaneSDK运行R420阅读器并收集存在的标签清单,并在某些文本框中显示每种标签的数量。

用于发现标记的事件处理程序非常简单,来自阅读器的标记报告是可配置的。我正在做所有的操作来计算事件处理程序中每种类型的标签,我担心这个问题导致我只能达到最大值1.还值得注意我处理的情况通过从标签中删除标签一段时间后看不到标签。)

任何人都知道我如何能够相对轻松地使事件处理程序调用的实例之间的列表保持一致?

void OnTagsReported(ImpinjReader sender, TagReport report )
    {
        // This event handler is called asynchronously 
        // when tag reports are available.
        // Loop through each tag in the report 
        // and print the data.


        List <Tag> listMed1 = new List<Tag>();
        List<Tag> listMed2 = new List<Tag>();
        List<Tag> listMed3 = new List<Tag>();
        List<Tag> listMed4 = new List<Tag>();
        List<Tag> listMed5 = new List<Tag>();
        List<Tag> tags = new List<Tag>();
        foreach (Tag tag in report)
        {

            ushort AntennaNum = tag.AntennaPortNumber;

            tags.Add(tag);
            int size = tags.Count();
            int i = 0;
            while (i < size)
            {
                Impinj.OctaneSdk.TagData first = tags[i].Epc;
                string epcCheck = first.ToString();

                if (epcCheck.IndexOf("A") != -1)
                {
                    listMed1.Add(tags[i]);
                }
                if (epcCheck.IndexOf("B") != -1)
                {
                    listMed2.Add(tags[i]);
                }
                if (epcCheck.IndexOf("C") != -1)
                {
                    listMed3.Add(tags[i]);
                }
                if (epcCheck.IndexOf("D") != -1)
                {
                    listMed4.Add(tags[i]);
                }
                if (epcCheck.IndexOf("E") != -1)
                {
                    listMed5.Add(tags[i]);
                }
                i++;
            }



            int Med1num = listMed1.Count();
            int Med2num = listMed2.Count();
            int Med3num = listMed3.Count();
            int Med4num = listMed4.Count();
            int Med5num = listMed5.Count();
            int loopr1 = 0;
            int loopr2 = 0;
            int loopr3 = 0;
            int loopr4 = 0;
            int loopr5 = 0;

            while (loopr1< Med1num)
            {
                Impinj.OctaneSdk.ImpinjTimestamp second = listMed1[loopr1].LastSeenTime;
                string milisecondsUTC = second.ToString();
                long lastseen = Convert.ToInt64(milisecondsUTC);
                TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                long secondsSinceEpoch = (long)t.TotalMilliseconds;
                if (secondsSinceEpoch - lastseen > 5000 )
                {
                    listMed1.RemoveAt(loopr1);
                }
                loopr1++;
            }
            while (loopr2 < Med2num)
            {
                Impinj.OctaneSdk.ImpinjTimestamp second = listMed2[loopr2].LastSeenTime;
                string milisecondsUTC = second.ToString();
                long lastseen = Convert.ToInt64(milisecondsUTC);
                TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                long secondsSinceEpoch = (long)t.TotalMilliseconds;
                if (secondsSinceEpoch - lastseen > 5000)
                {
                    listMed2.RemoveAt(loopr2);
                }
                loopr2++;
            }
            while (loopr3 < Med3num)
            {
                Impinj.OctaneSdk.ImpinjTimestamp second = listMed3[loopr3].LastSeenTime;
                string milisecondsUTC = second.ToString();
                long lastseen = Convert.ToInt64(milisecondsUTC);
                TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                long secondsSinceEpoch = (long)t.TotalMilliseconds;
                if (secondsSinceEpoch - lastseen > 5000)
                {
                    listMed3.RemoveAt(loopr3);
                }
                loopr3++;
            }
            while (loopr4 < Med4num)
            {
                Impinj.OctaneSdk.ImpinjTimestamp second = listMed4[loopr4].LastSeenTime;
                string milisecondsUTC = second.ToString();
                long lastseen = Convert.ToInt64(milisecondsUTC);
                TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                long secondsSinceEpoch = (long)t.TotalMilliseconds;
                if (secondsSinceEpoch - lastseen > 5000)
                {
                    listMed4.RemoveAt(loopr4);
                }
                loopr4++;
            }
            while (loopr5 < Med5num)
            {
                Impinj.OctaneSdk.ImpinjTimestamp second = listMed5[loopr5].LastSeenTime;
                string milisecondsUTC = second.ToString();
                long lastseen = Convert.ToInt64(milisecondsUTC);
                TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                long secondsSinceEpoch = (long)t.TotalMilliseconds;
                if (secondsSinceEpoch - lastseen > 5000)
                {
                    listMed5.RemoveAt(loopr5);
                }
                loopr5++;
            }
            Med1num = listMed1.Count();
            Med2num = listMed2.Count();
            Med3num = listMed3.Count();
            Med4num = listMed4.Count();
            Med5num = listMed5.Count();

            SetText(Med1num, Med2num, Med3num, Med4num, Med5num);



        }


    }

2 个答案:

答案 0 :(得分:1)

将标记变量移出函数,因为每次调用事件时它都会重新实例化并初始化为count = 0。

List<Tag> tags = new List<Tag>();

void OnTagsReported(ImpinjReader sender, TagReport report )
{
  ...
}

答案 1 :(得分:0)

这不是你问题的直接答案,而是更多的“大型”评论。

您可以使代码更小。

这是重构版本:

void OnTagsReported(ImpinjReader sender, TagReport report)
{
    List<Tag> tags = report.ToList();

    var lists =
        new [] { "A", "B", "C", "D", "E", }
            .Select(c =>
                tags
                    .Where(tag => tag.Epc.ToString().IndexOf(c) != -1)
                    .ToList())
            .ToList();

    foreach (var list in lists)
    {
        list.RemoveAll(tag =>
        {
            Impinj.OctaneSdk.ImpinjTimestamp second = tag.LastSeenTime;
            string milisecondsUTC = second.ToString();
            long lastseen = Convert.ToInt64(milisecondsUTC);
            TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
            long secondsSinceEpoch = (long)t.TotalMilliseconds;
            return secondsSinceEpoch - lastseen > 5000;
        });
    }

    SetText(lists.Select(list => list.Count()).ToArray());
}

假设SetText具有签名void SetText(params int[] xs)。如果不是,您只需将参数放入变量并逐个发送每个值。

从这里我看不出为什么你需要将tags作为字段级变量推出。在我看来,你每次都需要来计算它。

你能解释一下如何将tags推到方法之外吗?