/// <summary> /// Writes a block of data to the MFRC522 reader. /// </summary> /// <param name="blockNumber">The number of the block to write data to.</param> /// <param name="uid">The MFRC522 tag to write data to.</param> /// <param name="keyA">The first key to use while writing.</param> /// <param name="keyB">The second key to use while writing.</param> /// <returns>true if the data was written; false otherwise.</returns> public bool WriteBlock(byte blockNumber, TagUid uid, byte[] data, byte[] keyA = null, byte[] keyB = null) { if (keyA != null) { Authenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA); } else if (keyB != null) { Authenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB); } else { return(false); } //Write block Transceive(true, PiccCommands.Write, blockNumber); if (ReadFromFifo() != PiccResponses.Acknowledge) { return(false); } //Make sure we write only 16 bytes var buffer = new byte[MaxBlockLength]; data.CopyTo(buffer, 0); Transceive(true, buffer); return(ReadFromFifo() == PiccResponses.Acknowledge); }
/// <summary> /// Selects a MFRC522 tag. /// </summary> /// <param name="uid">The tag to be selected.</param> /// <returns>true if the specified tag was selected; false otherwise.</returns> public bool SelectTag(TagUid uid) { //Send Select command to tag var data = new byte[7]; data[0] = PiccCommands.Select1; data[1] = PiccCommands.Select2; uid.FullUid.CopyTo(data, 2); Transceive(true, data); return(GetFifoLevel() == 1 && ReadFromFifo() == PiccResponses.SelectAcknowledge); }
private void Authenticate(byte command, byte blockNumber, TagUid uid, [ReadOnlyArray] byte[] key) { //Put reader in Idle mode WriteRegister(MFRC522Registers.Command, PcdCommands.Idle); //Clear the FIFO SetRegisterBits(MFRC522Registers.FifoLevel, 0x80); //Create Authentication packet var data = new byte[AuthenticationPacketLength]; data[0] = command; data[1] = (byte)(blockNumber & 0xFF); key.CopyTo(data, 2); uid.Bytes.CopyTo(data, 8); WriteToFifo(data); //Put reader in MfAuthent mode WriteRegister(MFRC522Registers.Command, PcdCommands.Authenticate); }
/// <summary> /// Reads a block of data from the MFRC522 reader. /// </summary> /// <param name="blockNumber">The number of the block to read data from.</param> /// <param name="uid">The MFRC522 tag to read data from.</param> /// <param name="keyA">The first key to use while reading.</param> /// <param name="keyB">The second key to use while reading.</param> /// <returns>The data read.</returns> public byte[] ReadBlock(byte blockNumber, TagUid uid, byte[] keyA = null, byte[] keyB = null) { if (keyA != null) { Authenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA); } else if (keyB != null) { Authenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB); } else { return(null); } //Read block Transceive(true, PiccCommands.Read, blockNumber); return(ReadFromFifo(MaxBlockLength)); }
/// <summary> /// Writes a block of data to the MFRC522 reader. /// </summary> /// <param name="blockNumber">The number of the block to write data to.</param> /// <param name="uid">The MFRC522 tag to write data to.</param> /// <param name="keyA">The first key to use while writing.</param> /// <param name="keyB">The second key to use while writing.</param> /// <returns>true if the data was written; false otherwise.</returns> public bool WriteBlock(byte blockNumber, TagUid uid, byte[] data, byte[] keyA = null, byte[] keyB = null) { if (keyA != null) { Authenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA); } else if (keyB != null) { Authenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB); } else { return false; } //Write block Transceive(true, PiccCommands.Write, blockNumber); if (ReadFromFifo() != PiccResponses.Acknowledge) { return false; } //Make sure we write only 16 bytes var buffer = new byte[MaxBlockLength]; data.CopyTo(buffer, 0); Transceive(true, buffer); return ReadFromFifo() == PiccResponses.Acknowledge; }
/// <summary> /// Reads a block of data from the MFRC522 reader. /// </summary> /// <param name="blockNumber">The number of the block to read data from.</param> /// <param name="uid">The MFRC522 tag to read data from.</param> /// <param name="keyA">The first key to use while reading.</param> /// <param name="keyB">The second key to use while reading.</param> /// <returns>The data read.</returns> public byte[] ReadBlock(byte blockNumber, TagUid uid, byte[] keyA = null, byte[] keyB = null) { if (keyA != null) { Authenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA); } else if (keyB != null) { Authenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB); } else { return null; } //Read block Transceive(true, PiccCommands.Read, blockNumber); return ReadFromFifo(MaxBlockLength); }
/// <summary> /// Selects a MFRC522 tag. /// </summary> /// <param name="uid">The tag to be selected.</param> /// <returns>true if the specified tag was selected; false otherwise.</returns> public bool SelectTag(TagUid uid) { //Send Select command to tag var data = new byte[7]; data[0] = PiccCommands.Select1; data[1] = PiccCommands.Select2; uid.FullUid.CopyTo(data, 2); Transceive(true, data); return GetFifoLevel() == 1 && ReadFromFifo() == PiccResponses.SelectAcknowledge; }
/// <summary> /// Raises the <see cref="StartPageLogic.NewResponse"/> event. /// </summary> /// <param name="tagUid">The response to be included in the event data.</param> private void RaiseNewTagEvent(TagUid tagUid) { if (TagPresent != null) { TagPresent(this, new TagPresentEventArgs(tagUid)); } }
public async Task SendTagToServerAsync(TagUid tag) { await SendAuthRequestAsync(new AuthRequest() { TagId = tag.ToString(), CarPlate = "", ParkingCNPJ = "46.451.585/0001-67" }); }