0

I'm trying to read my rfid card MiFare 1k classic with a PN532 breakout board through UART with a simple python script. I installed libnfc, the nfc-poll example program reads my card perfectly fine. I also tried nfcpy, but MiFare 1k classic isn't supported. The nfcpy tagtool.py program does read my phone.

According to some basic python script I found on the internet, below code should do the trick aswell, which would be the simple solution I'm looking for:

  ser = serial.Serial('/dev/ttyAMA0', 2400, timeout=1)
  print("opened {0}" . format(ser.name))
  while True:
            sr = ser.read(12)
            s = sr.decode('utf8')
            if len(s) == 0:
                  continue
            else:
                sl = s[1:11] #exclude start x0A and stop x0D bytes
                print(sl)

I should be able to read my card, but instead s remains empty. It did successfully open ser, because the 2nd line is printed saying "opened /dev/ttyAMA0". Because s remains empty the program falls in an infinite loop, thus I cancel it with ctrl+c. Then the traceback says:

    Traceback (most recent call last):
      File "rfidtest.py", line 13, in <module>
         sr = ser.read(12)
      File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 442, in read
      ready,_,_ = select.select([self.fd],[],[], self._timeout)

After googling for a while now, I can't seem to find out why I get this, instead of the info on my rfid card. Anyone got an idea? As I said before the example from libnfc works, and so does the nfcpy for my phone so I don't think the problem is hardware related. I probably just miss an essential part of code for it to work. Any help is much appreciated.

  • What is the point of the decode? Why don't you just read and print the bytes you actually receive to see what is going on? – joan Feb 16 '15 at 16:24
  • When I remote-debug with visual studio it shows that sr has the value b'' after the readline() method. So adding print(sr) prints b''. – Guinn Feb 16 '15 at 16:31
  • I believe b'' is Python for a null byte string. I.e. no data has been received. Have you confirmed that data is being received? – joan Feb 16 '15 at 17:15
3

The PN532 has multiple interfaces and can do a lot more than just "read" a card. So it implements a serial protocol that requires you send it commands asking it to do something and give you a response.

That's why you're getting no data, because you're not sending it a command to configure for reading and asking it to tell you the card it can see (if any). The other programs you're testing with do that which does prove your hardware setup is fine.

This is an old post, but I was searching for a purely python implementation of this protocol (which I have yet to find) but I have implemented a custom C header interface for python which has let me read cards using the NFC libraries. It's not well documented, but it's apart of my bigger project for managing access with NFC cards: Ctrl-O

If I make or find a purely python interface to do this I'll update this answer.

UPDATE: I did find nfcpy as a "pure" nfc python library. I was able to get it to run on a piCore 7 image by editing the code to remove references and requirements for the usblib. With a PN532 reader attached the UART I could read cards with the example code as well as push URLs to my phone!

| improve this answer | |
2

I have no rep, so can't comment directly on Patrick's reply.

I know this post is old, but nearly no NFC work has been done for Python. There's one module out there that only does SPI, and there's the libnfc library. I've managed to get I2C working by simply running a subprocess. it's slow, and I'm working to improve that, but here it is.

(Assuming you have libnfc installed. I just threw the files/folders in my directory with my Python script. It was the only way I could make libnfc work.)

from subprocess import check_output
nfc_read = check_output('nfc-list', universal_newlines=True)
print(nfc_read)

It'll work without the universal_newlines=True but that arg pretties it up.

**EDIT - I was wrong about the only library out there doing SPI, I found an old one that support I2C, but am having issues with it myself.

Link to library: https://github.com/HubCityLabs/py532lib/blob/master/py532lib/i2c.py

Link to my question regarding it: py532lib - i2c - Working But Too Many Files Open After A While

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.