I'm doing an access control application in Python. I have a window in PyQt5, which has a running clock and a connection to an Arduino, which is the one that will read a card through its corresponding NFC.
The fact is that for the two tasks (clock and NFC) to work at the same time, I'm trying with 2 timer, one for the clock and one for Arduino, but as soon as I put the one of the Arduino, I'm left hanging Program. If at that time I read card, apparently it works, but the GUI is blocked. I'll put the code here to see if someone can help me out.
import sys, re, time
import serial
import os.path #Comprobar si la BBDD existe
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QMessageBox,
QTableWidget, QTableWidgetItem, QLineEdit
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtCore import Qt
from time import localtime, strftime
#Clase heredada de QMainWindow (Constructor de ventanas)
class Ventana(QDialog):
#Método constructor de la clase
def __init__ (self):
#Iniciar objeto QDialog
QDialog.__init__(self)
#Cargar configuración del archivo .ui en el objeto
uic.loadUi('fichajes.ui', self)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.Tick)
self.timer.start(1000)
self.timer2 = QtCore.QTimer(self)
self.timer2.timeout.connect(self.LeerTarjeta)
self.timer2.start(1000)
self.btnSalir.clicked.connect(self.Salir)
#self.lblTime.setText(strftime("%H:%M:%S", localtime()))
#self.lblDate.setText(strftime("%d-%m-%Y", localtime()))
def Tick(self):
# get the current local time from the PC
self.lblTime.setText(strftime("%H:%M:%S", localtime()))
self.lblDate.setText(strftime("%d-%m-%Y", localtime()))
def LeerTarjeta(self):
# Conectar con Arduino
try:
arduino = serial.Serial('COM4', 9600)
rawString = arduino.readline()
print(rawString)
self.txtUID.setText(str(rawString)[7:-5])
self.Tick()
except Exception:
# Colocado Exception para que detecte el CTRL + C como interrupción
print("Problem with the serial port" + Exception)
def Salir(self):
exit()
# Instancia para iniciar la aplicación (obligatorio pasar argumento)
app = QApplication(sys.argv)
#Crear un objeto de la clase
ventana = Ventana()
#Mostrar ventana
ventana.show()
#Ejecutar la aplicación
app.exec_()