So I'm trying to read data from my rfid reader PN532 which is connected to UART on my raspberry pi. The hardware is all connected and should be working properly, because libnfc's example nfc-poll reads my tag and phone, and the nfcpy library can read my phone.
Now I want to write a simple python program to read the serial port '/dev/ttyAMA0' (on which the UART rfid reader works) and retrieve the UID from the data that is read. I can't use the nfcpy library because it doesn't support my MiFare 1k classic card. As far as I could read up on, when pip installing pyserial, I could write a program like this to retrieve data from my UART serial port:
import serial
ser = serial.Serial('/dev/ttyAMA0', 115200, timeout=5)
print("opened {0}" . format(ser.name))
while True:
try:
sr = ser.readline()
s = sr.decode('utf8')
if len(s) == 0:
continue
else:
sl = s[1:11] #exclude start x0A and stop x0D bytes
print(sl)
except Exception as e:
print("error: {0}" . format(e))
But when I remote-debug with visual studio sr keeps being b''
and thus the decode results in an empty string.
Are my parameters for serial.Serial() wrong? Or does this method just not work for a PN532? Or is something else amiss? Any help is much appreciated.
There's a reason why there a libraries like libnfc and nfcpy to interface the PN532 NFC chip: This chip is not a stand-alone reader. Hence it does not just continuously enumerate tags and send their serial numbers across the serial interface.
Instead, the PN532 contains a couple of registers that you need to configure for your application and provides a couple of commands (to read/write registers, to poll for tags/NFC devices, to listen for initiators/readers, to exchange data across the RF interface, etc) that you need to use to instruct the PN532 to do what you want. See the PN532 user manual for the serial interface, the available commands and explainations on their use.