Table of Contents

Class MfRc522

Namespace
Iot.Device.Mfrc522
Assembly
Iot.Device.Mfrc522.dll

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

i2cDevice I2cDevice

An I2C device, note that there is no default address for this device, it can be programmed with pins.

pinReset int

A reset pin for the hardware reset.

gpioController GpioController

A GpioController for the hardware reset.

shouldDispose bool

True 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

spiDevice SpiDevice

A SPI device

pinReset int

A reset pin for the hardware reset.

gpioController GpioController

A GpioController for the hardware reset.

shouldDispose bool

True 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

serialPort SerialPort

A Serial Port, default speed is 9600 baud, no parity, 1 bit stop.

pinReset int

A reset pin for the hardware reset.

gpioController GpioController

A GpioController for the hardware reset.

shouldDispose bool

True 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

serialPort string

A Serial Port name, will construct a SerialPort with default speed of 9600 baud, no parity, 1 bit stop.

pinReset int

A reset pin for the hardware reset.

gpioController GpioController

A GpioController for the hardware reset.

shouldDispose bool

True to dispose the GpioController.

Fields

DefaultSpiMode

Only SPI Mode supported is Mode0

public const SpiMode DefaultSpiMode = Mode0

Field Value

SpiMode

MaximumSpiClockFrequency

The maximum speed for SPI transfer speed

public const int MaximumSpiClockFrequency = 10000000

Field Value

int

Properties

Enabled

Switch on or off the antenna.

public bool Enabled { get; set; }

Property Value

bool

Gain

Get or Set the gain.

public Gain Gain { get; set; }

Property Value

Gain

SerialSpeed

Set or Get the baud rate for the serial port communication. Default is 9600 baud.

public SerialSpeed SerialSpeed { get; set; }

Property Value

SerialSpeed

Version

Get the Version.

public Version Version { get; }

Property Value

Version

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

atqa byte[]

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

card Data106kbpsTypeA

A card once detected.

timeout TimeSpan

A 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

key SpanByte

A 6 bytes key

mifareCommand MifareCardCommand

MifareCardCommand.AuthenticationA or MifareCardCommand.AuthenticationB

blockAddress byte

The block address to authenticate.

cardUid SpanByte

The 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

targetNumber byte

The 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

command MfrcCommand

The MFRC522 command.

sendData SpanByte

The data to send.

receiveData SpanByte

The data to receive. Note that you need to have at least the size of data you expect to receive.

numberValidBitsLastByte byte

The 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

targetNumber byte

Some readers have a notion of target number for the cards as they can read multiple ones

dataToSend SpanByte

A standardized raw buffer with the command at the position 0 in the array

dataFromCard SpanByte

If 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 dataFromCard array