Link to home
Start Free TrialLog in
Avatar of Dier Eluom
Dier Eluom

asked on

Python and RFID

I have a cheap Microsoft RFID reader from eBay.  I am using 125mhz chips.  I have a Python program that writes the card number and date to a text file on a server.  Groups of people scan their card and it writes each number and date on a new line in this text file.  Sometimes however, it writes a string of numbers all on the same line with just one date at the end.  Could the problem be that a delay should be written into the Python program between reading the card and writing it - say 100ms?
Avatar of gelonida
gelonida
Flag of France image

Not sure, but can it be, that you execute the script multiple times in parallel and that two parallel writes are the root cause?

parallel writes could explain the behaviour you mention.
It could even explain loss of data.

If that would be the case file locking would solve the issue.

Make also sure, that you close the file immediately after having written to it.

Ideally you use file locking.
you collect the data and open the file only after having fetched the data it is also important, that you close the file immediately after your write.
Then you can unlock again.
Avatar of Dier Eluom
Dier Eluom

ASKER

from Tkinter import *
from sqlite3 import *
from tkFileDialog import askopenfilename, askdirectory
import re
from datetime import *

comma = ', '

classroom = 'testpi'
text_document = '/home/pi/ShareFile/event_tracker_data/testpi.txt'
#'/media/pi/VERBATIM/revised attendance/toilet.txt'

def tkinter_window():
    def read():
        csv_doc = open(text_document, "r")
        ## test
        print csv_doc.read()
        
    ##---- tkinter
    add_toilet_tkinter = Tk()
    add_toilet_tkinter.title('toilet movment')

    ## frames
    add_frame = Frame(add_toilet_tkinter)

    ## add
    add_entry = Entry(add_frame)
    add_entry.pack()

    print_file = Button(add_frame, text = 'read', command = lambda: read())
    print_file.pack()
    
    
    ##frame pack
    add_frame.pack()

    ## bind + mainloop
    def add_bind(event):
        global comma
        global classroom
        
        rfid = add_entry.get()
        
        #---- AutoFill the date, month, and year
        date_time = datetime.now()
        date_time_regax = "[0-9-:]+"
        all_date_time = re.findall(date_time_regax, str(date_time))
        
        time_for_db = all_date_time[1]
        date_for_db = all_date_time[0]

       
        csv_doc = open(text_document, "a")

        ##
        ## create the entry ('Rfid, first name, last name, name of place!)
        entry = classroom + comma + rfid + comma + comma + comma + comma + time_for_db + comma + date_for_db
        print entry
        csv_doc.write(entry + "\r\n")
        csv_doc.close()


        ## clear entry for new
        add_entry.delete(0, END)
        
    
    add_entry.focus() 
    add_toilet_tkinter.bind("<Return>", add_bind)
    add_toilet_tkinter.mainloop()


## start program

tkinter_window()
    
    

Open in new window

What is happening is this:

Each entry should be on a new line and look like this:

0268340542, , , , 11:28:41, 2017-06-07

I get a lot like this and then it does the following:

026835201202683520560268339729026833971602683537820268339746026835368202683500370268351993026833974102683397410268350159026835013402683375290268350089026835203902683501050268350105, , , , 09:25:48, 2017-06-12

So instead of recording each RFID card it puts them all into the one line with one , , ,  , time and date at the end.
So would the above be a file locking problem and if so, what do I need to change in the code to fix it?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.