Exemple #1
0
 private static void HNur_InventoryStreamEvent(object sender, NurApi.InventoryStreamEventArgs e)
 {
     try
     {
         if (e.data.tagsAdded > 0)
         {
             NurApi.TagStorage tags = hNur.GetTagStorage();
             foreach (NurApi.Tag tag in tags)
             {
                 //have we seen this tag yet?
                 if (!inventoryResults.Contains(tag.GetEpcString()))
                 {
                     inventoryResults.Add(tag.GetEpcString());
                     if (client != null)
                     {
                         client.PublishMessage("nurapisample/epc", System.Text.Encoding.ASCII.GetBytes(tag.GetEpcString()));
                     }
                 }
             }
         }
         //if streaming stopped, restart it
         if (e.data.stopped)
         {
             hNur.StartInventoryStream();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Something went wrong during inventory stream event [" + ex.Message + "]");
     }
 }
Exemple #2
0
 private void SimpleInventoryButton_Click(object sender, EventArgs e)
 {
     //Performs simple inventory of tags using current rounds-, Q and session parameters
     try
     {
         //Clear existing items from Listbox
         listBox1.Items.Clear();
         //Clear existing tags from NurModule memory
         hNur.ClearTags();
         NurApi.InventoryResponse response; //Information about inventory store here
         //Make Inventory..
         response = hNur.SimpleInventory();
         //Show information about inventory first
         listBox1.Items.Add("Number of tags found in this inventory: " + response.numTagsFound.ToString());
         listBox1.Items.Add("Total number of tags in module memory : " + response.numTagsMem.ToString());
         listBox1.Items.Add("Number of possible collisions or reception errors in this inventory: " + response.collisions.ToString());
         listBox1.Items.Add("Q used in this inventory: " + response.Q.ToString());
         listBox1.Items.Add("Number of full Q rounds done in this inventory: " + response.roundsDone.ToString());
         listBox1.Items.Add("-------------EPC----------------");
         //Read result to TagStorage object
         NurApi.TagStorage inv = hNur.FetchTags(true);
         //Show results in the ListBox (EPC code)
         foreach (NurApi.Tag tag in inv)
         {
             listBox1.Items.Add(tag.GetEpcString());
         }
     }
     catch (Exception ex)
     {   //Something went wrong.(usually transport not connected) Show reason in the MessageBox.
         MessageBox.Show(ex.ToString(), "Exception");
     }
 }
        // Update our unique tag storage from NurApi tag storage
        // NOTE: UI is not updated here
        bool UpdateInventoriedTags()
        {
            bool ret = false;

            NurApi.TagStorage ts = App.NurApi.GetTagStorage();
            lock (ts)
            {
                foreach (NurApi.Tag tag in ts)
                {
                    if (InventoriedTagsDict.ContainsKey(tag.GetEpcString()))
                    {
                        // Tag updated
                        InventoriedTag iTag = InventoriedTagsDict[tag.GetEpcString()];
                        iTag.Rssi      = tag.rssi.ToString();
                        iTag.UiUpdated = true;
                    }
                    else
                    {
                        // New tag added
                        ret = true;
                        InventoriedTag iTag = new InventoriedTag()
                        {
                            EPC  = tag.GetEpcString(),
                            Rssi = tag.rssi.ToString()
                        };

                        InventoriedTagsDict.Add(tag.GetEpcString(), iTag);
                    }
                }
            }
            return(ret);
        }
        /// <summary>
        /// Handles the InventoryStreamEvent event of the NUR module.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="NurApi.InventoryStreamEventArgs" /> instance containing the event data.</param>
        private void hNur_InventoryStreamEvent(object sender, NurApi.InventoryStreamEventArgs e)
        {
            try
            {
                NurApi hNur = sender as NurApi;

                if (e.data.tagsAdded > 0)
                {
                    totalReads += e.data.tagsAdded;
                    NurApi.TagStorage inventoriedTags = hNur.GetTagStorage();
                    int numberOfNewTag = tagListBox.UpdateTagList(inventoriedTags);
                    beeperInventory.Beep(numberOfNewTag);
                    WriteLogToFile(inventoriedTags);
                    UpdateStatistics(inventoriedTags);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), Program.appName);
            }

            // Restart if stopped by TimeLimit
            if (e.data.stopped && continueInventory)
            {
                StartInventory(hNur);
            }

            //Keepd device alive
            HHUtils.KeepDeviceAlive();
        }
        /// <summary>
        /// Updates the statistics.
        /// </summary>
        private void UpdateStatistics(NurApi.TagStorage tagStorage)
        {
            if (tagStorage != null)
            {
                int ticksNow = System.Environment.TickCount;
                int elapsed  = ticksNow - startTick;
                totalLabel.Text        = "Total reads in " + (elapsed / 1000).ToString() + " sec";
                totalReadsLabel.Text   = "  " + totalReads.ToString() + " reads";
                totalAverageLabel.Text = "  " + ((totalReads * 1000) / elapsed).ToString() + " reads/sec";

                if (uniqueTags != tagStorage.Count)
                {
                    uniqueTick = ticksNow;
                    uniqueTags = tagStorage.Count;
                    int uniqueElapsed = uniqueTick - startTick;
                    uniqueLabel.Text        = "Unique tags in " + (uniqueElapsed / 1000).ToString() + " sec";
                    tagsFoundLabel.Text     = string.Format("{0} Tags", uniqueTags);
                    uniqueTagsLabel.Text    = "  " + uniqueTags.ToString() + " unique tags";
                    uniqueAverageLabel.Text = "  " + ((uniqueTags * 1000) / uniqueElapsed).ToString() + " unique tags/sec";
                }
            }
            else
            {
                tagsFoundLabel.Text     = "- - -";
                totalReadsLabel.Text    = "- - -";
                totalAverageLabel.Text  = "- - -";
                uniqueTagsLabel.Text    = "- - -";
                uniqueAverageLabel.Text = "- - -";

                totalReads = 0;
                uniqueTags = 0;
                startTick  = System.Environment.TickCount;
                uniqueTick = startTick;
            }
        }
