Python Forum
Having strange results from an RFID HID card reader - I'm stuck - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Having strange results from an RFID HID card reader - I'm stuck (/thread-36770.html)



Having strange results from an RFID HID card reader - I'm stuck - orbisnz - Mar-28-2022

Hi all, and thanks for taking a look. I'm in the process of upgrading an old Python 2.7 program I wrote about 5 years ago to Python 3 as well as using a USB connected RFID reader rather than the old SPI interface one I used to use. So I'm using Python 3.10 on Debian Bullseye (Raspberry Pi 3+). The section of code below is used to read 8 bytes from the card reader:

        
import evdev
EVENT_READ_NUMBER = 1

#read card code here
	for event in device.read_loop():
		if event.value == EVENT_VALUE_KEYDOWN:
			#logger.debug(event )
			which_key = evdev.ecodes.KEY[event.code]
			#logger.debug(which_key)
			press = which_key[-1]
			logger.debug('key ' + press)
			readlist.append(press)
			EVENT_READ_NUMBER += 1
			logger.debug(EVENT_READ_NUMBER)
			if EVENT_READ_NUMBER == 8:
				logger.debug('card read complete')
				rfidstring=(rfidraw.join(readlist))
				logger.debug('rfidstring is: ' + rfidstring)
				sn = rfidstring.lower()
				snum = sn
				EVENT_READ_NUMBER = 0
				readlist = []
				sn = ""
				rfidstring = ""
				logger.debug('rfidstring is now: ' + rfidstring)
				break
	logger.debug('Card Detected')
The card reader does not return any end of string character, if it did, I'd use that rather than count 8 bytes. When I go slow (wait at least a couple of seconds between card swipes) then it works perfectly as expected creating a result like sn = cabd5b04 which is what I'm looking for.

I'm not getting any python errors (finally the code is running for me at least!)

Here is the logger output from a successful swipe:

Output:
2022-03-20 15:36:09,378 - DEBUG - key C 2022-03-20 15:36:09,379 - DEBUG - 2 2022-03-20 15:36:09,396 - DEBUG - key A 2022-03-20 15:36:09,397 - DEBUG - 3 2022-03-20 15:36:09,414 - DEBUG - key B 2022-03-20 15:36:09,415 - DEBUG - 4 2022-03-20 15:36:09,432 - DEBUG - key D 2022-03-20 15:36:09,433 - DEBUG - 5 2022-03-20 15:36:09,450 - DEBUG - key 5 2022-03-20 15:36:09,451 - DEBUG - 6 2022-03-20 15:36:09,468 - DEBUG - key B 2022-03-20 15:36:09,469 - DEBUG - 7 2022-03-20 15:36:09,486 - DEBUG - key 0 2022-03-20 15:36:09,487 - DEBUG - 8 2022-03-20 15:36:09,504 - DEBUG - key 4 2022-03-20 15:36:09,504 - DEBUG - 9 2022-03-20 15:36:09,505 - DEBUG - card read complete 2022-03-20 15:36:09,506 - DEBUG - rfidstring is: CABD5B04 2022-03-20 15:36:09,507 - DEBUG - rfidstring is now: 2022-03-20 15:36:09,508 - DEBUG - Card Detected 2022-03-20 15:36:09,509 - DEBUG - Card UID string cabd5b04 2022-03-20 15:36:09,513 - DEBUG - ('David', 'Birch', 'TRUE', '1073A', 13, 'cabd5b04')
The problem I'm having comes when a user repeat swipes several times in quick succession. In that case the byte order seems to get "mixed up" like part of it is read out of order/sequence. For example:

Output:
2022-03-20 15:36:12,991 - DEBUG - key B 2022-03-20 15:36:12,991 - DEBUG - 2 2022-03-20 15:36:12,992 - DEBUG - key D 2022-03-20 15:36:12,993 - DEBUG - 3 2022-03-20 15:36:12,994 - DEBUG - key 5 2022-03-20 15:36:12,994 - DEBUG - 4 2022-03-20 15:36:12,995 - DEBUG - key B 2022-03-20 15:36:12,996 - DEBUG - 5 2022-03-20 15:36:12,997 - DEBUG - key 0 2022-03-20 15:36:12,997 - DEBUG - 6 2022-03-20 15:36:12,998 - DEBUG - key 4 2022-03-20 15:36:12,999 - DEBUG - 7 2022-03-20 15:36:17,978 - DEBUG - key C 2022-03-20 15:36:17,979 - DEBUG - 8 2022-03-20 15:36:17,996 - DEBUG - key A 2022-03-20 15:36:17,997 - DEBUG - 9 2022-03-20 15:36:18,000 - DEBUG - card read complete 2022-03-20 15:36:18,001 - DEBUG - rfidstring is: BD5B04CA 2022-03-20 15:36:18,002 - DEBUG - rfidstring is now: 2022-03-20 15:36:18,002 - DEBUG - Card Detected 2022-03-20 15:36:18,003 - DEBUG - Card UID string bd5b04ca 2022-03-20 15:36:18,007 - DEBUG - Unknown Card 2022-03-20 15:36:21,272 - DEBUG - key B 2022-03-20 15:36:21,273 - DEBUG - 2 2022-03-20 15:36:21,274 - DEBUG - key D 2022-03-20 15:36:21,274 - DEBUG - 3 2022-03-20 15:36:21,275 - DEBUG - key 5 2022-03-20 15:36:21,276 - DEBUG - 4 2022-03-20 15:36:21,276 - DEBUG - key B 2022-03-20 15:36:21,277 - DEBUG - 5 2022-03-20 15:36:21,278 - DEBUG - key 0 2022-03-20 15:36:21,278 - DEBUG - 6 2022-03-20 15:36:21,279 - DEBUG - key 4 2022-03-20 15:36:21,280 - DEBUG - 7
To me it looks like the EVENT_READ_NUMBER is not resetting properly after getting the full 8 bytes from the rfid card. I have tried resetting everything in the for loop, but I’m hitting the end of my knowledge here. Any hints? Should I be inserting some sort of time.sleep in there somewhere? Thanks very much in advance!

Pete


RE: Having strange results from an RFID HID card reader - I'm stuck - Larz60+ - Mar-28-2022

Have you installed all of the python 3 drivers required (with python 3.1 active)?
see chapter 2 http://python-evdev.readthedocs.io/en/latest/

for Debian, reinstall using apt:

Output:
$ apt-get install python-dev python-pip gcc $ apt-get install linux-headers-$(uname -r)
then reinstall evdev using pip