Class MfRc522
MfRc522 module
public class MfRc522 : CardTransceiver
- Inheritance
-
MfRc522
Constructors
MfRc522(I2cDevice, int, GpioController?, bool)
Constructor for MFRC5222 with I2C interface.
public MfRc522(I2cDevice i2cDevice, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
Parameters
i2cDeviceI2cDeviceAn I2C device, note that there is no default address for this device, it can be programmed with pins.
pinResetintA reset pin for the hardware reset.
gpioControllerGpioControllerA GpioController for the hardware reset.
shouldDisposeboolTrue to dispose the GpioController.
MfRc522(SpiDevice, int, GpioController?, bool)
Constructor for MFRC5222 with SPI interface.
public MfRc522(SpiDevice spiDevice, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
Parameters
spiDeviceSpiDeviceA SPI device
pinResetintA reset pin for the hardware reset.
gpioControllerGpioControllerA GpioController for the hardware reset.
shouldDisposeboolTrue to dispose the GpioController.
MfRc522(SerialPort, int, GpioController?, bool)
Constructor for MFRC5222 with Serial Port interface.
public MfRc522(SerialPort serialPort, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
Parameters
serialPortSerialPortA Serial Port, default speed is 9600 baud, no parity, 1 bit stop.
pinResetintA reset pin for the hardware reset.
gpioControllerGpioControllerA GpioController for the hardware reset.
shouldDisposeboolTrue to dispose the GpioController.
MfRc522(string, int, GpioController?, bool)
Constructor for MFRC5222 with Serial Port interface.
public MfRc522(string serialPort, int pinReset = -1, GpioController? gpioController = null, bool shouldDispose = true)
Parameters
serialPortstringA Serial Port name, will construct a SerialPort with default speed of 9600 baud, no parity, 1 bit stop.
pinResetintA reset pin for the hardware reset.
gpioControllerGpioControllerA GpioController for the hardware reset.
shouldDisposeboolTrue to dispose the GpioController.
Fields
DefaultSpiMode
Only SPI Mode supported is Mode0
public const SpiMode DefaultSpiMode = Mode0
Field Value
MaximumSpiClockFrequency
The maximum speed for SPI transfer speed
public const int MaximumSpiClockFrequency = 10000000
Field Value
Properties
Enabled
Switch on or off the antenna.
public bool Enabled { get; set; }
Property Value
Gain
Get or Set the gain.
public Gain Gain { get; set; }
Property Value
SerialSpeed
Set or Get the baud rate for the serial port communication. Default is 9600 baud.
public SerialSpeed SerialSpeed { get; set; }
Property Value
Version
Get the Version.
public Version Version { get; }
Property Value
Remarks
Only versions 1.0 and 2.0 are valid for authentic MFRC522. Some copies may not have a proper version but would just work.
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Halt()
Stop to communicate with a card.
public bool Halt()
Returns
- bool
True if success.
Remarks
It's not because you don't get a positive result the card is not halt.
IsCardPresent(byte[])
Check if a new card is present.
public bool IsCardPresent(byte[] atqa)
Parameters
atqabyte[]ATQA buffer must be 2 bytes length and will contain the ATQA answer if there is a card.
Returns
- bool
true if there is a card, else false.
ListenToCardIso14443TypeA(out Data106kbpsTypeA, TimeSpan)
Listen to any 14443 Type A card.
public bool ListenToCardIso14443TypeA(out Data106kbpsTypeA card, TimeSpan timeout)
Parameters
cardData106kbpsTypeAA card once detected.
timeoutTimeSpanA timeout for pulling the card.
Returns
- bool
True if success.
MifareAuthenticate(SpanByte, MifareCardCommand, byte, SpanByte)
Specific function to authenticate Mifare cards
public Status MifareAuthenticate(SpanByte key, MifareCardCommand mifareCommand, byte blockAddress, SpanByte cardUid)
Parameters
keySpanByteA 6 bytes key
mifareCommandMifareCardCommandMifareCardCommand.AuthenticationA or MifareCardCommand.AuthenticationB
blockAddressbyteThe block address to authenticate.
cardUidSpanByteThe 4 bytes UUID of the card.
Returns
- Status
True if success.
PrepareForSleep()
Prepare for sleep, make sure cryptography is off and switch off the antenna.
public void PrepareForSleep()
ReselectTarget(byte)
Once you have an authentication operation failing with Mifare cards or a read/write, the card stop. TYhe only way to have it back is to send the unselect and anti collision. This function provides this feature
public override bool ReselectTarget(byte targetNumber)
Parameters
targetNumberbyteThe target number to reselect
Returns
- bool
True if success
SendAndReceiveData(MfrcCommand, SpanByte, SpanByte, byte)
Send and Receive Data.
public Status SendAndReceiveData(MfrcCommand command, SpanByte sendData, SpanByte receiveData, byte numberValidBitsLastByte = 8)
Parameters
commandMfrcCommandThe MFRC522 command.
sendDataSpanByteThe data to send.
receiveDataSpanByteThe data to receive. Note that you need to have at least the size of data you expect to receive.
numberValidBitsLastBytebyteThe number of bits valid in the last byte, 8 is the default.
Returns
- Status
True if the operation is successful.
SoftReset()
Perform a soft reset. The configuration data of the internal buffer remains unchanged.All registers are set to the reset values.This command automatically terminates when finished.
public void SoftReset()
Remarks
The SerialSpeedReg register is reset and therefore the serial data rate is set to 9600 baud.
Transceive(byte, SpanByte, SpanByte)
This function has to be implemented by all NFC/RFID/Card readers. This function is used in exchange of data with the reader and the cards.
public override int Transceive(byte targetNumber, SpanByte dataToSend, SpanByte dataFromCard)
Parameters
targetNumberbyteSome readers have a notion of target number for the cards as they can read multiple ones
dataToSendSpanByteA standardized raw buffer with the command at the position 0 in the array
dataFromCardSpanByteIf any data are read from the card, they will be put into this array
Returns
- int
-1 in case of error, otherwise the number of bytes read and copied into the
dataFromCardarray