Exemple #6
0
        /// <summary>
        /// Handles the InventoryStreamEvent event of the hNur control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="NurApi.InventoryStreamEventArgs"/> instance containing the event data.</param>
        void hNur_InventoryStreamEvent(object sender, NurApi.InventoryStreamEventArgs e)
        {
            try
            {
                NurApi hNur = sender as NurApi;

                if (e.data.tagsAdded > 0)
                {
                    NurApi.TagStorage inventoriedTags = hNur.GetTagStorage();
                    int numberOfNewTag = nxpTagListView.UpdateTagList(inventoriedTags);
                    //beeperInventory.Beep(numberOfNewTag);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), Program.appName);
            }

            // Restart if stopped by TimeLimit
            if (e.data.stopped && continueInventory)
            {
                StartPsfInventory();
            }

            //Keepd device alive
            HHUtils.KeepDeviceAlive();
        }
Exemple #7
0
        // Inventory stream event, this is called when there's tags available in NurApi tag storage
        void hNur_InventoryStreamReady(object sender, NurApi.InventoryStreamEventArgs e)
        {
            // Get NurApi object that generated the event
            NurApi hNur = sender as NurApi;

            try
            {
                // Copy tags from NurApi internal tag storage to application tag storage
                NurApi.TagStorage intTagStorage = hNur.GetTagStorage();
                lock (intTagStorage)
                {
                    SendMessage(string.Format("Reader: {0}, Tags: {1}", hNur.GetReaderInfo().altSerial, intTagStorage.Count));
                    // Clear NurApi internal tag storage
                    hNur.ClearTags();
                }

                if (e.data.stopped)
                {
                    // Start streaming again if stopped
                    hNur.StartInventoryStream();
                }
            }
            catch (Exception ex)
            {
                // Handle error
                SendMessage(ex.Message);
            }
        }
        /// <summary>
        /// Handles the NXPAlarmStreamEvent event of the hNur control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="NurApi.NXPAlarmStreamEventArgs"/> instance containing the event data.</param>
        void hNur_NXPAlarmStreamEvent(object sender, NurApi.NXPAlarmStreamEventArgs e)
        {
            try
            {
                NurApi hNur = sender as NurApi;

                if (e.data.armed)
                {
                    NurApi.TagStorage inventoriedTags = hNur.GetTagStorage();
                    int numberOfNewTag = nxpTagListView.UpdateTagList(inventoriedTags);
                    //tagsFoundLabel.Text = string.Format("{0} Tags", inventoriedTags.Count);
                    //beeperInventory.Beep(numberOfNewTag);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), Program.appName);
            }

            // Restart if stopped by TimeLimit
            //if (e.data.stopped && continueInventory)
            //{
            //    hNur.StartInventoryStream();
            //}

            //Keepd device alive
            HHUtils.KeepDeviceAlive();
        }
Exemple #9
0
 // Add all unique tags from srcStorage to application own tag storage
 private void AddTagsToList(NurApi.TagStorage srcStorage)
 {
     foreach (NurApi.Tag tag in srcStorage)
     {
         // Copy to application tag storage
         if (tagStorage.AddTag(tag))
         {
             // Only add to list, if tag was new unique tag
             lstbox.Items.Add(tag.GetEpcString());
         }
     }
 }
 /// <summary>
 /// Handles the Click event of the refreshBtn control.
 /// </summary>
 /// <param name="sender">The source of the event.</param>
 /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
 private void refreshBtn_Click(object sender, EventArgs e)
 {
     // Clear Tag List
     nxpTagListView.ClearTagList();
     // Clear previously inventoried tags from memory
     hNur.ClearTags();
     // Perform simple inventory
     for (int i = 0; i < 3; i++)
     {
         hNur.SimpleInventory();
     }
     // Fetch tags from module, including tag meta
     NurApi.TagStorage inventoriedTags = hNur.FetchTags(true);
     // Update Tag List
     nxpTagListView.UpdateTagList(inventoriedTags);
 }
        private async void InventoryButton_Click(object sender, RoutedEventArgs e)
        {
            InventoryButton.IsEnabled = false;

            // Perform inventory, run in task because NurApi calls may block
            await Task.Run(async() =>
            {
                try
                {
                    // Clear nur tag stroage first
                    mApi.ClearTagsEx();

                    // Perform simple inventory
                    NurApi.InventoryResponse rsp = mApi.Inventory();

                    // Fetch all tags from reader
                    NurApi.TagStorage storage = mApi.FetchTags();

                    // Print some info
                    string txt = string.Format("{0} tags found\n", rsp.numTagsMem);
                    lock (storage)
                    {
                        foreach (NurApi.Tag tag in storage)
                        {
                            txt += tag.GetEpcString() + "\n";
                        }
                    }

                    // Show dialog in UI thread
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                    {
                        var dialog = new MessageDialog(txt, "Inventory Results");
                        await dialog.ShowAsync();
                    });
                }
                catch (Exception ex)
                {
                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                    {
                        var dialog = new MessageDialog(ex.Message, "Inventory Error");
                        await dialog.ShowAsync();
                    });
                }
            });

            InventoryButton.IsEnabled = true;
        }
