USB RFID-card-reader with PYTHON (Raspberry pi)

Viewed 43

I have a rfid reader that is connected to my raspberry pi. I wrote a python program that is reading the id from the rfid card and playing a song that is refering to the id. It works like it is supposed to.

The problem is, that when I put the next card on the reader too early <6s then it gives a long peeping sound 3 times as long as normal about. When I put it on there within the first second, then it peeps even longer and the program hangs up.

I am searching for the mistake since days. It is waiting until a song is done to send in the new id, so it cannot have to do anything with that. I quess I need to reset the connection to the reader somehow, but I don't know how and if thats would solve the problem.

here is my code:

from __future__ import print_function
import sys
import usb.core
import usb.util
#music
import wave
import getopt
import alsaaudio
#thread
from time import sleep
from threading import Thread


#music
def play(device, f):
        print('%d channels, %d sampling rate\n' % (f.getnchannels(), f.getframerate()))
        # 8bit is unsigned in wav files
        if f.getsampwidth() == 1:
                format = alsaaudio.PCM_FORMAT_U8
        # otherwise we assume signed data, little endian
        elif f.getsampwidth() == 2:
                format = alsaaudio.PCM_FORMAT_S16_LE
        elif f.getsampwidth() == 3:
                format = alsaaudio.PCM_FORMAT_S24_LE
        elif f.getsampwidth() == 4:
                format = alsaaudio.PCM_FORMAT_S32_LE
        else:
                raise valueError('Unsupported format')

        periodsize = f.getframerate() //8
        device = alsaaudio.PCM(channels=f.getnchannels(), rate=f.getframerate(), format=format, per>        data = f.readframes(periodsize)
        while data:
                # Read data from stdin
                device.write(data)
                data = f.readframes(periodsize)
def usage():
        print('usage: Sounddatei nicht vorhanden', file=sys.stderr)
        sys.exit(2)

if __name__ == '__main__':
        device = 'default'
        opts, args = getopt.getopt(sys.argv[1:], 'd:')
        for o, a in opts:
                if o == '-d':
                        device = a

# Ende  music
# rfid
dev=usb.core.find(idVendor=0xffff, idProduct=0x0035)
interface=0
endpoint = dev[0][(0,0)][0]

if dev.is_kernel_driver_active(interface) is True:
        dev.detach_kernel_driver(interface)
        usb.util.claim_interface(dev,interface)
# Ende rfid


def rfid_auslesen():

    collected = 0
    attempts = 22
    rfid_id = ""
    while collected < attempts:
            try:
                    data = dev.read(endpoint.bEndpointAddress,endpoint.wMaxPacketSize)
                    print("testread")
                    print(data)
                    collected +=1
                    daten_usb = data
                    rfid_id += str(daten_usb[2])
                    # print (data)
                    if collected == attempts:
                            print (rfid_id)
                            if rfid_id == str("390390390320330300300350380300400"):
                                    print ("ENDE")
                                    
                                 collected = attempts
                                    break
                            else:
                                    if rfid_id == str("390390390320390330320320340370400"):
                                            id_rfid = 'Status_1'
                                            thread_zwei = Thread(target=musik_spielen, args=(id_rfi>                                            thread_zwei.start()
                                            thread_zwei.join()
                                            id_rfid = ''

                                    rfid_id = ""
                                    collected = 0

            except usb.core.USBError as e:
                    data = None
                    if e.args == ('Operation timed out',):
                            continue
    
    usb.util.release_interface(dev,interface)
    dev.attach_kernel_driver(interface)

def musik_spielen(p_id_rfid):
    if(p_id_rfid == 'Status_1'):
        f = wave.open('status_1.wav','rb')
        play(device,f)
        f.close()

thread_eins = Thread(target=rfid_auslesen)
thread_eins.start()

0 Answers
Related