Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RFID RC522 Raspberry PI 2 Windows物联网_C#_Raspberry Pi2_Iot_Windows 10 Iot Core_Windowsiot - Fatal编程技术网

C# RFID RC522 Raspberry PI 2 Windows物联网

C# RFID RC522 Raspberry PI 2 Windows物联网,c#,raspberry-pi2,iot,windows-10-iot-core,windowsiot,C#,Raspberry Pi2,Iot,Windows 10 Iot Core,Windowsiot,我正在寻找一种在Windows物联网上的Raspberry Pi 2.0上使用RFID“RC522”的方法 这当然不是官方兼容的 官方的一款(OM5577演示板)在法国非常昂贵(我还没有发现任何经销商在没有大量运费的情况下出售它(总成本约为80美元)) RC522很便宜(因为它使用SPI,所以不应该存在硬件兼容性问题。如果您不想翻译现有的Arduino代码,Microsoft确实有一些技术允许您使用现有的Arduino草图和库。您可以在此处阅读更多信息:我终于找到了解决方案 我在arudino的

我正在寻找一种在Windows物联网上的Raspberry Pi 2.0上使用RFID“RC522”的方法

这当然不是官方兼容的

官方的一款(OM5577演示板)在法国非常昂贵(我还没有发现任何经销商在没有大量运费的情况下出售它(总成本约为80美元))