Exemple #12
0
        public bool InitTag(long uidTarget)
        {
            try
            {
                hNur.ClearTags();
                NurApi.InventoryResponse response;          //Information about inventory store here
                response = hNur.SimpleInventory();          //Make Inventory..
                NurApi.TagStorage inv = hNur.FetchTags(true);
                NurApi.Tag        tag = null;

                if (response.numTagsFound > 1)
                {
                    throw new Exception("more than one tag found in the antena writing EPC");
                }

                tag = inv[0];
                byte[] uid = convertLongToByteArray(uidTarget, 6);
                byte[] EPC = new byte[EPCbyteCount];

                Array.Copy(uid, 0, EPC, 0, 6);
                tag.WriteTag(0, false, EPCbank, 2, EPC);

                hNur.ClearTags();
                response = hNur.SimpleInventory();          //Make Inventory..
                inv      = hNur.FetchTags(true);
                tag      = null;

                if (response.numTagsFound > 1)
                {
                    throw new Exception("more than one tag found in the antena writing userdata");
                }

                tag = inv[0];

                byte[] USERDATA = new byte[USERbyteCount];
                USERDATA[0] = (byte)(USERDATA[0]);
                tag.WriteTag(0, false, USERbank, 0, USERDATA);

                return(true);
            }
            catch (Exception ex)
            {
                _Error = "Error initializing RFID tag " + ex.Message;
                return(false);
            }
        }
        /// <summary>
        /// Updates the tag list.
        /// </summary>
        /// <param name="inventoriedTags">The inventoried tags.</param>
        /// <returns>The number of new tags</returns>
        public int UpdateTagList(NurApi.TagStorage inventoriedTags)
        {
            int numberOfNewTags = inventoriedTags.Count - tagListView.Items.Count;

            if (inventoriedTags.Count > tagListView.Items.Count)
            {
                // Update ListBox
                tagListView.BeginUpdate();
                for (int i = tagListView.Items.Count; i < inventoriedTags.Count; i++)
                {
                    TagItem tagItem = new TagItem(inventoriedTags[i]);
                    tagListView.Items.Add(tagItem.TagViewItem);
                }
                tagListView.EndUpdate();
            }
            return(numberOfNewTags);
        }
