0

I made a pyqt5 script which uses python3.6 my scripts takes input from serial device and updates the GUI and the remote database and also take remote input from web browser and make necessary changes to the serial device and update the GUI accordingly. I am using QTimer to run the 4 functions/methods in the background to achieve this and it's working. But the problem is that after receiving a manual input or remote input (from web browser) it takes almost 15-20 seconds to implement the necessary changes in the serial device and the GUI and it makes the GUI almost unresponsive (it responds after almost 15-20 seconds). I tried QThread but it always results in multiple access on the serial port at the same time causing the script to crash. Please, help me in this regard.

from PyQt5.QtCore import QTimer, QThread
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
import os
import sys
import time
import serial
import MySQLdb
from pathlib import Path
import json


ui, _ = loadUiType('myapplication.ui')


class MainApp(QMainWindow, ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)

    def new_method1(self):
        print("This is new_method1")

    def new_method2(self):
        print("This is new_method2")

    def new_method3(self):
        print("This is new_method3")

    def new_method4(self):
        print("This is new_method4")

    def continuous_calling(self):            
        self.timer = QTimer()
        self.timer.timeout.connect(self.new_method1)
        self.timer.start(1000)
        self.timer.timeout.connect(self.new_method2)
        self.timer.start(1000)
        self.timer.timeout.connect(self.new_method3)
        self.timer.start(1000)
        self.timer.timeout.connect(self.new_method4)
        self.timer.start(1000)

def main():
    app = QApplication(sys.argv)
    window = MainApp()
    window.move(0, -30)
    window.show()
    window.continuous_calling()
    app.exec_()



if __name__ == '__main__':
    main()

Here, i want to run new_method1, new_method2, new_method3 & new_method4 methods continuously in the background as long as the GUI is not exiting without freezing the GUI. Thanks.

additional information:: This is an example of other functions that manages direct user inputs from GUI(button clicks from the GUI). And these functions don't run in the background repeatedly. These functions are triggered only when buttons(in the GUI) are clicked.

def ser_conn_on_zero(self):
    self.ser = serial.Serial(
        port=self.comm_port,
        baudrate=9600,
        bytesize=serial.EIGHTBITS,
        parity=serial.PARITY_EVEN,
        stopbits=serial.STOPBITS_ONE,
        timeout=1)
    if self.ser.isOpen():
        print("Open: " + self.ser.name)
        self.ser.flushInput()
        self.ser.flushOutput()
        self.ser.close()
    self.ser.open()
    print('Connected to :' + self.comm_port)
    print("Turning ON Relay Zero")
    self.ser.write(b'\x46\x6D\x45\x63\x73\x4B\x6F\x6C\x00\x05\x00\x01\x00\x01\x00\r\n')
    output = self.ser.readline().decode('iso-8859-1')
    print("Serial Response: " + output)
    self.ser.close()
    print("Serial connection Closed for Relay Zero")
    conn = MySQLdb.connect("XX.XX.XX.XX", "User2", "PASSWORD", "TestSite_001", use_unicode=True, charset="utf8")
    cursor = conn.cursor()
    try:
        cursor.execute("update some_table set chn = %s where id = %s", (1, 1))
        conn.commit()
    except MySQLdb.Error as e:
        print("ser_conn_on_zero: couldn't update the remote database : ", str(e))
    cursor.close()
    conn.close()
jyoti
  • 21
  • 1
  • 9
  • How are you updating the gui when a manual input is received? Is this done from within the four functions? – Heike Oct 02 '19 at 08:05
  • Yes. The received input is processed through those previously mentioned methods and the result is shown in a Label like: ``` self.label_19.setText(str(some_data)) ``` – jyoti Oct 02 '19 at 08:26
  • Sorry, I made a mistake. Remote inputs are processed through those previously mentioned methods but direct user inputs are managed by other functions and those functions don't run periodically in the background and they are triggered when user clicks buttons in the GUI. – jyoti Oct 02 '19 at 08:47
  • So are those other functions causing the delay then? Could you post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of what you are actually doing? – Heike Oct 02 '19 at 08:50
  • No. They're triggered when user clicks buttons so most of the time they are left unused. Apart from that, those functions take very little time. – jyoti Oct 02 '19 at 08:54
  • @Heike I've added an example of the functions that you mentioned before. – jyoti Oct 03 '19 at 06:43
  • @jyoti That's not a [mcve]. You should post a complete script that other people can run so they can try to reproduce the problem and then debug it. – ekhumoro Oct 03 '19 at 16:13
  • @ekhumoro, I can't give you the whole script and apart from that, even if I give you the whole script without that custom made Serial device the script won't run at all. – jyoti Oct 04 '19 at 06:06
  • @dano would you help in this regard? – jyoti Oct 09 '19 at 05:32

0 Answers0