示例#1
0
        /// <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);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        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);
        }
示例#4
0
        /// <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));
        }
示例#5
0
文件: MFRC522.cs 项目: andremn/SPS
        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);
        }
示例#6
0
文件: MFRC522.cs 项目: andremn/SPS
        /// <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;
        }
示例#7
0
文件: MFRC522.cs 项目: andremn/SPS
        /// <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);
        }
示例#8
0
文件: MFRC522.cs 项目: andremn/SPS
        /// <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;
        }
示例#9
0
 /// <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));
     }
 }
示例#10
0
 public async Task SendTagToServerAsync(TagUid tag)
 {
     await SendAuthRequestAsync(new AuthRequest() { TagId = tag.ToString(), CarPlate = "", ParkingCNPJ = "46.451.585/0001-67" });
 }