我正在做一个项目,在这个项目中,我必须连接多个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
我的问题是
我想从同一个程序中的两个RFID阅读器同时读取数据。
我有两个程序用上面的代码,station1.py和station2.py。
Station1.py用于USB0,Station2.py用于USB1。 我在不同的终端同时执行程序。在
例如,终端站1.2中的终端py和站点2。程序运行良好,但读数混乱。例如,6E0009D2CC79和4e070792760是我用于测试的标记id。如果我只执行一个程序,我得到正确的读数,但如果我在两个终端同时执行两个程序,我会得到标签Id的混乱。在
提前谢谢
要获得并发流,可以使用
threading
模块。以下是文档链接:https://docs.python.org/2/library/threading.html
我建议创建一个新的串行对象,并根据需要多次读取:
也可以将超时设置为0:
^{pr2}$在同一个程序中也可以轻松打开串行连接:
^{3}$这意味着第二个读卡器将等待第一个读卡器完成打印,这就是为什么fingaz建议使用线程。在
以下是线程化的基本概念证明:
请注意,我还没有附加实际的硬件来测试,所以这可能不能正常工作,但应该提供足够的信息来进行。在
我也建议尽量与读者保持距离。根据读卡器及其功能,它们可能相互干扰,从而导致错误的数据。如果您仍然对混乱的数据有问题,我会尝试通过排除一些假设来找出问题所在(例如,问题是硬件(读卡器干扰、读卡器损坏、USB接口松动等)还是软件(串行端口未正确初始化/刷新等),一次取出一件东西)
相关问题 更多 >
编程相关推荐