I have a program in which I use pyqt5, MFRC522 and bluetooth. I have 3 threads, one for the interface, one for the MFRC522 and one for the bluetooth. If I run the MFRC522 and the bluetooth separately they work fine, but if I run both at the same time only the bluetooth works and the MFRC522 stops reading the rfid.
The MFRC522 library used is here https://github.com/pimylifeup/MFRC522-python
Why can't work together? MFRC522 and Bluetooth
This is the code:
from PyQt5.QtCore import QThread, pyqtSignal
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow, QStackedWidget, QWidget
import time
import bluetooth import *
from mfrc522 import SimpleMFRC522
class ThreadBluetooth(QThread):
def __del__(self):
self.wait()
def __init__(self):
super().__init__()
def recv_timeout(self, timeout=0.3):
self.sock.setblocking(0)
total_data = []
data = ''
begin = time.time()
begin2 = time.time()
while 1:
if time.time() - begin2 > 1:
break
if total_data and time.time() - begin > timeout:
break
elif time.time() - begin > timeout * 2:
break
try:
data = self.sock.recv(8192)
if data:
total_data.append(data)
begin = time.time()
else:
time.sleep(0.1)
except:
pass
return b''.join(total_data).decode() # ''.join(total_data)
def run(self):
while True:
bd_addr = "98:D3:D2:72:8B:43"
port = 1
self.sock = BluetoothSocket(RFCOMM)
self.bluetoothConnecting = True
try:
self.sock.connect((bd_addr, port))
while self.bluetoothConnecting:
self.verifyConnection()
data = self.recv_timeout()
except Exception as e:
self.sock.close()
time.sleep(10)
def verifyConnection(self):
try:
self.sock.getpeername()
self.bluetoothConnecting = True
except:
self.bluetoothConnecting = False
self.sock.close()
class RFID (QThread):
signal = pyqtSignal(str)
delay = 2
# terminandoTurno = False
def __del__(self):
self.wait()
def run(self):
while True:
self.readCard()
def readCard(self):
try:
reader = SimpleMFRC522()
while True:
codeRFID, text = reader.read()
self.signal.emit(codeRFID)
time.sleep(self.delay)
except Exception as e:
print(e)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.setGeometry(100, 100, 100, 800, 480)
home = Home(self)
self.central_widget.addWidget(home)
class Home(QWidget):
def __init__(self, parent=None):
super(Home, self).__init__(parent)
uic.loadUi("UI/MainQWidget.ui", self)
self.initBluetooth()
self.initRFID()
def initBluetooth(self):
self.threadBluetooth = ThreadBluetooth()
self.threadBluetooth.start()
def initRFID(self):
self.RFID = RFID()
self.RFID.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show
sys.exit(app.exec_())