Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for multiple RFID Readers #1012

Merged
merged 8 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added multiple USB reader support for Reader.py.experimental (see fil…
…e Reader.py.experimental.Multi)

Added Register NonUSB Devices with RegisterDevice (see RegisterDevice.py.Multi)
  • Loading branch information
Markus Prochaska authored and Markus Prochaska committed Jun 14, 2020
commit 2aeb6570f3a0633c19047798db54c5b34300bd44
29 changes: 21 additions & 8 deletions scripts/Reader.py.experimental.Multi
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
# Please use the github issue threads to share bugs and improvements
# or create pull requests.
import multiprocessing
try:
from multiprocessing import SimpleQueue
except ImportError:
from multiprocessing.queues import SimpleQueue
import os.path
import sys
import serial
import string
import RPi.GPIO as GPIO
import logging
from queue import Queue

from evdev import InputDevice, categorize, ecodes, list_devices
from evdev import InputDevice, ecodes, list_devices
# Workaround: when using RC522 reader with pirc522 pkg the py532lib pkg may not be installed and vice-versa
try:
import pirc522
Expand All @@ -36,8 +39,9 @@ def get_devices():
class NonUsbDevice(object):
name = None

def __init__(self, name):
def __init__(self, name, phys=''):
self.name = name
self.phys = phys


class UsbReader(object):
Expand Down Expand Up @@ -159,7 +163,11 @@ class Reader(object):
devices = get_devices()
for device in devices:
for dev_key in device_keys:
dev_name, dev_phys = dev_key.rstrip().split(';', 1)
dev_name_phys = dev_key.rstrip().split(';', 1)
dev_name = dev_name_phys[0]
dev_phys = ''
if len(dev_name) > 1:
dev_phys = dev_name_phys[0]
if device.name == dev_name and device.phys == dev_phys:
if dev_name == 'MFRC522':
self.devs.append(Mfrc522Reader())
Expand All @@ -176,18 +184,23 @@ class Reader(object):
break

def readCard(self):
que = Queue()
que = SimpleQueue()
threads_list = list()

for dev in self.devs:
t = multiprocessing.Process(target=lambda q: q.put(dev.readCard()), args=(que,))
t.start()
threads_list.append(t)

while que.empty():
for read_thread in threads_list:
if not read_thread.is_alive():
found_result = False
while not found_result:
for process in threads_list:
process.join(0.001)
if not process.is_alive():
found_result = True
break

for process in threads_list:
process.terminate()

return que.get()
41 changes: 32 additions & 9 deletions scripts/RegisterDevice.py.Multi
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
#!/usr/bin/env python3

import os.path
from Reader import get_devices
from evdev import InputDevice, list_devices


class NonUsbDevice(object):
name = None

def __init__(self, name, phys=''):
self.name = name
self.phys = phys


def get_devices():
devs = [InputDevice(fn) for fn in list_devices()]
devs.append(NonUsbDevice('MFRC522'))
devs.append(NonUsbDevice('RDM6300'))
devs.append(NonUsbDevice('PN532'))
return devs


list_dev_ids = list()
devices = get_devices()

def addDevice():

def addUsbDevice():
i = 0
print("Choose the reader from list")
for dev in devices:
print(i, dev.name + str(dev.phys))
if i not in list_dev_ids:
print(i, dev.name + str(dev.phys))
i += 1
dev_id = int(input('Device Number: '))
if dev_id not in list_dev_ids:
list_dev_ids.append(dev_id)


addDevice()
while True:
answer = input('Do you want to add another device: [Y/n]')
if not answer or answer[0] != 'Y':
break
addDevice()
def configureDevices():
addUsbDevice()
while True:
answer = input('Do you want to add another device: [Y/n]')
if not answer or answer[0] != 'Y':
break
addUsbDevice()


configureDevices()

path = os.path.dirname(os.path.realpath(__file__))
with open(path + '/deviceName.txt', 'w') as f:
Expand Down