如何使用Impinj高速公路仅对RFID UHF标签进行一次编码

时间:2016-11-03 10:28:14

标签: c# sdk rfid octane-sdk

我使用ocatne SDK C#和impinj speedway R420编码RFID UHF gen2标签。

我使用SDK的样本写入标签并且它正在工作,我的问题在我希望编码连续时开始,我将解释。

我想在阅读器字段中放置一个标签对其进行编码并将其删除,然后等待第二个标签对其进行编码等等....

我想把搜索模式设置为' Single Target'并使用Session 2,但它只是读取标签而不是编码它,我从来没有得到编码seq来运行。

任何其他选项它只是保持编码和编码非常快,我不知道何时删除标签,以便我可以控制编码数据。

我的代码在这里:

using System;
using System.Threading;
using Impinj.OctaneSdk;
using System.Windows.Threading;


namespace OctaneSdkExamples
{
    class Program
    {
        // Create an instance of the ImpinjReader class.
        static ImpinjReader reader = new ImpinjReader();

        const ushort EPC_OP_ID = 123;
        const ushort PC_BITS_OP_ID = 321;

        static Random random = new Random((int)DateTime.Now.Ticks);

        public static void DelayAction(int millisecond)
        {
            var timer = new DispatcherTimer();
            timer.Tick += delegate

            {
                timer.Stop();
            };

            timer.Interval = TimeSpan.FromMilliseconds(millisecond);
            timer.Start();
        }

        static string GetRandomEpc()
        {
            string epc = "";
            int numWords = random.Next(1, 7);

            for (int i = 0; i < numWords; i++)
                epc += random.Next(0, ushort.MaxValue + 1).ToString("X4");

            return epc;
        }

        static void ProgramEpc(string currentEpc, ushort currentPcBits, string newEpc)
        {
            // Check that the specified EPCs are a valid length
            if ((currentEpc.Length % 4 != 0) || (newEpc.Length % 4 != 0))
                throw new Exception("EPCs must be a multiple of 16 bits (4 hex chars)");

            Console.WriteLine("\nAdding a write operation to change the EPC from :");
            Console.WriteLine("{0} to {1}\n", currentEpc, newEpc);

            // Create a tag operation sequence.
            // You can add multiple read, write, lock, kill and QT
            // operations to this sequence.
            TagOpSequence seq = new TagOpSequence();

            // Specify a target tag based on the EPC.
            seq.TargetTag.MemoryBank = MemoryBank.Epc;
            seq.TargetTag.BitPointer = BitPointers.Epc;
            seq.TargetTag.Data = currentEpc;

            // If you are using Monza 4, Monza 5 or Monza X tag chips,
            // uncomment these two lines. This enables 32-bit block writes
            // which significantly improves write performance.
            //seq.BlockWriteEnabled = true;
            //seq.BlockWriteWordCount = 2;

            // Create a tag write operation to change the EPC.
            TagWriteOp writeEpc = new TagWriteOp();
            // Set an ID so we can tell when this operation has executed.
            writeEpc.Id = EPC_OP_ID;
            // Write to EPC memory
            writeEpc.MemoryBank = MemoryBank.Epc;
            // Specify the new EPC data
            writeEpc.Data = TagData.FromHexString(newEpc);
            // Starting writing at word 2 (word 0 = CRC, word 1 = PC bits)
            writeEpc.WordPointer = WordPointers.Epc;

            // Add this tag write op to the tag operation sequence.
            seq.Ops.Add(writeEpc);

            // Is the new EPC a different length than the current EPC?
            if (currentEpc.Length != newEpc.Length)
            {
                // We need adjust the PC bits and write them back to the 
                // tag because the length of the EPC has changed.

                // Adjust the PC bits (4 hex characters per word).
                ushort newEpcLenWords = (ushort)(newEpc.Length / 4);
                ushort newPcBits = PcBits.AdjustPcBits(currentPcBits, newEpcLenWords);

                Console.WriteLine("Adding a write operation to change the PC bits from :");
                Console.WriteLine("{0} to {1}\n", currentPcBits.ToString("X4"), newPcBits.ToString("X4"));

                TagWriteOp writePc = new TagWriteOp();
                writePc.Id = PC_BITS_OP_ID;
                // The PC bits are in the EPC memory bank.
                writePc.MemoryBank = MemoryBank.Epc;
                // Specify the data to write (the modified PC bits).
                writePc.Data = TagData.FromWord(newPcBits);
                // Start writing at the start of the PC bits.
                writePc.WordPointer = WordPointers.PcBits;

                // Add this tag write op to the tag operation sequence.
                seq.Ops.Add(writePc);

            }

            // Add the tag operation sequence to the reader.
            // The reader supports multiple sequences.
            reader.AddOpSequence(seq);
        }