Exemple #14
0
        // Inventory stream event, this is called when there's tags available in NurApi tag storage
        void hNur_InventoryStreamReady(object sender, NurApi.InventoryStreamEventArgs e)
        {
            try
            {
                // Copy tags from NurApi internal tag storage to application tag storage
                NurApi.TagStorage intTagStorage = hNur.GetTagStorage();
                lock (intTagStorage)
                {
                    for (int i = 0; i < intTagStorage.Count; i++)
                    {
                        NurApi.Tag tag;
                        if (tags.AddTag(intTagStorage[i], out tag))
                        {
                            // Add new unique tag added
                            int index = listBox1.Items.Add(string.Format("EPC: {0}, ANT: {1}, RSSI: {2}",
                                                                         tag.GetEpcString(), tag.antennaId, tag.rssi));
                            tag.userData = index;
                        }
                        else
                        {
                            // Update current tag
                            int index = (int)tag.userData;
                            listBox1.Items[index] = string.Format("EPC: {0}, ANT: {1}, RSSI: {2}",
                                                                  tag.GetEpcString(), tag.antennaId, tag.rssi);
                        }
                    }
                    // Clear NurApi internal tag storage
                    hNur.ClearTags();
                }

                if (e.data.stopped && running)
                {
                    // Start streaming again if stopped
                    hNur.StartInventoryStream();
                }
            }
            catch (Exception ex)
            {
                // Handle error
                listBox1.Items.Add(ex.Message);
            }
        }
        /// <summary>
        /// Writes the log to file.
        /// </summary>
        /// <param name="tagStorage">The tag storage.</param>
        private void WriteLogToFile(NurApi.TagStorage tagStorage)
        {
            if (logToFileEnabled)
            {
                for (; logToFileTagCounter < tagStorage.Count; logToFileTagCounter++)
                {
                    switch (logToFileFormat)
                    {
                    case 0:
                        // EPC
                        logToFile.WriteLog(tagStorage[logToFileTagCounter].GetEpcString());
                        break;

                    case 1:
                        // EPC, DATA
                        logToFile.WriteLog(
                            string.Format("{0}{1}{2}",
                                          tagStorage[logToFileTagCounter].GetEpcString(),
                                          logToFileSeparator,
                                          tagStorage[logToFileTagCounter].irData != null ?
                                          NurApi.BinToHexString(tagStorage[logToFileTagCounter].irData) :
                                          ""));
                        break;

                    case 2:
                        // Date&Time, EPC, RSSI
                        logToFile.WriteLog(
                            string.Format("{0}{1}{2}{3}{4}",
                                          DateTime.Now.ToString(),
                                          logToFileSeparator,
                                          tagStorage[logToFileTagCounter].GetEpcString(),
                                          logToFileSeparator,
                                          tagStorage[logToFileTagCounter].rssi.ToString()));
                        break;
                    }
                }
            }
        }
Exemple #16
0
        private void InventoryStreamEventHandler(object sender, NurApi.InventoryStreamEventArgs e)
        {
            try
            {
                //Only what we do here is to take count of readed tags.
                //Real app do much more...
                tags     = myNur.GetTagStorage();
                tagCount = tags.Count;
                totalInvRounds++;

                myNur.ClearTags();
                if (e.data.stopped)
                {
                    //Start again if stopped.
                    myNur.StartInventoryStream();
                }
            }
            catch
            {
                //Some problems. Stop all and exit from this thread.
                Stop();
            }
        }
