python - Jumbled out put of RFID Tags -
i working on project, in have interface multiple rfid readers (i'm using em 18, serial out) raspberry pi. i'm using usb ttl converter connect em 18 raspberry pi. i've connected 2 rfid readers using usb ttl adapter. code 1 station
code
import serial, time while true: try: print 'station 1 ready!! please show card' card_dataa = serial.serial('/dev/ttyusb0', 9600).read(12) print card_dataa continue except serial.serialexception: print 'station 1 down' break
my issues are
i readings both rfid readers in same program simultaneously.
i've 2 programs above code, station1.py , station2.py.
station1.py usb0 , station2.py usb1. executing programs in different terminal simultaneously.
for example station1.py in terminal 1 , station2.py in terminal 2. program executes fine, readings jumbled. example, 6e0009d2cc79 , 4e0070792760 tag id's i'm using testing. if i'm executing 1 program i'm getting reading properly, if i'm executing both programs simultaneously in 2 terminals i'm getting tag id's jumbled.
- i want combine both readings in same program.
thanks in advance
i'd recommend create new serial object once , reading multiple times, needed:
import serial, time try: station1 = serial.serial('/dev/ttyusb0', 9600) print 'station 1 ready!! please show card' except serial.serialexception: print 'station 1 down' while true: card_dataa = station1.read(12) print card_dataa
optionally can set timeout of 0:
import serial, time try: station1 = serial.serial('/dev/ttyusb0', 9600,timeout=0) print 'station 1 ready!! please show card' except serial.serialexception: print 'station 1 down' while true: card_dataa = station1.read(12) if len(card_dataa) > 0: print card_dataa
you should able open 2 serial connections in same program:
import serial, time station1 = none station2 = none try: station1 = serial.serial('/dev/ttyusb0', 9600,timeout=0) print 'station 1 ready!! please show card' except exception,e: print 'station 1 down',e try: station2 = serial.serial('/dev/ttyusb1', 9600,timeout=0) print 'station 2 ready!! please show card' except exception,e: print 'station 2 down',e while true: if station1 != none: card_dataa1 = station1.read(12) if len(card_dataa1) > 0: print card_dataa1 if station2 != none: card_dataa2 = station2.read(12) if len(card_dataa2) > 0: print card_dataa2
this means 2nd reader wait 1st reader finish before printing, why fingaz recommended threading.
here's basic commented proof of concept threading:
import threading,time,serial #subclass threading.thread class rfidserialthread(threading.thread): sleep_time = 0.001 #setup sleep time (update speed) #constructor 3 parameters: serial port, baud , label reader it's easy spot def __init__(self,port,baud,name): threading.thread.__init__(self) #initialize instance thread self.isalive = false #keep track if thread needs run(is setup , able go) or not self.name = name #potentially handy debugging self.data = "" #a placeholder data read via serial self.station = none #the serial object property/variable try: self.station = serial.serial(port,baud,timeout=0) #attempt initialize serial self.isalive = true #and flag thread running except exception,e: print name + " down",e #in case of errors print message, including station/serial port def run(self): #this gets executed when thread started while self.isalive: if self.station != none: self.data = self.station.read(12) #read serial data time.sleep(rfidserialthread.sleep_time) if __name__ == '__main__': #initialize 2 readers station1 = rfidserialthread('/dev/ttyusb0',9600,"station #1") station2 = rfidserialthread('/dev/ttyusb1',9600,"station #2") #start threads station1.start() station2.start() while true:#continously print data (if any) if len(station1.data) > 0: print station1.name,station1.data if len(station2.data) > 0: print station2.name,station2.data
note haven't attached actual hardware test, may not work is, should provide enough info progress.
i suggest trying physically distance readers. depending on readers , capabilities may interfering witch each other might result in faulty data. in case still have issues jumbled data, i'd try figure out problem taking assumptions out.(e.g. issue hardware (readers interfering, broken reader, loose usb connector, etc.) or software(serial port not initialized/flushed/etc.), taking 1 thing out @ time)
Comments
Post a Comment