Beispiel #1
0
        private static void WriteCard()
        {
            Console.Clear();
            "Testing RFID".Info();

            var device    = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]);
            var userInput = Terminal.ReadLine("Insert a message to be written in the card (16 characters only)").Truncate(16);

            "Place the card on the sensor".Info();

            while (true)
            {
                // If a card is found
                if (device.DetectCard() != RFIDControllerMfrc522.Status.AllOk)
                {
                    continue;
                }

                // Get the UID of the card
                var uidResponse = device.ReadCardUniqueId();

                // If we have the UID, continue
                if (uidResponse.Status != RFIDControllerMfrc522.Status.AllOk)
                {
                    continue;
                }

                var cardUid = uidResponse.Data;

                // Select the scanned tag
                device.SelectCardUniqueId(cardUid);

                // Writing data to sector 1 blocks
                // Authenticate sector
                if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, 19) == RFIDControllerMfrc522.Status.AllOk)
                {
                    userInput = (userInput + new string(' ', 16)).Truncate(16);
                    device.CardWriteData(16, Encoding.ASCII.GetBytes(userInput));
                }

                device.ClearCardSelection();
                "Data has been written".Info();

                Terminal.WriteLine(ExitMessage);

                while (true)
                {
                    var input = Console.ReadKey(true).Key;
                    if (input != ConsoleKey.Escape)
                    {
                        continue;
                    }

                    break;
                }

                break;
            }
        }
Beispiel #2
0
        private static void TestRfidController()
        {
            "Testing RFID".Info();
            var device = new RFIDControllerMfrc522(Pi.Spi.Channel1, 500000, Pi.Gpio[18]);

            while (true)
            {
                // If a card is found
                if (device.DetectCard() == RFIDControllerMfrc522.Status.AllOk)
                {
                    "Card detected".Info();

                    // Get the UID of the card
                    var uidResponse = device.ReadCardUniqueId();

                    // If we have the UID, continue
                    if (uidResponse.Status == RFIDControllerMfrc522.Status.AllOk)
                    {
                        var cardUid = uidResponse.Data;

                        // Print UID
                        $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info();

                        // Select the scanned tag
                        device.SelectCardUniqueId(cardUid);

                        // Writing data to sector 1 blocks
                        // Authenticate sector
                        if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, 7) == RFIDControllerMfrc522.Status.AllOk)
                        {
                            var data = new byte[16 * 3];
                            for (var x = 0; x < data.Length; x++)
                            {
                                data[x] = (byte)(x + 65);
                            }

                            for(int b = 0; b < 3; b++)
                            {
                                device.CardWriteData((byte)(4 + b), data.Skip(b * 16).Take(16).ToArray());
                            }
                        }

                        // Reading data
                        var continueReading = true;
                        for (int s = 0; s < 16 && continueReading; s++)
                        {
                            // Authenticate sector
                            if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, (byte)((4 * s) + 3)) == RFIDControllerMfrc522.Status.AllOk)
                            {
                                $"Sector {s}".Info();
                                for (int b = 0; b < 3 && continueReading; b++)
                                {
                                    var data = device.CardReadData((byte)((4 * s) + b));
                                    if (data.Status != RFIDControllerMfrc522.Status.AllOk)
                                    {
                                        continueReading = false;
                                        break;
                                    }

                                    $"  Block {b} ({data.Data.Length} bytes): {string.Join(" ", data.Data.Select(x => x.ToString("X2")))}".Info();
                                }
                            }
                            else
                            {
                                "Authentication error".Error();
                                break;
                            }
                        }

                        device.ClearCardSelection();
                    }
                }
            }
        }
Beispiel #3
0
        internal static (bool, bool) WriteTagRC522(string nfcNewDataContent)
        {
            if (_initDone == false)
            {
                Init();
            }

            string result       = string.Empty;
            bool   cardDetected = _nfcReader.DetectCard() == RFIDControllerMfrc522.Status.AllOk;
            bool   writeDone    = false;

            if (cardDetected)
            {
                var uidResponse = _nfcReader.ReadCardUniqueId();
                if (uidResponse.Status == RFIDControllerMfrc522.Status.AllOk)
                {
                    var cardUid = uidResponse.Data;
                    //PrintUid(cardUid, uidResponse.DataBitLength);
                    _nfcReader.SelectCardUniqueId(cardUid);
                    int byteBlocksToWrite = 7; // 7 * 16 bytes = 112 bytes
                    int byteBlocksWritten = 0;

                    try
                    {
                        // Write data to sectors
                        byte blockAdress = 4;

                        for (int i = 0; i < byteBlocksToWrite; i++)
                        {
                            if (_authOn && _nfcReader.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, (byte)(blockAdress + 3)) != RFIDControllerMfrc522.Status.AllOk)
                            {
                                //Console.WriteLine("Authentication error");
                            }
                            string data = string.Concat(nfcNewDataContent.Skip(i * 16).Take(16));
                            data = (data + new string(' ', 16)).Truncate(16);
                            var writeStatus = _nfcReader.CardWriteData(blockAdress, Encoding.ASCII.GetBytes(data));

                            Console.WriteLine($"Status: {writeStatus}");
                            if (writeStatus == RFIDControllerMfrc522.Status.AllOk)
                            {
                                byteBlocksWritten++;
                            }

                            blockAdress += 4;
                        }

                        if (byteBlocksWritten == byteBlocksToWrite)
                        {
                            Console.WriteLine("Write done");
                            writeDone = true;
                        }
                        else
                        {
                            Console.WriteLine("Write error");
                        }
                        Console.WriteLine($"{byteBlocksWritten} of {byteBlocksToWrite} written");
                    }
                    finally
                    {
                        _nfcReader.ClearCardSelection();
                    }
                }
            }
            return(cardDetected, writeDone);
        }