Exemple #17
0
        public bool ReadTags()
        {
            int n        = 0;
            int numTries = 3;

            try
            {
                hNur.TxLevel = _Level;
                hNur.ClearTags();
                NurApi.InventoryResponse response; //Information about inventory store here
                do
                {
                    response = hNur.SimpleInventory();         //Make Inventory..
                    _InventoryInfo.numTagsFound = response.numTagsFound;
                    _InventoryInfo.numTagsMem   = response.numTagsMem;
                    _InventoryInfo.collisions   = response.collisions;
                    _InventoryInfo.Q            = response.Q;
                    _InventoryInfo.roundsDone   = response.roundsDone;
                } while (--numTries > 0 && response.numTagsFound == 0);
                if (numTries < 0)
                {
                    throw new Exception("NOREAD");
                }


                _InventoryInfo.TIDEX      = new string[response.numTagsFound];
                _InventoryInfo.RSSI       = new sbyte[response.numTagsFound];
                _InventoryInfo.EPCuid     = new long[response.numTagsFound];
                _InventoryInfo.EPCref     = new long[response.numTagsFound];
                _InventoryInfo.EPCdate    = new UInt16[response.numTagsFound];
                _InventoryInfo.EPCcontrol = new UInt16[response.numTagsFound];
                _InventoryInfo.USERdata   = new UInt32[response.numTagsFound];

                NurApi.TagStorage inv = hNur.FetchTags(true);
                foreach (NurApi.Tag tag in inv)
                {
                    _InventoryInfo.RSSI[n] = tag.rssi;

                    byte[] TIDEXbytes = tag.ReadTag(0, false, TIDbank, 0, TIDEXbyteCount);
                    _InventoryInfo.TIDEX[n] = byteArryayToHexString(TIDEXbytes);

                    byte[] EPCbytes = tag.ReadTag(0, false, EPCbank, 2, EPCbyteCount);

                    byte[] eUID = new byte[6];
                    Array.Copy(EPCbytes, 0, eUID, 0, 6);
                    _InventoryInfo.EPCuid[n] = convertByteArrayToLong(eUID);

                    byte[] eRef = new byte[6];
                    Array.Copy(EPCbytes, 6, eRef, 0, 6);
                    _InventoryInfo.EPCref[n] = convertByteArrayToLong(eRef);

                    byte[] eDate = new byte[2];
                    Array.Copy(EPCbytes, 12, eDate, 0, 2);
                    _InventoryInfo.EPCdate[n] = (UInt16)convertByteArrayToLong(eDate);

                    byte[] eControl = new byte[2];
                    Array.Copy(EPCbytes, 14, eControl, 0, 2);
                    _InventoryInfo.EPCcontrol[n] = (UInt16)convertByteArrayToLong(eControl);

                    byte[] USERbytes = tag.ReadTag(0, false, USERbank, 0, USERbyteCount);
                    _InventoryInfo.USERdata[n] = (UInt32)convertByteArrayToLong(USERbytes);
                    n++;
                }

                //TODO:
                if (response.numTagsFound == 0)
                {
                    newDataReceived(ReaderName, null);
                }
                else
                {
                    newDataReceived(ReaderName, _InventoryInfo.TIDEX);
                }

                return(true);
            }
            catch (Exception ex)
            {
                _Error = "Error reading RFID Tag " + ex.Message;
                return(false);
            }
        }
