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.
edit: unplugging the device, then plugging the device in again dmesg -w
[19557.527332] usb 1-1.3: USB disconnect, device number 4
[19565.156398] usb 1-1.3: new low-speed USB device number 5 using dwc_otg
[19565.298471] usb 1-1.3: New USB device found, idVendor=16c0, idProduct=27db, bcdDevice= 0.01
[19565.298491] usb 1-1.3: New USB device strings: Mfr=1, Product=0, SerialNumber=0
[19565.298504] usb 1-1.3: Manufacturer: HXGCoLtd
[19565.314868] input: HXGCoLtd Keyboard as /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/0003:16C0:27DB.0002/input/input1
[19565.382012] hid-generic 0003:16C0:27DB.0002: input,hiddev96,hidraw0: USB HID v1.10 Keyboard [HXGCoLtd] on usb-3f980000.usb-1.3/input0