I am working on a project, in which I have to interface multiple RFID Readers (I'm Using EM 18, With Serial out) with Raspberry Pi. I'm using an USB to TTL Converter to connect EM 18 to Raspberry Pi. I've connected two RFID Readers using USB to TTL Adapter. This is my code for one station
Code
import serial, time
while True:
try:
print 'station one Is Ready!! Please Show your Card'
card_dataa = serial.Serial('/dev/ttyUSB0', 9600).read(12)
print card_dataa
continue
except serial.SerialException:
print 'Station 1 is Down'
break
My Issues are
I would like to get the readings from both the RFID Readers in the same program simultaneously.
I've two Programs with the above code, station1.py and station2.py.
Station1.py is for USB0 and Station2.py is for USB1. I am executing the programs in different Terminal simultaneously.
For example station1.py in terminal 1 and station2.py in terminal 2. The program executes fine, but the readings are jumbled. For example, 6E0009D2CC79 and 4E0070792760 are the tag id's that i'm using for testing. if I'm executing only one program i'm getting the reading properly, but if I'm executing both programs simultaneously in two terminals I'm getting the tag Id's jumbled.
- I want to combine both the readings in the same program.
Thanks in Advance
I'd recommend create a new Serial object once and reading multiple times, as needed:
Optionally you can set a timeout of 0:
You should also easily be able to open 2 serial connections in the same program:
This means the 2nd reader would wait for the 1st reader to finish before printing, which is why fingaz recommended threading.
Here's a basic commented proof of concept for threading:
Note that I haven't attached actual hardware to test, so this may not work as is, but should provide enough info to progress.
I would also suggest trying to physically distance the readers. Depending on the readers and their capabilities they may be interfering witch each other which might result in faulty data. In case you still have issues with jumbled data, I'd try to figure out where the problem is by taking some assumptions out.(e.g. is the issue a hardware (readers interfering, broken reader, loose USB connector, etc.) or software(serial port not properly initialized/flushed/etc.), taking one thing out at a time)