I have an RFID reader that attaches via USB. Via /var/log/syslog
I found out, that it is available as /dev/tty1
. The following Python script should read and print the scanned RFID tags:
#!/usr/bin/python3
import sys,datetime
def main():
while True:
sys.stdin = open('/dev/tty1', 'r')
rfid = input()
print("Read code from RFID reader: "+str(rfid.rstrip()))
sys.stdin.close()
main()
While this usually works fine, it is not reliable at all. For the first scan, the reader beeps as if it read successfully, but the script doesn't print anything.
Sometimes the read RFID by the script is a mangled. Mostly is okay, some are skipped, or not read but the reader beeps as if it read just fine. After a while the script crashes.
Read code from RFID reader: 0015766026
Read code from RFID reader: 0015766026
Read code from RFID reader: 0015766026
Read code from RFID reader: 0015766026
Read code from RFID reader: 0015766026
Read code from RFID reader: 0015766026
Read code from RFID reader: 0015766026
Read code from RFID reader: 0014090374
Read code from RFID reader: 0004284829
Read code from RFID reader: 0016658737
Read code from RFID reader: 0003342559
Read code from RFID reader: 0003342559
Read code from RFID reader: 0016658737
Read code from RFID reader: 0016658737
Read code from RFID reader: 0004284829
Read code from RFID reader: 0048890004284829
Read code from RFID reader: 0016658737
Read code from RFID reader: 0016658737
Traceback (most recent call last):
File "rfid.py", line 14, in <module>
main()
File "rfid.py", line 7, in main
rfid = input()
EOFError: EOF when reading a line
How can I modify the script to be more reliable. In the end the Raspberry Pi 2 should log the days and time where specific tags are scanned.