Python Forum
Using evdev for background script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using evdev for background script
#1
What I'm attempting to do is have a python script running in the background at all times that only takes input from one of the usb devices plugged into the Raspberry Pi, in this case, it's a barcode scanner. What I've gotten so far works really well, but here's what's missing. I don't want that input to "show up" anywhere. I want the script to handle it and then make it go away.

Here's what I've got, in case anyone's interested:

Any help is appreciated in advance!

import evdev from evdev import ecodes import RPi.GPIO as GPIO import time from datetime import datetime 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', 56: u'LALT', 57: u' ', 100: u'RALT' } in1 = 16 in2 = 18 GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(in1, GPIO.OUT) GPIO.setup(in2, GPIO.OUT) GPIO.output(in1, True) GPIO.output(in2, True) mem_num = "" valid_card = False mem_file = open("/home/verhovay/Programs/members.txt", "r") mem_list = mem_file.readlines() mem_file.close() def read_keys(device): obj = evdev.InputDevice(device) for x in obj.read_loop(): if x.code in SCANCODES and x.type == ecodes.EV_KEY and x.value == 1: yield SCANCODES[x.code] def inputesque(what): s = [] for x in what: if x == 'CRLF': break s.append(x) return "".join(s) def LogEntry( mNum, Valid ): today = datetime.today() now = datetime.now() dateCap = today.strftime("%Y-%m-%d") timeCap = now.strftime("%H:%M:%S") targetFolder = "/home/verhovay/Programs/Logs/" + dateCap + ".txt" target = open(targetFolder, 'a') if Valid: target.write(timeCap + " => " + mNum + " => Valid \n") else: target.write(timeCap + " => " + mNum + " => Invalid \n") target.close() return while True: try: mem_num = inputesque(read_keys("/dev/input/by-id/usb-SM_SM-2D_PRODUCT_HID_KBW_APP-000000000-event-kbd")) if mem_num == "1": break valid_card = mem_num.startswith("LSHFTvLSHFThLSHFTa100") or mem_num.startswith("LSHFTvLSHFThLSHFTa200") mem_num = str(mem_num.replace("LSHFTvLSHFThLSHFTa100", "")) mem_num = str(mem_num.replace("LSHFTvLSHFThLSHFTa200", "")) if (mem_num + "\n") in mem_list and valid_card: LogEntry (mem_num, True); GPIO.output(in1, False) GPIO.output(in2, False) time.sleep(.5) GPIO.output(in1, True) GPIO.output(in2, True) else: LogEntry (mem_num, False); print("Not Valid") except Exception as err: now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print (err, "***", dt_string)
Reply
#2
I figured it out. Looks like I can't delete a post, so I may as well provide the answer. To get exclusive access to a device, the following code worked well:

from evdev import ecodes, InputDevice dev = InputDevice('/dev/input/by-id/[deviceID]') dev.grab()
...and to release exclusive access...
dev.ungrab()
Simple enough!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 3,583 May-28-2020, 09:41 PM
Last Post: rangeshgupta
  Using Subprocess.Popen to start another python script running in background on Window johnb546 0 15,574 Jun-01-2018, 01:57 PM
Last Post: johnb546
  Differntiating two keyboards using evdev HANSJORG2 5 10,313 Mar-08-2018, 02:09 PM
Last Post: buran

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.