Python Forum
Having strange results from an RFID HID card reader - I'm stuck
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having strange results from an RFID HID card reader - I'm stuck
#1
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
Reply
#2
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
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml simple reader kucingkembar 2 1,050 Aug-19-2022, 08:51 PM
Last Post: kucingkembar
  SQL wild card use hammer 3 1,261 Jan-07-2022, 02:17 PM
Last Post: hammer
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,465 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  NFC reader code help johnroberts2k 1 2,566 Jul-02-2021, 08:43 PM
Last Post: deanhystad
  csv.reader(): Limit the number of columns read in Windows Pedroski55 9 5,169 Jan-23-2021, 01:03 AM
Last Post: pjfarley3
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,224 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Closing Files - CSV Reader/Writer lummers 2 2,598 May-28-2020, 06:36 AM
Last Post: Knight18
  How to append one function1 results to function2 results SriRajesh 5 3,120 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  csv reader kgiles 3 5,329 Nov-05-2019, 09:04 AM
Last Post: perfringo
  Credit card number redacting script Drone4four 6 5,187 Jan-18-2019, 02:07 PM
Last Post: Drone4four

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020