RC522很便宜(因为它使用SPI,所以不应该存在硬件兼容性问题。如果您不想翻译现有的Arduino代码,Microsoft确实有一些技术允许您使用现有的Arduino草图和库。您可以在此处阅读更多信息:

我终于找到了解决方案

我在arudino的可移植性方面还没有成功,所以我用这个项目作为起点

该项目是用C#编写的。我刚刚修改了Windows IOT GPIO和SPI的代码。它正在工作

Main

        var mfrc = new Mfrc522();
        await mfrc.InitIO();

        while (true)
        {
            if (mfrc.IsTagPresent())
            {
                var uid = mfrc.ReadUid();

                mfrc.HaltTag();
            }

        }
库Mfrc522Lib.cs(一体机)

使用系统;
使用System.Collections.Generic;
使用系统线程;
使用System.Threading.Tasks;
使用Windows.Devices.Enumeration;
使用Windows.Devices.Gpio;
使用Windows.Devices.Spi;
名称空间Mfrc522Lib
{
公共静态类寄存器
{
私有常量字节位帧=0x0D;
私有常量字节comIrq=0x04;
私有常量字节comIrqEnable=0x02;
私有常量字节命令=0x01;
私有常量字节控制=0x0C;
私有常量字节错误=0x06;
私有常量字节fifoData=0x09;
私有常量字节fifoLevel=0x0A;
私有常量字节模式=0x11;
私有常量字节rxMode=0x13;
专用常量字节timerMode=0x2A;
私有常量字节timerPrescaler=0x2B;
私有常量字节timerReloadHigh=0x2C;
私有常量字节timerReloadLow=0x2D;
私有常量字节txAsk=0x15;
专用常量字节txControl=0x14;
私有常量字节txMode=0x12;
私有常量字节版本=0x37;
公共静态字节位帧
{
得到
{
返回位帧;
}
}
公共静态字节ComIrq
{
得到
{
返回comIrq;
}
}
公共静态字节ComIrqEnable
{
得到
{
返回comIrqEnable;
}
}
公共静态字节命令
{
得到
{
返回命令;
}
}
公共静态字节控制
{
得到
{
返回控制;
}
}
公共静态字节错误
{
得到
{
返回误差;
}
}
公共静态字节五位数据
{
得到
{
返回五脚架;
}
}
公共静态字节FifoLevel
{
得到
{
返回五电平;
}
}
公共静态字节模式
{
得到
{
返回模式;
}
}
公共静态字节RxMode
{
得到
{
返回rxMode;
}
}
公共静态字节计时器
{
得到
{
返回时间模式;
}
}
公共静态字节计时器
{
得到
{
返回定时器;
}
}
公共静态字节TimerReloadHigh
{
得到
{
返回时间高;
}
}
公共静态字节TimerReloadLow
{
得到
{
返回时间低;
}
}
公共静态字节TxAsk
{
得到
{
返回txAsk;
}
}
公共静态字节TXC控制
{
得到
{
返回TXC控制;
}
}
公共静态字节TxMode
{
得到
{
返回txMode;
}
}
公共静态字节版本
{
得到
{
返回版本;
}
}
}
公共静态类微微通信
{
私人const ushort answerToRequest=0x0004;
私有常量字节selectAcknowledge=0x08;
专用常量字节确认=0x0A;
公共静态字节应答
{
得到
{
回复确认;
}
}
公共静态字节选择确认
{
得到
{
返回selectAcknowledge;
}
}
公共静态ushort响应请求
{
得到
{
回答问题;
}
}
}
公共静态类piccCommand
{
私有常量字节防冲突_1=0x93;
私有常量字节防冲突_2=0x20;
私有常量字节authenticateKeyA=0x60;
私有常量字节authenticateKeyB=0x61;
私有常量字节halt_1=0x50;
私有常量字节halt_2=0x00;
私有常量字节读取=0x30;
私有常量字节请求=0x26;
私有常量字节选择_1=0x93;
私有常量字节选择_2=0x70;
私有常量字节写入=0xA0;
公共静态字节认证
{
得到
{
返回认证Eya;
}
}
公共静态字节认证EYB
{
得到
{
返回AuthenticateEYB;
}
}
公共静态字节暂停_1
{
得到
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.Gpio;
using Windows.Devices.Spi;

namespace Mfrc522Lib
{
    public static class Registers
    {
        private const byte bitFraming = 0x0D;
        private const byte comIrq = 0x04;
        private const byte comIrqEnable = 0x02;
        private const byte command = 0x01;
        private const byte control = 0x0C;
        private const byte error = 0x06;
        private const byte fifoData = 0x09;
        private const byte fifoLevel = 0x0A;
        private const byte mode = 0x11;
        private const byte rxMode = 0x13;
        private const byte timerMode = 0x2A;
        private const byte timerPrescaler = 0x2B;
        private const byte timerReloadHigh = 0x2C;
        private const byte timerReloadLow = 0x2D;
        private const byte txAsk = 0x15;
        private const byte txControl = 0x14;
        private const byte txMode = 0x12;
        private const byte version = 0x37;

        public static byte BitFraming
        {
            get
            {
                return bitFraming;
            }
        }

        public static byte ComIrq
        {
            get
            {
                return comIrq;
            }
        }

        public static byte ComIrqEnable
        {
            get
            {
                return comIrqEnable;
            }
        }

        public static byte Command
        {
            get
            {
                return command;
            }
        }

        public static byte Control
        {
            get
            {
                return control;
            }
        }

        public static byte Error
        {
            get
            {
                return error;
            }
        }

        public static byte FifoData
        {
            get
            {
                return fifoData;
            }
        }

        public static byte FifoLevel
        {
            get
            {
                return fifoLevel;
            }
        }

        public static byte Mode
        {
            get
            {
                return mode;
            }
        }

        public static byte RxMode
        {
            get
            {
                return rxMode;
            }
        }

        public static byte TimerMode
        {
            get
            {
                return timerMode;
            }
        }

        public static byte TimerPrescaler
        {
            get
            {
                return timerPrescaler;
            }
        }

        public static byte TimerReloadHigh
        {
            get
            {
                return timerReloadHigh;
            }
        }

        public static byte TimerReloadLow
        {
            get
            {
                return timerReloadLow;
            }
        }

        public static byte TxAsk
        {
            get
            {
                return txAsk;
            }
        }

        public static byte TxControl
        {
            get
            {
                return txControl;
            }
        }

        public static byte TxMode
        {
            get
            {
                return txMode;
            }
        }

        public static byte Version
        {
            get
            {
                return version;
            }
        }
    }
    public static class PiccResponses
    {
        private const ushort answerToRequest = 0x0004;
        private const byte selectAcknowledge = 0x08;
        private const byte acknowledge = 0x0A;

        public static byte Acknowledge
        {
            get
            {
                return acknowledge;
            }
        }

        public static byte SelectAcknowledge
        {
            get
            {
                return selectAcknowledge;
            }
        }

        public static ushort AnswerToRequest
        {
            get
            {
                return answerToRequest;
            }
        }
    }
    public static class PiccCommands
    {
        private const byte anticollision_1 = 0x93;
        private const byte anticollision_2 = 0x20;
        private const byte authenticateKeyA = 0x60;
        private const byte authenticateKeyB = 0x61;
        private const byte halt_1 = 0x50;
        private const byte halt_2 = 0x00;
        private const byte read = 0x30;
        private const byte request = 0x26;
        private const byte select_1 = 0x93;
        private const byte select_2 = 0x70;
        private const byte write = 0xA0;

        public static byte AuthenticateKeyA
        {
            get
            {
                return authenticateKeyA;
            }
        }

        public static byte AuthenticateKeyB
        {
            get
            {
                return authenticateKeyB;
            }
        }

        public static byte Halt_1
        {
            get
            {
                return halt_1;
            }
        }

        public static byte Halt_2
        {
            get
            {
                return halt_2;
            }
        }

        public static byte Read
        {
            get
            {
                return read;
            }
        }

        public static byte Request
        {
            get
            {
                return request;
            }
        }

        public static byte Select_1
        {
            get
            {
                return select_1;
            }
        }

        public static byte Select_2
        {
            get
            {
                return select_2;
            }
        }

        public static byte Write
        {
            get
            {
                return write;
            }
        }

        public static byte Anticollision_1
        {
            get
            {
                return anticollision_1;
            }
        }

        public static byte Anticollision_2
        {
            get
            {
                return anticollision_2;
            }
        }
    }

    public static class PcdCommands
    {
        private const byte idle = 0x00;
        private const byte mifareAuthenticate = 0x0E;
        private const byte transceive = 0x0C;

        public static byte Idle
        {
            get
            {
                return idle;
            }
        }

        public static byte MifareAuthenticate
        {
            get
            {
                return mifareAuthenticate;
            }
        }

        public static byte Transceive
        {
            get
            {
                return transceive;
            }
        }
    }


    public class Uid
    {
        public byte Bcc { get; private set; }
        public byte[] Bytes { get; private set; }
        public byte[] FullUid { get; private set; }
        public bool IsValid { get; private set; }

        internal Uid(byte[] uid)
        {
            FullUid = uid;
            Bcc = uid[4];

            Bytes = new byte[4];
            System.Array.Copy(FullUid, 0, Bytes, 0, 4);

            foreach (var b in Bytes)
            {
                if (b != 0x00)
                    IsValid = true;
            }
        }

        public sealed override bool Equals(object obj)
        {
            if (!(obj is Uid))
                return false;

            var uidWrapper = (Uid)obj;

            for (int i = 0; i < 5; i++)
            {
                if (FullUid[i] != uidWrapper.FullUid[i])
                    return false;
            }

            return true;
        }

        public sealed override int GetHashCode()
        {
            int uid = 0;

            for (int i = 0; i < 4; i++)
                uid |= Bytes[i] << (i * 8);

            return uid;
        }

        public sealed override string ToString()
        {
            var formatString = "x" + (Bytes.Length * 2);
            return GetHashCode().ToString(formatString);
        }
    }

    public sealed class Mfrc522
    {
        public SpiDevice _spi { get; private set; }
        public GpioController IoController { get; private set; }
        public GpioPin _resetPowerDown { get; private set; }

        /* Uncomment for Raspberry Pi 2 */
        private const string SPI_CONTROLLER_NAME = "SPI0"; 
        private const Int32 SPI_CHIP_SELECT_LINE = 0;  
        private const Int32 RESET_PIN = 25;

        internal async Task InitIO()
        {

            try
            {
                IoController = GpioController.GetDefault(); 

                _resetPowerDown = IoController.OpenPin(RESET_PIN);
                _resetPowerDown.Write(GpioPinValue.High);
                _resetPowerDown.SetDriveMode(GpioPinDriveMode.Output);
            }
            /* If initialization fails, throw an exception */
            catch (Exception ex)
            {
                throw new Exception("GPIO initialization failed", ex);
            }

            try
            {
                var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
                settings.ClockFrequency = 1000000;
                settings.Mode = SpiMode.Mode0;

                String spiDeviceSelector = SpiDevice.GetDeviceSelector();
                IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);

                _spi = await SpiDevice.FromIdAsync(devices[0].Id, settings); 

            }
            /* If initialization fails, display the exception and stop running */
            catch (Exception ex)
            {
                throw new Exception("SPI Initialization Failed", ex);
            }


            Reset();
        }

        public void Reset()
        {
            _resetPowerDown.Write(GpioPinValue.Low);
            System.Threading.Tasks.Task.Delay(50).Wait();
            _resetPowerDown.Write(GpioPinValue.High);
            System.Threading.Tasks.Task.Delay(50).Wait();

            // Force 100% ASK modulation
            WriteRegister(Registers.TxAsk, 0x40);

            // Set CRC to 0x6363
            WriteRegister(Registers.Mode, 0x3D);

            // Enable antenna
            SetRegisterBits(Registers.TxControl, 0x03);
        }


        public bool IsTagPresent()
        {
            // Enable short frames
            WriteRegister(Registers.BitFraming, 0x07);

            // Transceive the Request command to the tag
            Transceive(false, PiccCommands.Request);

            // Disable short frames
            WriteRegister(Registers.BitFraming, 0x00);

            // Check if we found a card
            return GetFifoLevel() == 2 && ReadFromFifoShort() == PiccResponses.AnswerToRequest;
        }

        public Uid ReadUid()
        {
            // Run the anti-collision loop on the card
            Transceive(false, PiccCommands.Anticollision_1, PiccCommands.Anticollision_2);

            // Return tag UID from FIFO
            return new Uid(ReadFromFifo(5));
        }

        public void HaltTag()
        {
            // Transceive the Halt command to the tag
            Transceive(false, PiccCommands.Halt_1, PiccCommands.Halt_2);
        }

        public bool SelectTag(Uid uid)
        {
            // Send Select command to tag
            var data = new byte[7];
            data[0] = PiccCommands.Select_1;
            data[1] = PiccCommands.Select_2;
            uid.FullUid.CopyTo(data, 2);

            Transceive(true, data);

            return GetFifoLevel() == 1 && ReadFromFifo() == PiccResponses.SelectAcknowledge;
        }

        internal byte[] ReadBlock(byte blockNumber, Uid uid, byte[] keyA = null, byte[] keyB = null)
        {
            if (keyA != null)
                MifareAuthenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA);
            else if (keyB != null)
                MifareAuthenticate(PiccCommands.AuthenticateKeyB, blockNumber, uid, keyB);
            else
                return null;

            // Read block
            Transceive(true, PiccCommands.Read, blockNumber);

            return ReadFromFifo(16);
        }

        internal bool WriteBlock(byte blockNumber, Uid uid, byte[] data, byte[] keyA = null, byte[] keyB = null)
        {
            if (keyA != null)
                MifareAuthenticate(PiccCommands.AuthenticateKeyA, blockNumber, uid, keyA);
            else if (keyB != null)
                MifareAuthenticate(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[16];
            data.CopyTo(buffer, 0);

            Transceive(true, buffer);

            return ReadFromFifo() == PiccResponses.Acknowledge;
        }


        protected void MifareAuthenticate(byte command, byte blockNumber, Uid uid, byte[] key)
        {
            // Put reader in Idle mode
            WriteRegister(Registers.Command, PcdCommands.Idle);

            // Clear the FIFO
            SetRegisterBits(Registers.FifoLevel, 0x80);

            // Create Authentication packet
            var data = new byte[12];
            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(Registers.Command, PcdCommands.MifareAuthenticate);

            // Wait for (a generous) 25 ms
            System.Threading.Tasks.Task.Delay(25).Wait();
        }

        protected void Transceive(bool enableCrc, params byte[] data)
        {
            if (enableCrc)
            {
                // Enable CRC
                SetRegisterBits(Registers.TxMode, 0x80);
                SetRegisterBits(Registers.RxMode, 0x80);
            }

            // Put reader in Idle mode
            WriteRegister(Registers.Command, PcdCommands.Idle);

            // Clear the FIFO
            SetRegisterBits(Registers.FifoLevel, 0x80);

            // Write the data to the FIFO
            WriteToFifo(data);

            // Put reader in Transceive mode and start sending
            WriteRegister(Registers.Command, PcdCommands.Transceive);
            SetRegisterBits(Registers.BitFraming, 0x80);

            // Wait for (a generous) 25 ms
            System.Threading.Tasks.Task.Delay(25).Wait();

            // Stop sending
            ClearRegisterBits(Registers.BitFraming, 0x80);

            if (enableCrc)
            {
                // Disable CRC
                ClearRegisterBits(Registers.TxMode, 0x80);
                ClearRegisterBits(Registers.RxMode, 0x80);
            }
        }


        protected byte[] ReadFromFifo(int length)
        {
            var buffer = new byte[length];

            for (int i = 0; i < length; i++)
                buffer[i] = ReadRegister(Registers.FifoData);

            return buffer;
        }

        protected byte ReadFromFifo()
        {
            return ReadFromFifo(1)[0];
        }

        protected void WriteToFifo(params byte[] values)
        {
            foreach (var b in values)
                WriteRegister(Registers.FifoData, b);
        }

        protected int GetFifoLevel()
        {
            return ReadRegister(Registers.FifoLevel);
        }


        protected byte ReadRegister(byte register)
        {
            register <<= 1;
            register |= 0x80;

            var writeBuffer = new byte[] { register, 0x00 };

            return TransferSpi(writeBuffer)[1];
        }

        protected ushort ReadFromFifoShort()
        {
            var low = ReadRegister(Registers.FifoData);
            var high = (ushort)(ReadRegister(Registers.FifoData) << 8);

            return (ushort)(high | low);
        }

        protected void WriteRegister(byte register, byte value)
        {
            register <<= 1;

            var writeBuffer = new byte[] { register, value };

            TransferSpi(writeBuffer);
        }

        protected void SetRegisterBits(byte register, byte bits)
        {
            var currentValue = ReadRegister(register);
            WriteRegister(register, (byte)(currentValue | bits));
        }

        protected void ClearRegisterBits(byte register, byte bits)
        {
            var currentValue = ReadRegister(register);
            WriteRegister(register, (byte)(currentValue & ~bits));
        }


        private byte[] TransferSpi(byte[] writeBuffer)
        {
            var readBuffer = new byte[writeBuffer.Length];

            _spi.TransferFullDuplex(writeBuffer, readBuffer);

            return readBuffer;
        }
    }
}
var uid = mfrc.ReadUid();
string txt_Result = "";
foreach (byte byt in uid.FullUid)
{
    txt_Result = txt_Result + byt.ToString("x2");
}
mfrc.HaltTag();