Exemple #18
0
        private void DoGetTags()
        {
            if (!hNur.IsConnected())
            {
                AddLog("Connection required.");
                return;
            }

            CurrentTag = null;
            AllTags.Clear();
            TagListView.Items.Clear();
            TagLabel.Text = "---";

            try
            {
                ResetToA();

                hNur.ClearTagsEx();



                NurApi.InventoryExParams   invParam;
                NurApi.InventoryExFilter[] invFilters;

                // Disable EPC+DATA mode
                hNur.InventoryReadCtl = false;
                // Configure InventoryExParams for Temperature read
                invParam.inventorySelState = NurApi.SELSTATE_SL;
                invParam.inventoryTarget   = NurApi.INVTARGET_A;
                invParam.Q           = hNur.InventoryQ;
                invParam.rounds      = hNur.InventoryRounds;
                invParam.session     = NurApi.SESSION_S0;
                invParam.transitTime = 0;                 // Disable
                // Configure InventoryExFilter
                invFilters                  = new NurApi.InventoryExFilter[1];
                invFilters[0].action        = NurApi.FACTION_0;
                invFilters[0].address       = 0;
                invFilters[0].bank          = NurApi.BANK_TID;
                invFilters[0].maskData      = new byte[] { 0xE2, 0x80, 0xB0, 0x40 };
                invFilters[0].maskBitLength = (invFilters[0].maskData.Length * 8) - 4;
                invFilters[0].target        = NurApi.SESSION_SL;
                invFilters[0].truncate      = false;
                // Read
                NurApi.InventoryResponse resp = hNur.InventoryEx(ref invParam, invFilters);
                // Fetch tags from the module
                NurApi.TagStorage tagStorage = hNur.FetchTags(true);

                for (int i = 0; i < tagStorage.Count; i++)
                {
                    NurApi.Tag tag = tagStorage[i];
                    AllTags.Add(new EM4325Tag(tag));
                    ListViewItem lvi = new ListViewItem(tag.GetEpcString());
                    lvi.SubItems.Add(tag.rssi.ToString());
                    lvi.SubItems.Add(tag.antennaId.ToString());
                    TagListView.Items.Add(lvi);
                }
                columnHeader_EPC.Width   = -2;
                columnHeader_RSSI.Width  = -2;
                columnHeader_AntID.Width = -2;
            }
            catch (Exception ex)
            {
                AddLog("Inventory error.");
                AddLog("Message: " + ex.Message);
            }
        }
Exemple #19
0
        /// <summary>
        /// Searches the nearest tag.
        /// </summary>
        /// <param name="hNur">The NurApi header.</param>
        /// <param name="autoTxLevel">if set to <c>true</c> [use auto tx level].</param>
        /// <param name="tag">reference for the nearest tag</param>
        /// <param name="usedTxLevel">reference for the used TxLevel.</param>
        /// <returns>
        /// The number of found tags.
        /// </returns>
        public static int SearchNearestTag(NurApi hNur, bool autoTxLevel, out NurApi.Tag tag, out int usedTxLevel)
        {
            // Set the used TxLevel
            usedTxLevel = hNur.TxLevel;
            // Clear previously inventoried tags from memory
            hNur.ClearTags();
            NurApi.TagStorage inventoriedTags = null;

            if (autoTxLevel)
            {
                // Backup TX Level
                int backupTxLevel = hNur.TxLevel;
                // Search Tags with auto TX Level
                for (int tx = MIN_TX_LEVEL; tx >= MAX_TX_LEVEL; tx--)
                {
                    // Set TX Level
                    hNur.TxLevel = tx;
                    // Set the used TxLevel
                    usedTxLevel = tx;
                    // Perform simple inventory
                    NurApi.InventoryResponse ir = hNur.SimpleInventory();
                    // Did we find any Tag
                    if (ir.numTagsMem > 0)
                    {
                        // Yes we did
                        break;
                    }
                }
                // Fetch tags from module, including tag meta
                inventoriedTags = hNur.FetchTags(true);
                // Restore TX Level
                hNur.TxLevel = backupTxLevel;
            }
            else
            {
                // Search Tags with fixed TX Level
                for (int i = 0; i < RETRIES; i++)
                {
                    // Perform simple inventory
                    NurApi.InventoryResponse ir = hNur.SimpleInventory();
                    // Did we find any Tag
                    if (ir.numTagsMem > 0)
                    {
                        // Yes we did
                        break;
                    }
                }
                // Fetch tags from module, including tag meta
                inventoriedTags = hNur.FetchTags(true);
            }

            // Search stongest Tag
            tag = null;
            int maxRssi = -128;

            for (int i = 0; i < inventoriedTags.Count; i++)
            {
                if (maxRssi < inventoriedTags[i].rssi)
                {
                    maxRssi = inventoriedTags[i].rssi;
                    tag     = inventoriedTags[i];
                }
            }

            // Return number of found tags
            return(inventoriedTags.Count);
        }
