乱七八糟的 RFID 标签
Jumbled out put of RFID Tags
我正在开展一个项目,其中我必须将多个 RFID 阅读器(我使用的是 EM 18,带有串行输出)与 Raspberry Pi 连接。我正在使用 USB 转 TTL 转换器将 EM 18 连接到 Raspberry Pi。我已经使用 USB 转 TTL 适配器连接了两个 RFID 阅读器。
这是我的一站代码
代码
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
我的问题是
我想在同一个程序中同时获取两个 RFID 阅读器的读数。
我有两个包含上述代码的程序,station1.py 和 station2.py。
Station1.py 用于 USB0,Station2.py 用于 USB1。
我在不同的终端同时执行程序。
例如终端1中的station1.py和终端2中的station2.py。程序执行正常,但读数混乱。例如,6E0009D2CC79 和 4E0070792760 是我用于测试的标签 ID。如果我只执行一个程序,我就能正确读取,但如果我在两个终端同时执行两个程序,我就会得到标签 ID 的混乱。
- 我想在同一个程序中合并这两个读数。
提前致谢
要有并发流,可以使用threading
模块。这是文档的 link:
我建议根据需要创建一个新的 Serial 对象一次并多次读取:
import serial, time
try:
station1 = serial.Serial('/dev/ttyUSB0', 9600)
print 'station one Is Ready!! Please Show your Card'
except serial.SerialException:
print 'Station 1 is Down'
while True:
card_dataa = station1.read(12)
print card_dataa
您可以选择将超时设置为 0:
import serial, time
try:
station1 = serial.Serial('/dev/ttyUSB0', 9600,timeout=0)
print 'station one Is Ready!! Please Show your Card'
except serial.SerialException:
print 'Station 1 is Down'
while True:
card_dataa = station1.read(12)
if len(card_dataa) > 0: print card_dataa
您还应该能够在同一个程序中轻松打开 2 个串行连接:
import serial, time
station1 = None
station2 = None
try:
station1 = serial.Serial('/dev/ttyUSB0', 9600,timeout=0)
print 'station 1 Is Ready!! Please Show your Card'
except Exception,e:
print 'Station 1 is Down',e
try:
station2 = serial.Serial('/dev/ttyUSB1', 9600,timeout=0)
print 'station 2 Is Ready!! Please Show your Card'
except Exception,e:
print 'Station 2 is 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
这意味着第二个 reader 会等待第一个 reader 完成后再打印,这就是 fingaz 推荐线程的原因。
这是线程概念的基本注释证明:
import threading,time,serial
#subclass threading.Thread
class RFIDSerialThread(threading.Thread):
SLEEP_TIME = 0.001 #setup sleep time (update speed)
#constructor with 3 parameters: serial port, baud and a label for this reader so it's easy to spot
def __init__(self,port,baud,name):
threading.Thread.__init__(self) #initialize this instance as a thread
self.isAlive = False #keep track if the thread needs to run(is setup and able to go) or not
self.name = name #potentially handy for debugging
self.data = "" #a placeholder for the data read via serial
self.station = None #the serial object property/variable
try:
self.station = serial.Serial(port,baud,timeout=0) #attempt to initialize serial
self.isAlive = True #and flag the thread as running
except Exception,e:
print name + " is Down",e #in case of errors print the message, including the station/serial port
def run(self): #this gets executed when the thread is 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 the two readers
station1 = RFIDSerialThread('/dev/ttyUSB0',9600,"station #1")
station2 = RFIDSerialThread('/dev/ttyUSB1',9600,"station #2")
#start the threads
station1.start()
station2.start()
while True:#continously print the data (if any)
if len(station1.data) > 0: print station1.name,station1.data
if len(station2.data) > 0: print station2.name,station2.data
请注意,我没有附加实际硬件进行测试,因此这可能无法正常工作,但应该提供足够的信息来进行测试。
我还建议尝试与 reader 保持物理距离。根据 reader 及其功能,它们可能会相互干扰 ,这可能会导致数据错误。如果您仍然遇到混乱数据的问题,我会尝试通过排除一些假设来找出问题出在哪里。(例如,问题是硬件(readers 干扰,损坏 reader, USB 接头松动等)或软件(串口不正常initialized/flushed/etc。),一次取出一个东西)
我正在开展一个项目,其中我必须将多个 RFID 阅读器(我使用的是 EM 18,带有串行输出)与 Raspberry Pi 连接。我正在使用 USB 转 TTL 转换器将 EM 18 连接到 Raspberry Pi。我已经使用 USB 转 TTL 适配器连接了两个 RFID 阅读器。 这是我的一站代码
代码
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
我的问题是
我想在同一个程序中同时获取两个 RFID 阅读器的读数。
我有两个包含上述代码的程序,station1.py 和 station2.py。
Station1.py 用于 USB0,Station2.py 用于 USB1。 我在不同的终端同时执行程序。
例如终端1中的station1.py和终端2中的station2.py。程序执行正常,但读数混乱。例如,6E0009D2CC79 和 4E0070792760 是我用于测试的标签 ID。如果我只执行一个程序,我就能正确读取,但如果我在两个终端同时执行两个程序,我就会得到标签 ID 的混乱。
- 我想在同一个程序中合并这两个读数。
提前致谢
要有并发流,可以使用threading
模块。这是文档的 link:
我建议根据需要创建一个新的 Serial 对象一次并多次读取:
import serial, time
try:
station1 = serial.Serial('/dev/ttyUSB0', 9600)
print 'station one Is Ready!! Please Show your Card'
except serial.SerialException:
print 'Station 1 is Down'
while True:
card_dataa = station1.read(12)
print card_dataa
您可以选择将超时设置为 0:
import serial, time
try:
station1 = serial.Serial('/dev/ttyUSB0', 9600,timeout=0)
print 'station one Is Ready!! Please Show your Card'
except serial.SerialException:
print 'Station 1 is Down'
while True:
card_dataa = station1.read(12)
if len(card_dataa) > 0: print card_dataa
您还应该能够在同一个程序中轻松打开 2 个串行连接:
import serial, time
station1 = None
station2 = None
try:
station1 = serial.Serial('/dev/ttyUSB0', 9600,timeout=0)
print 'station 1 Is Ready!! Please Show your Card'
except Exception,e:
print 'Station 1 is Down',e
try:
station2 = serial.Serial('/dev/ttyUSB1', 9600,timeout=0)
print 'station 2 Is Ready!! Please Show your Card'
except Exception,e:
print 'Station 2 is 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
这意味着第二个 reader 会等待第一个 reader 完成后再打印,这就是 fingaz 推荐线程的原因。
这是线程概念的基本注释证明:
import threading,time,serial
#subclass threading.Thread
class RFIDSerialThread(threading.Thread):
SLEEP_TIME = 0.001 #setup sleep time (update speed)
#constructor with 3 parameters: serial port, baud and a label for this reader so it's easy to spot
def __init__(self,port,baud,name):
threading.Thread.__init__(self) #initialize this instance as a thread
self.isAlive = False #keep track if the thread needs to run(is setup and able to go) or not
self.name = name #potentially handy for debugging
self.data = "" #a placeholder for the data read via serial
self.station = None #the serial object property/variable
try:
self.station = serial.Serial(port,baud,timeout=0) #attempt to initialize serial
self.isAlive = True #and flag the thread as running
except Exception,e:
print name + " is Down",e #in case of errors print the message, including the station/serial port
def run(self): #this gets executed when the thread is 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 the two readers
station1 = RFIDSerialThread('/dev/ttyUSB0',9600,"station #1")
station2 = RFIDSerialThread('/dev/ttyUSB1',9600,"station #2")
#start the threads
station1.start()
station2.start()
while True:#continously print the data (if any)
if len(station1.data) > 0: print station1.name,station1.data
if len(station2.data) > 0: print station2.name,station2.data
请注意,我没有附加实际硬件进行测试,因此这可能无法正常工作,但应该提供足够的信息来进行测试。
我还建议尝试与 reader 保持物理距离。根据 reader 及其功能,它们可能会相互干扰 ,这可能会导致数据错误。如果您仍然遇到混乱数据的问题,我会尝试通过排除一些假设来找出问题出在哪里。(例如,问题是硬件(readers 干扰,损坏 reader, USB 接头松动等)或软件(串口不正常initialized/flushed/etc。),一次取出一个东西)