I have an RFID reader (Neuftech USB RFID Reader, https://www.amazon.de/Neuftech-Reader-Kartenleseger%C3%A4t-Kartenleser-Kontaktlos/dp/B018OYOR3E) 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.

---

Attempted approaches from the suggestions. Neither of these prints anything, even after repeated scanning of different RFID tags.

    import serial
    ser = serial.Serial('/dev/tty1', 9600)
    ser.flushInput()
    
    while True:
        try:
            ser_bytes = ser.readline()
            print(str(ser_bytes))
        except:
            print("Keyboard Interrupt")
            break

Second approach:

    import sys
    
    for line in sys.stdin:
        print(str(line))

Third approach:

    import sys
    
    while 1:
        try:
            line = sys.stdin.readline()
        except KeyboardInterrupt:
            break
    
        if not line:
            break
    
        print line

I've also changed the script to initially open stdin from tty1 and just close it after the script is terminated, which makes sense, but doesn't change the outcome.