我正在开展一个项目,我必须使用Raspberry Pi连接多个RFID阅读器(我正在使用EM 18,带有串口)。我正在使用USB转TTL转换器将EM 18连接到Raspberry Pi。我已经使用USB连接两个RFID阅读器到TTL适配器。 这是我的一个电台的代码
代码
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的混乱。
先谢谢
答案 0 :(得分:2)
我建议您根据需要创建一个新的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
这意味着第二个读者会在打印前等待第一个读者完成,这就是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
请注意,我尚未附加实际硬件进行测试,因此可能无法正常工作,但应提供足够的信息以便进展。
我还建议尝试与读者保持距离。根据读者及其能力,他们可能互相干扰,这可能导致数据错误。如果您仍然遇到混乱数据的问题,我会尝试通过一些假设来找出问题所在。(例如问题是硬件(读卡器干扰,读卡器损坏,USB连接器松动等)或软件(串口未正确初始化/刷新/等),一次取出一件事)
答案 1 :(得分:0)
要拥有并发流,您可以使用threading
模块。这是文档的链接: