Beispiel #1
0
        private byte[] ReadPassiveTargetId(byte baudrate, int timeout)
        {
            byte[] packet =
            {
                CommandInlistpassivetarget,
                1, // Activate only one target (max 2)
                baudrate
            };

            if (!SendCommandCheckAck(packet, timeout))
            {
                return(null);
            }

            var response = ReadData(20);

            if (response[7] != 1)
            {
                return(null);
            }

            DebugOnly.Print("Tag found!");

            var uid = new byte[response[12]];

            for (uint i = 0; i < response[12]; i++)
            {
                uid[i] = response[13 + i];
            }

            DebugOnly.Print("UID:");
            DebugOnly.PrintByteArray(uid);

            return(uid);
        }
Beispiel #2
0
        private bool SendCommandCheckAck(byte[] cmd, int timeout = 1000)
        {
            // Write the command
            WriteCommand(cmd);

            // Wait for chip to say its ready!
            if (!WaitReady(timeout))
            {
                notifyError();

                return(false);
            }

            // Read acknowledgement
            if (!ReadAck())
            {
                DebugOnly.Print("No ACK frame received!");

                notifyError();

                return(false);
            }

            // Remove error block
            errorNotified = false;

            DebugOnly.Print("PN532 acknowledged the command!");

            return(true);
        }
Beispiel #3
0
        /// <summary>Initializes NFC module</summary>
        public bool Init()
        {
            DebugOnly.Print("Starting NFC module...");

            // This is needed for synchronization
            byte[] packet = { CommandGetfirmwareversion };
            SendCommandCheckAck(packet);

            // Wait some time
            Thread.Sleep(1000);

            // Configure safe access manager
            if (SamConfig())
            {
                return(false);
            }

            return(false);
        }
Beispiel #4
0
        private void WriteCommand(byte[] cmd)
        {
            var cmdlen = (uint)cmd.Length + 1;

            // Compute checksum
            var checksum = Preamble + Preamble + Startcode + HostToPn532;

            for (uint i = 0; i < cmdlen - 1; i++)
            {
                checksum += cmd[i];
            }

            byte[] writeHeader =
            {
                SpiDatawrite,
                Preamble,
                Preamble,
                Startcode,
                (byte)cmdlen,
                (byte)(~cmdlen + 1),
                HostToPn532
            };

            byte[] writeFooter =
            {
                (byte)~checksum,
                Postamble
            };

            // Create write array
            var write = Utility.CombineArrays(writeHeader, cmd);

            write = Utility.CombineArrays(write, writeFooter);

            write = Common.ReverseBit(write);

            spi.Write(write);

            // Debug informations
            DebugOnly.Print("\nSending command: ");
            DebugOnly.PrintByteArray(write);
        }
Beispiel #5
0
        // LOW LEVEL COMM
        private byte[] ReadData(int n)
        {
            var read = new byte[n + 1];

            byte[] write = { SpiDataread };
            write = Utility.CombineArrays(write, Common.CreateNullPayload(n));

            write = Common.ReverseBit(write);

            spi.WriteRead(write, read);

            read = Utility.ExtractRangeFromArray(read, 1, n);
            read = Common.ReverseBit(read);

            // Debug informations
            DebugOnly.Print("\nReading response: ");
            DebugOnly.PrintByteArray(read);

            return(read);
        }
Beispiel #6
0
        private bool WaitReady(int timeout)
        {
            uint timer = 0;

            while (!IsReady())
            {
                if (timeout != 0)
                {
                    timer += 10;
                    if (timer > timeout)
                    {
                        DebugOnly.Print("ERROR: No response from PN532!");
                        return(false);
                    }
                }

                Thread.Sleep(10);
            }

            DebugOnly.Print("PN532 is READY!");

            return(true);
        }
Beispiel #7
0
        /// <summary>Returns current firmware version</summary>
        public uint GetFirmwareVersion()
        {
            // Request firmware version
            byte[] packet = { CommandGetfirmwareversion };
            if (!SendCommandCheckAck(packet))
            {
                return(0);
            }

            // Read response
            var response = ReadData(12);

            // Extract firmware version
            var header = Utility.ExtractRangeFromArray(response, 0, 6);

            if (!Common.CompareByteArray(header, FvHead))
            {
                DebugOnly.Print("Response is not firmware!");
            }

            var version = (Utility.ExtractValueFromArray(response, 6, 4) >> 8) & 0xFF;

            return(version);
        }