Exemple #20
0
        public bool WriteTagByUID(long uidTarget, long NewUID, bool checkOnlyOne, bool writeUserData)
        {
            long thisTagUID = 0;
            int  num        = 0;
            int  invIndex   = 0;

            try
            {
                hNur.ClearTags();
                NurApi.InventoryResponse response;          //Information about inventory store here
                response = hNur.SimpleInventory();          //Make Inventory..
                NurApi.TagStorage inv = hNur.FetchTags(true);
                NurApi.Tag        tag = null;
                if (checkOnlyOne)
                {
                    if (response.numTagsFound > 1)
                    {
                        throw new Exception("more than one tag found in the antena");
                    }
                }

                for (num = 0; num < inv.Count; num++)
                {
                    tag = inv[num];
                    byte[] EPCbytes = tag.ReadTag(0, false, EPCbank, 2, EPCbyteCount);
                    byte[] eUID     = new byte[6];
                    Array.Copy(EPCbytes, 0, eUID, 0, 6);
                    thisTagUID = convertByteArrayToLong(eUID);
                    if (thisTagUID == uidTarget)
                    {
                        break;                            // found in the antena
                    }
                }

                if (num >= inv.Count)
                {
                    throw new Exception("uidTarget not found in the antena");
                }

                //Verifica se o inventário já consta o novo UID:
                for (invIndex = 0; invIndex < _InventoryInfo.numTagsFound; invIndex++)
                {
                    if (_InventoryInfo.EPCuid[invIndex] == uidTarget || _InventoryInfo.EPCuid[invIndex] == NewUID)
                    {
                        break;
                    }
                }
                if (invIndex >= _InventoryInfo.numTagsFound)
                {
                    throw new Exception("uidTarget not found in 'inventoryInfo'");
                }

                // start writing
                byte[] EPC = new byte[EPCbyteCount];
                Array.Copy(convertLongToByteArray(_InventoryInfo.EPCuid[invIndex], 6), 0, EPC, 0, 6);
                Array.Copy(convertLongToByteArray(_InventoryInfo.EPCref[invIndex], 6), 0, EPC, 6, 6);
                Array.Copy(convertLongToByteArray(_InventoryInfo.EPCdate[invIndex], 2), 0, EPC, 12, 2);
                Array.Copy(convertLongToByteArray(_InventoryInfo.EPCcontrol[invIndex], 2), 0, EPC, 14, 2);
                tag.WriteTag(0, false, EPCbank, 2, EPC);

                if (writeUserData)
                {
                    hNur.ClearTags();
                    response = hNur.SimpleInventory();          //Make Inventory..
                    inv      = hNur.FetchTags(true);
                    tag      = null;
                    if (checkOnlyOne)
                    {
                        if (response.numTagsFound > 1)
                        {
                            throw new Exception("more than one tag found in the antena");
                        }
                    }

                    for (num = 0; num < inv.Count; num++)
                    {
                        tag = inv[num];
                        byte[] EPCbytes = tag.ReadTag(0, false, EPCbank, 2, EPCbyteCount);
                        byte[] eUID     = new byte[6];
                        Array.Copy(EPCbytes, 0, eUID, 0, 6);
                        thisTagUID = convertByteArrayToLong(eUID);
                        if (thisTagUID == uidTarget)
                        {
                            break;                            // found in the antena
                        }
                    }

                    if (num >= inv.Count)
                    {
                        throw new Exception("uidTarget not found in the antena");
                    }

                    for (invIndex = 0; invIndex < _InventoryInfo.numTagsFound; invIndex++)
                    {
                        if (_InventoryInfo.EPCuid[invIndex] == uidTarget)
                        {
                            break;                                                // found in the struct
                        }
                    }
                    if (invIndex >= _InventoryInfo.numTagsFound)
                    {
                        throw new Exception("uidTarget not found in 'inventoryInfo'");
                    }

                    byte[] USERDATA = new byte[USERbyteCount];
                    Array.Copy(convertLongToByteArray(_InventoryInfo.USERdata[invIndex], 4), 0, USERDATA, 0, 4);
                    tag.WriteTag(0, false, USERbank, 0, USERDATA);
                }

                return(true);
            }
            catch (Exception ex)
            {
                _Error = "Error writting in RFID tag " + ex.Message;
                return(false);
            }
        }