Reputation: 564
I have a card reader (MagTec MSR100) that I want to use to read information of of the magnetic strip of cards. When I plug in the card reader, the computer interprets the card reader as if it were a keyboard, sending the corresponding keypresses to standard input for whatever application I'm in (terminal, vim, etc.). What I'd really like to do is to capture the output that comes specifically from this device and not from the keyboard.
I have read permissions on the device (for me, /dev/usb/hiddev0), and when I try the following code
f = open('/dev/usb/hiddev0', 'r')
f.readline()
in Python and swipe the card, the information encoded in the magnetic strip (something like %6091430968014=FIRST/LAST?
) appears as if I just typed it in, and the call to readline does not return. How can I listen to the input from this specific device?
Upvotes: 1
Views: 1999
Reputation: 41
If you're using Linux, try the Python module evdev.
from evdev import InputDevice, categorize, ecodes
# Initialize card reader
swiper = InputDevice("/dev/input/event0") # magswipe card reader
scancodes = {
# Scancode: ASCIICode
0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 57: u' ', 100: u'?'
}
def read_card(lcd_line_1, lcd_line_2):
while True:
# listen to card swiper
for event in swiper.read_loop():
if event.type == ecodes.EV_KEY:
data = categorize(event) # Save the event temporarily to introspect it
if data.keystate == 1: # Down events only
key_lookup = scancodes.get(data.scancode) or u'UNKNOWN:{}'.format(data.scancode) # Lookup or return UNKNOWN:XX
print(key_lookup)
Upvotes: 0