Slimestone
Posts: 11
Joined: Sat Oct 29, 2016 1:07 pm

[Solved] How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Mon Sep 17, 2018 10:38 am

Currently I'm using the python code/library from this git:

https://github.com/mxgxw/MFRC522-python

And as the title says, I'm using a MFRC522-module for Raspberry Pi with a RPi 3+. Reading the tags that came with the RFID-Reader was easy with the python-code from mxgxw.

But I also bought MIFARE DESFire EV2 tags, because they should work with the hard and software i have. It kinda works... BUT:
I checked the tags with the NXP Taginfo App for Android first. The ID that was shown on the Raspberry Pi and the ID I got with the app were the same for the ones, that came with the reader. And now to the problem. The python code provided only reads 4 bytes, which is perfectly fine for the ones that came with the reader, but the MIFARE ones (I bought them seperately) have an ID of 7 bytes. Thats atleast what the App showed me. The output of the Raspberry Pi was also a bit weird. The first byte is 0x88 which seems pretty random to me, because i dont know where it comes from, because the other 3 bytes of the output are equal to the first 3 bytes of the ID from the tag. Furthermore, I'm getting authentication errors, when trying to read the MIFARE ones, which I don't get when I try to read the "free" ones.

So my question is. Can I read those tags, with the hard and software, that im currently using? Because I just want to read the UID for my application. I dont need deeper access which needs authentication. But I'm curious if I'll get into trouble if you just comment out the Authentication and use the 4 bytes that this app gives me.

Context: I'll have to identify about 100 different tags with my app.
Last edited by Slimestone on Tue Sep 18, 2018 8:55 am, edited 1 time in total.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Mon Sep 17, 2018 11:06 am

I did find one version on google which works very well with any size id.

https://storage.googleapis.com/google-c ... rchive.zip


I modified the original code to get ridd of bcm2835 library.
git clone https://github.com/danjperron/rc522_reader
cd rc522_reader
make

Code: Select all

./rc522_reader -d
will show the serial ID.

Look into the code . All functions needed are there.

If you speak french I made a post to display different videos using rfid card.
https://www.raspberrypi.org/forums/view ... hilit=rfid

Slimestone
Posts: 11
Joined: Sat Oct 29, 2016 1:07 pm

Re: How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Mon Sep 17, 2018 11:23 am

danjperron wrote:
Mon Sep 17, 2018 11:06 am
I did find one version on google which works very well with any size id.

https://storage.googleapis.com/google-c ... rchive.zip
This is in written in C++. Any chance you have something for python?

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Mon Sep 17, 2018 3:22 pm

I looked at the python code and there are some changes to do.

Some mifare cards could report up to 10 bytes of data. but the python code just look at range(0,5).

There is some added collision check to do for card with 7 or 10 bytes long.

I will check it tonight if this python class could be change to read the two other types of cards.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Tue Sep 18, 2018 4:21 am

It is not fully complete but I modified MFRC522 to read RFID card with 7 and 10 bytes ID

I added some debug print out. it will need to be clean and verify but looks like this works!! :D

Code: Select all

pi@Pi2:~/MFRC522-python $ python Read.py 
Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
Card detected
anticol1() [136, 4, 46, 115, 209]
Size: 4
PcdSelect 147 [4, 218, 23]
pcdSelect1() [136, 4, 46, 115, 209]
Anticol2() [26, 121, 38, 129, 196]
Size: 0
PcdSelect 149 [0, 254, 81]
pcdSelect2 return=4 uid=[26, 121, 38, 129, 196]
PcdSelect2() [26, 121, 38, 129, 196]
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81 

https://github.com/danjperron/MFRC522-python

Slimestone
Posts: 11
Joined: Sat Oct 29, 2016 1:07 pm

Re: How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Tue Sep 18, 2018 7:22 am

Thanks a lot. I just stumbled upon the 2nd anticoll cascade myself and am trying to get it working, but I'm doing it differently than you and do not seem to get it working :( But I kinda want to fix it myself now, knowing that I'm on the right path :D If I'll be too frustratet some time, then I'll just try to use your code. Thanks a lot, I will keep you guys updated :D

Slimestone
Posts: 11
Joined: Sat Oct 29, 2016 1:07 pm

Re: How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Tue Sep 18, 2018 8:54 am

I managed to get it working! :D Your example helped me a lot! I didn't know that I have to select the tags between the anticolls :? Anyone trying to read 7 bytes of RFID with python, should definetly check his modified library out!

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: [Solved] How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Tue Sep 18, 2018 2:52 pm

I Just updated the github with cleaner code and a working Read function. the DEBUG print are an option.

Code: Select all

git clone https://github.com/danjperron/MFRC522-python

Code: Select all

cd MFRC522-python
python Read.pypi@Pi2:~/MFRC522-python $ python Read.py
Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
Card detected
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81
Card detected
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81
Card detected
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81
Card detected
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81
Card detected
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81
Card detected
Card read UID: 0x4,0x2e,0x73,0x1a,0x79,0x26,0x81
^CCtrl+C captured, ending read.

atika13
Posts: 1
Joined: Wed Apr 24, 2019 1:10 pm

Re: [Solved] How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Wed Apr 24, 2019 1:22 pm

Hello , please help me i want to read desfire card with Raspberry pi and MFRC522 , do you have a tutorial ??

p.s. I speak french better than English.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: [Solved] How to read MIFARE DESfire EV2 tags with MFRC522 properly?

Wed Apr 24, 2019 2:33 pm

@atika13.

No need for a tuto it is straightforward. (Also there is a lot of doc , a little bit out of date, on the net).

- enable SPI
sudo raspi-config , 5 Interfacing option, P4 SPI, Yes, finish

You could test if the SPI is enabled using this command
ls -l /dev/spi*
if /dev/spidev0.0 and /dev/spidev0.1 are displayed then you are in business.

- clone the github
git clone https://github.com/danjperron/MFRC522-python

- power off the PI to make wrie connection

- connect the RC-522 card like the readme description. Don't connect the reset pin.

- power up the PI

- run the command

cd MFRC522-python
python3 Read.py


Now put the RFID card near the reader and it should output the serial Id.

Return to “Interfacing (DSI, CSI, I2C, etc.)”