RFID标签乱放

2024-09-30 22:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,在这个项目中,我必须连接多个RFID阅读器(我使用em18,带串行输出)和Raspberry Pi。我用USB转TTL转换器把EM18连接到树莓派。我用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

我的问题是

  1. 我想从同一个程序中的两个RFID阅读器同时读取数据。

  2. 我有两个程序用上面的代码,station1.py和station2.py。

Station1.py用于USB0,Station2.py用于USB1。 我在不同的终端同时执行程序。在

例如,终端站1.2中的终端py和站点2。程序运行良好,但读数混乱。例如,6E0009D2CC79和4e070792760是我用于测试的标记id。如果我只执行一个程序,我得到正确的读数,但如果我在两个终端同时执行两个程序,我会得到标签Id的混乱。在

  1. 我想在同一个程序中合并两个读数。在

提前谢谢


Tags: 项目代码py程序终端serialcardrfid
2条回答

要获得并发流,可以使用threading模块。以下是文档链接:

https://docs.python.org/2/library/threading.html

我建议创建一个新的串行对象,并根据需要多次读取:

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:

^{pr2}$

在同一个程序中也可以轻松打开串行连接:

^{3}$

这意味着第二个读卡器将等待第一个读卡器完成打印,这就是为什么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接口松动等)还是软件(串行端口未正确初始化/刷新等),一次取出一件东西)

相关问题 更多 >