public static bool WriteTagMemory(uint address, byte[] data, string uid, Reader reader)
        {
            bool flag = false;

            byte[]      _uid    = ByteFormat.FromHex(uid);
            TagFilter   filter  = new Select_UID((byte)(_uid.Length * 8), _uid);
            MemoryType  type    = MemoryType.BLOCK_MEMORY;
            WriteMemory writeOp = new WriteMemory(type, address, data);

            try
            {
                reader.ExecuteTagOp(writeOp, filter);
                flag = true;
            }
            catch (Exception)
            {
                flag = false;
                //throw;
            }
            return(flag);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // Program setup
            if (1 > args.Length)
            {
                Usage();
            }

            for (int nextarg = 1; nextarg < args.Length; nextarg++)
            {
                string arg = args[nextarg];
                if (arg.Equals("--ant"))
                {
                    if (null != antennaList)
                    {
                        Console.WriteLine("Duplicate argument: --ant specified more than once");
                        Usage();
                    }
                    antennaList = ParseAntennaList(args, nextarg);
                    nextarg++;
                }
                else
                {
                    Console.WriteLine("Argument {0}:\"{1}\" is not recognized", nextarg, arg);
                    Usage();
                }
            }

            try
            {
                // Create Reader object, connecting to physical device.
                // Wrap reader in a "using" block to get automatic
                // reader shutdown (using IDisposable interface).
                using (r = Reader.Create(args[0]))
                {
                    //Uncomment this line to add default transport listener.
                    //r.Transport += r.SimpleTransportListener;

                    r.Connect();
                    if (Reader.Region.UNSPEC == (Reader.Region)r.ParamGet("/reader/region/id"))
                    {
                        Reader.Region[] supportedRegions = (Reader.Region[])r.ParamGet("/reader/region/supportedRegions");
                        if (supportedRegions.Length < 1)
                        {
                            throw new FAULT_INVALID_REGION_Exception();
                        }
                        r.ParamSet("/reader/region/id", supportedRegions[0]);
                    }
                    string model = (string)r.ParamGet("/reader/version/model").ToString();
                    if (!model.Equals("M3e"))
                    {
                        if (r.isAntDetectEnabled(antennaList))
                        {
                            Console.WriteLine("Module doesn't has antenna detection support please provide antenna list");
                            Usage();
                        }
                        //Use first antenna for operation
                        if (antennaList != null)
                        {
                            r.ParamSet("/reader/tagop/antenna", antennaList[0]);
                        }
                    }
                    else
                    {
                        if (antennaList != null)
                        {
                            Console.WriteLine("Module doesn't support antenna input");
                            Usage();
                        }
                    }

                    // This select filter matches all Gen2 tags where bits 32-48 of the EPC are 0x0123
#if ENABLE_FILTER
                    TagFilter filter = new Gen2.Select(false, Gen2.Bank.EPC, 32, 16, new byte[] { 0x01, 0x23 });
#endif
                    if (!model.Equals("M3e"))
                    {
                        //Gen2.TagData epc = new Gen2.TagData(new byte[] {
                        //    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
                        //    0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67,
                        //});
                        //Gen2.WriteTag tagop = new Gen2.WriteTag(epc);
                        //r.ExecuteTagOp(tagop, null);
                    }

                    // Reads data from a tag memory bank after writing data to the requested memory bank without powering down of tag
#if ENABLE_READ_AFTER_WRITE
                    {
                        //create a tagopList with write tagop followed by read tagop
                        TagOpList tagopList = new TagOpList();
                        byte      wordCount;
                        ushort[]  readData;

                        //Write one word of data to USER memory and read back 8 words from EPC memory using WriteData and ReadData
                        {
                            ushort[] writeData = { 0x9999 };
                            wordCount = 8;
                            Gen2.WriteData wData = new Gen2.WriteData(Gen2.Bank.USER, 2, writeData);
                            Gen2.ReadData  rData = new Gen2.ReadData(Gen2.Bank.EPC, 0, wordCount);
                            //Gen2.WriteTag wTag = new Gen2.WriteTag(epc);

                            // assemble tagops into list
                            tagopList.list.Add(wData);
                            tagopList.list.Add(rData);

                            Console.WriteLine("###################Embedded Read after write######################");
                            // uncomment the following for embedded read after write.
                            embeddedRead(TagProtocol.GEN2, null, tagopList);

                            // call executeTagOp with list of tagops
                            //readData = (ushort[])r.ExecuteTagOp(tagopList, null);
                            //Console.WriteLine("ReadData: ");
                            //foreach (ushort word in readData)
                            //{
                            //    Console.Write(" {0:X4}", word);
                            //}
                            //Console.WriteLine("\n");
                        }

                        //clearing the list for next operation
                        tagopList.list.Clear();

                        //Write 12 bytes(6 words) of EPC and read back 8 words from EPC memory using WriteTag and ReadData
                        {
                            Gen2.TagData epc1 = new Gen2.TagData(new byte[] {
                                0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
                                0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
                            });
                            wordCount = 8;
                            Gen2.WriteTag wtag  = new Gen2.WriteTag(epc1);
                            Gen2.ReadData rData = new Gen2.ReadData(Gen2.Bank.EPC, 0, wordCount);

                            // assemble tagops into list
                            tagopList.list.Add(wtag);
                            tagopList.list.Add(rData);

                            // call executeTagOp with list of tagops
                            //readData = (ushort[])r.ExecuteTagOp(tagopList, null);
                            //Console.WriteLine("ReadData: ");
                            //foreach (ushort word in readData)
                            //{
                            //    Console.Write(" {0:X4}", word);
                            //}
                            //Console.WriteLine("\n");
                        }
                    }
#endif

                    // Perform read and print UID and tagtype of tag found
                    SimpleReadPlan plan = new SimpleReadPlan(antennaList, TagProtocol.ISO15693, null, null, 1000);
                    r.ParamSet("/reader/read/plan", plan);
                    TagReadData[] tagReads = r.Read(1000);
                    Console.WriteLine("UID: " + tagReads[0].EpcString);
                    Console.WriteLine("TagType:  " + (Iso15693.TagType)(tagReads[0].TagType));
                    MemoryType  type;
                    MultiFilter mulfilter = null;
                    UInt32      address;
                    byte        length;

#if ENABLE_M3E_FILTER
                    //Initialize filter
                    // Filters the tag based on tagtype
                    TagFilter tagTypeFilter = new Select_TagType((UInt64)((Iso15693.TagType)(tagReads[0].TagType)));
                    // Filters the tag based on UID
                    TagFilter uidFilter = new Select_UID(32, ByteFormat.FromHex(tagReads[0].Tag.EpcString.Substring(0, 8)));
                    // Initialize multi filter
                    mulfilter = new MultiFilter(new TagFilter[] { tagTypeFilter, uidFilter });
#endif

#if ENABLE_M3E_BLOCK_READ_WRITE
                    //Initialize all the fields required for Read Data Tag operation
                    type    = MemoryType.BLOCK_MEMORY;
                    address = 0;
                    length  = 1;

                    // Read memory before write
                    ReadMemory bRead    = new ReadMemory(type, address, length);
                    byte[]     dataRead = (byte[])r.ExecuteTagOp(bRead, mulfilter);

                    // prints the data read
                    Console.WriteLine("Read Data before performing block write: ");
                    foreach (byte i in dataRead)
                    {
                        Console.Write(" {0:X2}", i);
                    }
                    Console.WriteLine("\n");
                    // Uncomment this to enable Embedded read memory
                    //embeddedRead(TagProtocol.ISO15693, mulfilter, bRead);

                    // Initialize write memory
                    byte[]      data    = new byte[] { 0x11, 0x22, 0x33, 0x44 };
                    WriteMemory writeOp = new WriteMemory(type, address, data);

                    // Execute the tagop
                    r.ExecuteTagOp(writeOp, mulfilter);
                    // Uncomment this to enable Embedded write data
                    //embeddedRead(TagProtocol.ISO15693, mulfilter, writeOp);

                    //Read memory after block write
                    ReadMemory readOp   = new ReadMemory(type, address, length);
                    byte[]     readData = (byte[])r.ExecuteTagOp(readOp, mulfilter);

                    // prints the data read
                    Console.WriteLine("Read Data after performing block write operation: ");
                    foreach (byte i in readData)
                    {
                        Console.Write(" {0:X2}", i);
                    }
                    Console.WriteLine("\n");
#endif

#if ENABLE_M3E_SYSTEM_INFORMATION_MEMORY
                    //Get the system information of tag. Address and length fields have no significance if memory type is BLOCK_SYSTEM_INFORMATION_MEMORY.
                    type    = MemoryType.BLOCK_SYSTEM_INFORMATION_MEMORY;
                    address = 0;
                    length  = 0;
                    ReadMemory sysInfoOp  = new ReadMemory(type, address, length);
                    byte[]     systemInfo = (byte[])r.ExecuteTagOp(sysInfoOp, mulfilter);

                    // parsing the system info response
                    if (systemInfo.Length > 0)
                    {
                        parseGetSystemInfoResponse(systemInfo);
                    }
#endif

#if ENABLE_M3E_SECURE_ID
                    // Read secure id of tag. Address and length fields have no significance if memory type is SECURE_ID.
                    type    = MemoryType.SECURE_ID;
                    address = 0;
                    length  = 0;
                    ReadMemory secureIdOp = new ReadMemory(type, address, length);
                    byte[]     rspData    = (byte[])r.ExecuteTagOp(secureIdOp, mulfilter);

                    // parse secure id operation response.
                    if (rspData.Length > 0)
                    {
                        parseSecureIdResponse(rspData);
                    }
#endif

#if ENABLE_M3E_BLOCK_PROTECTION_STATUS
                    // Get the block protection status of block 0.
                    type    = MemoryType.BLOCK_PROTECTION_STATUS_MEMORY;
                    address = 0;
                    length  = 1;
                    ReadMemory blkProtectionOp = new ReadMemory(type, address, length);
                    byte[]     statusData      = (byte[])r.ExecuteTagOp(blkProtectionOp, mulfilter);

                    // parse the block protection status response.
                    if (statusData.Length == length)
                    {
                        parseGetBlockProtectionStatusResponse(statusData, address, length);
                    }
#endif
                }
            }
            catch (ReaderException re)
            {
                Console.WriteLine("Error: " + re.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
        }