Ejemplo n.º 1
0
//        static void Main(string[] args)
//        {
//            //Console.WriteLine("Attached debugger and press a key");
//            //while (!Console.KeyAvailable)
//            //    ;
//            //Console.ReadKey();

//            Pn532 pn532 = null;
//            if (args?.Length > 0)
//            {
//                if (args[0] == "-c")
//                {
//                    string device = "/dev/ttyS0";
//                    pn532 = new Pn532(device);
//                }
//                else if (args[0] == "-i")
//                {
//                    I2cConnectionSettings connectionString = new I2cConnectionSettings(1, Pn532.I2cDefaultAddress);
//                    var device = I2cDevice.Create(connectionString);
//                    pn532 = new Pn532(device);
//                }
//                else if (args[0] == "-s")
//                {
//                    var settings = new SpiConnectionSettings(0, 0)
//                    {
//                        ClockFrequency = 2_000_000,
//                        Mode = SpiMode.Mode0,
//                        ChipSelectLineActiveState = PinValue.Low,
//                        //    DataFlow = DataFlow.LsbFirst
//                    };
//                    SpiDevice device = SpiDevice.Create(settings);
//                    pn532 = new Pn532(device);
//                }

//            }
//            else
//            {
//                string device = "COM7";
//                //pn532 = new Pn532(device, LogLevel.Debug);
//                pn532 = new Pn532(device, LogLevel.None);
//            }

//#if DEBUG
//            //pn532.LogLevel = LogLevel.Debug;
//#endif
//            var version = pn532.FirmwareVersion;
//            if (version != null)
//            {
//                Console.WriteLine($"Is it a PN532!: {version.IsPn532}, Version: {version.Version}, Version supported: {version.VersionSupported}");
//                //pn532.SetSerialBaudRate(BaudRate.B0921600);

//                //DumpAllRegisters(pn532);
//                //ReadMiFare(pn532);
//                //FixRegister(pn532);
//                //pn532.SetAnalog106kbpsTypeA(new Analog106kbpsTypeAMode() { CIU_DemodWhenRfOn = 0x4D,  CIU_TxBitPhase = 0x83 });
//                //pn532.SetAnalogTypeB(new AnalogSettingsTypeBMode() { CIU_GsNOn = 0xFF, CIU_RxThreshold = 0x85, CIU_ModGsP = 0x10});

//                ReadCreditCard(pn532);
//                //ReadTypeB(pn532);
//                //TestMiFareAccess(pn532);
//                //AsTarget(pn532);
//            }
//            else
//                Console.WriteLine($"Error");

//        }

        static void AsTarget(Pn532 pn532)
        {
            byte[] retData = null;
            TargetModeInitialized modeInitialized = null;

            while ((!Console.KeyAvailable))
            {
                (modeInitialized, retData) = pn532.InitAsTarget(
                    TargetModeInitialization.PiccOnly,
                    new TargetMifareParameters()
                {
                    Atqa = new byte[] { 0x08, 0x00 }, Sak = 0x60
                },
                    new TargetFeliCaParameters()
                {
                    NfcId2 = new byte[] { 0x01, 0xFE, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7 }, Pad = new byte[] { 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7 }
                },
                    new TargetPiccParameters()
                {
                    NfcId3 = new byte[] { 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, GeneralTarget = new byte[0], HistoricalTarget = new byte[0]
                });
                if (modeInitialized != null)
                {
                    break;
                }
                // Give time to PN532 to process
                Thread.Sleep(200);
            }
            if (modeInitialized == null)
            {
                return;
            }

            Console.WriteLine($"PN532 as a target: ISDep: {modeInitialized.IsDep}, IsPicc {modeInitialized.IsISO14443_4Picc}, {modeInitialized.TargetBaudRate}, {modeInitialized.TargetFramingType}");
            Console.WriteLine($"Initiator: {BitConverter.ToString(retData)}");
            // 25-D4-00-E8-11-6A-0A-69-1C-46-5D-2D-7C-00-00-00-32-46-66-6D-01-01-12-02-02-07-FF-03-02-00-13-04-01-64-07-01-03
            // 11-D4-00-01-FE-A2-A3-A4-A5-A6-A7-00-00-00-00-00-30
            // E0-80

            Span <byte> read = stackalloc byte[512];
            int         ret  = -1;

            while (ret < 0)
            {
                ret = pn532.ReadDataAsTarget(read);
            }

            // For example: 00-00-A4-04-00-0E-32-50-41-59-2E-53-59-53-2E-44-44-46-30-31-00
            Console.WriteLine($"Status: {read[0]}, Data: {BitConverter.ToString(read.Slice(1).ToArray())}");
        }