Search code examples
c#scoperfid

How can I enable my counters to increase beyond 1? EventHandler C#


So I am using the Impinj OctaneSDK to run an R420 reader and collect an inventory of tags present and display the number of each type of tag in some text boxes.

the event handler for discovering a tag is pretty straight forward, and tag reports from the reader are configurable. I'm doing all of my operations to count the tags of each type within the event handler, and I fear that something about this is causing me to only be able to reach a max of 1. (also worth noting that I handle the case in which a tag is not seen for some time by removing it from its list.)

Anyone have any idea how I can make the lists persistant between instances of the event handler call with relative ease?

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);



        }


    }

Solution

  • Move your tag variable out of the function because it is reinstantiated and initialized to count = 0 each time the event is called.

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