private static void ReadCard() { Console.Clear(); "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]); 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; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); // Select the scanned tag device.SelectCardUniqueId(cardUid); // Authenticate sector if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, 19) == RFIDControllerMfrc522.Status.AllOk) { var data = device.CardReadData(16); var text = Encoding.ASCII.GetString(data.Data); $" Message read: {text}".Info(); } else { "Authentication error".Error(); } device.ClearCardSelection(); ExitMessage.WriteLine(); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } break; } }
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(); } } } }
private static void ReadAllCardSectors() { Console.Clear(); "Testing RFID".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[18]); 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; // Print UID $"Card UID: {cardUid[0]},{cardUid[1]},{cardUid[2]},{cardUid[3]}".Info(); // Select the scanned tag device.SelectCardUniqueId(cardUid); // Reading data var continueReading = true; for (var 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 (var 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, sector {s}".Error(); } } device.ClearCardSelection(); Terminal.WriteLine(ExitMessage); while (true) { var input = Console.ReadKey(true).Key; if (input != ConsoleKey.Escape) { continue; } break; } break; } }
public static (bool, bool, string) ReadTagFromRC522() { if (_initDone == false) { Init(); } string result = string.Empty; bool cardDetected = _nfcReader.DetectCard() == RFIDControllerMfrc522.Status.AllOk; bool readDone = 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 byteBlocksToRead = 7; // 7 * 16 bytes = 112 bytes int byteBlocksRead = 0; try { // Read data from sectors byte blockAdress = 4; for (int i = 0; i < byteBlocksToRead; i++) { if (_authOn && _nfcReader.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, cardUid, (byte)(blockAdress + 3)) != RFIDControllerMfrc522.Status.AllOk) { //Console.WriteLine("Authentication error"); } var nfcResponse = _nfcReader.CardReadData(blockAdress); result = result + Encoding.Default.GetString(nfcResponse.Data); blockAdress += 4; if (nfcResponse.Status == RFIDControllerMfrc522.Status.AllOk) { byteBlocksRead++; } } if (byteBlocksRead == byteBlocksToRead) { Console.WriteLine("Read done"); readDone = true; } else { Console.WriteLine("Read error"); } Console.WriteLine($"{byteBlocksRead} of {byteBlocksToRead} read"); } finally { _nfcReader.ClearCardSelection(); } } } return(cardDetected, readDone, result); }
private async Task Run() { "Idle".Info(); var device = new RFIDControllerMfrc522(Pi.Spi.Channel0, 500000, Pi.Gpio[22]); 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 uid = uidResponse.Data; device.SelectCardUniqueId(uid); // Authentication if (device.AuthenticateCard1A(RFIDControllerMfrc522.DefaultAuthKey, uid, 19) == RFIDControllerMfrc522.Status.AllOk) { var data = device.CardReadData(16); var secretText = Encoding.ASCII.GetString(data.Data); if (secretText == "HelloHelloHellos") { // Card with access "Access granted!".Info(); ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio22], -1); await _unitOfWork.Logs.AddAsync(new Log { Uid = uid, AttemptType = AttemptType.Success, Message = "Door has been successfully unlocked." }).ConfigureAwait(false); await _doorService.UpdateDoorStateAsync(DoorStatus.Open).ConfigureAwait(false); _access = true; //Wait until button is pressed. while (!_readAgain) { await Task.Delay(500).ConfigureAwait(false); } _readAgain = false; ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio22], 0); await _doorService.UpdateDoorStateAsync(DoorStatus.Closed).ConfigureAwait(false); } else { // Card without access "Access denied!".Info(); ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio18], 3000); await _unitOfWork.Logs.AddAsync(new Log { AttemptType = AttemptType.Fail, Message = "Card does not have permission to unlock door!" }).ConfigureAwait(false); } } else { // Unknown Card ControlLed.BlinkLed(Pi.Gpio[BcmPin.Gpio18], 3000); await _unitOfWork.Logs.AddAsync(new Log { AttemptType = AttemptType.UnkownUid, Message = "Authentication error, can't read card!" }).ConfigureAwait(false); } device.ClearCardSelection(); await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); } }