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.
sr
has the valueb''
after thereadline()
method. So addingprint(sr)
printsb''
.