        static void Main(string[] args)
        {
            try
            {

                reader.Connect("10.0.1.201");

                // Assign the TagsReported event handler.
                // This specifies which method to call
                // when tags reports are available.
                reader.TagsReported += OnTagsReported;

                reader.TagOpComplete += OnTagOpComplete;

                Settings settings = reader.QueryDefaultSettings();

                settings.Report.IncludeAntennaPortNumber = true;
                settings.Report.IncludePcBits = true;
                settings.ReaderMode = ReaderMode.AutoSetDenseReader;
                settings.Antennas.DisableAll();
                settings.Antennas.GetAntenna(1).IsEnabled = true;
                settings.Antennas.GetAntenna(1).MaxRxSensitivity = true;
                settings.Antennas.GetAntenna(1).TxPowerInDbm = 10;
                settings.SearchMode = SearchMode.SingleTarget;
                settings.Session =2;
                settings.TagPopulationEstimate = 1;

                reader.ApplySettings(settings);
                // Start reading.
                reader.Start();

                // Wait for the user to press enter.
                Console.WriteLine("Press enter to exit.");
                Console.ReadLine();

                // Stop reading.
                reader.Stop();

                // Disconnect from the reader.
                reader.Disconnect();
            }
            catch (OctaneSdkException e)
            {
                // Handle Octane SDK errors.
                Console.WriteLine("Octane SDK exception: {0}", e.Message);
            }
            catch (Exception e)
            {
                // Handle other .NET errors.
                Console.WriteLine("Exception : {0}", e.Message);
            }
        }

        static 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.
            reader.TagsReported -= OnTagsReported;
            Tag tag = report.Tags[0];
            ProgramEpc(tag.Epc.ToHexString(), tag.PcBits, GetRandomEpc());

        }

        static void OnTagOpComplete(ImpinjReader reader, TagOpReport report)
        {
            // Loop through all the completed tag operations.
            foreach (TagOpResult result in report)
            {
                // Was this completed operation a tag write operation?
                if (result is TagWriteOpResult)
                {
                    // Cast it to the correct type.
                    TagWriteOpResult writeResult = result as TagWriteOpResult;
                    if (writeResult.OpId == EPC_OP_ID)
                        Console.WriteLine("Write to EPC complete : {0}", writeResult.Result);
                    else if (writeResult.OpId == PC_BITS_OP_ID)
                        Console.WriteLine("Write to PC bits complete : {0}", writeResult.Result);

                    // Print out the number of words written
                    Console.WriteLine("Number of words written : {0}", writeResult.NumWordsWritten);


                }
            }
            //DelayAction(3000);
            //Thread.Sleep(3000);
            reader.TagsReported += OnTagsReported;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

当您使用会话2和单个目标时,标记只会响应一次,直到它超出该字段超过一定的时间。这意味着当你阅读它,然后尝试编程它时,它就不再响应了。因此,在此设置中必须使用双目标。

要确定您是否已经编写了特定标签,请跟踪标签的TID:对于大多数标签,这包含在IC生产中编程的唯一序列号,即使您更改EPC也不会改变。您可以在字段中输入要标记的新标记。