Saturday, November 12, 2022

Enhancing my Python Serial Monitoring Program by Adding a Screen


Remember my post "Create a Wifi Captive Portal using a NodeMCU"? It has a python program that retrieves the user input from the wifi captive portal over serial port. This tiny program does not have a screen so I need to enhance it a little bit just to demo the threading features of python which I also mentioned in this post: PyQt6 Progress Bar Enhancement. The program is still very simple and very straightforward. The tricky part is the threading, it is what makes this program feasible. I also added 2 buttons(Start and Stop) and the function is quite obvious. 

Here is the screenshot of the screen:

Here is the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
from PyQt6 import QtGui
import serial
#import asyncio
import time
from PyQt6.QtWidgets import QTextEdit, QApplication, QMainWindow, QPushButton, QToolTip, QMessageBox, QLabel
from PyQt6.QtCore import QTimer, QThread, pyqtSignal, Qt
import multiprocessing as mp
arduinodata = ''
breaker = 0
class Window2(QMainWindow):                          
    def __init__(self):

        
        super().__init__()
        self.setWindowTitle("Serial MOnitoring")
        self.top = 200
        self.left = 200
        self.width = 300
        self.height = 350
        self.setGeometry(self.top, self.left, self.width, self.height)
        lbl1a =  QLabel('Credentials', self)
        lbl1a.setGeometry(5, 10, 290, 35)
        lbl1a.setStyleSheet('QLabel {background-color: #0E95A6; color: #d4d4d4;}')
        lbl1a.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.le = QTextEdit(self)
        #self.le.setEnabled(False)
        self.le.setGeometry(5, 50, 290, 250)
        self.pushButton = QPushButton("Start", self)
        self.pushButton.move(40, 313)
        self.stopButton = QPushButton("Stop", self)
        self.stopButton.move(160, 313)
        self.stopButton.setEnabled(False)
        self.stopButton.clicked.connect(self.stop_bot)
        timer = QTimer(self)
        timer.timeout.connect(self.showtext)
        timer.start(1000)
        self.thread()
        self.pushButton.clicked.connect(self.window3)
        self.show()

    def showtext(self):
        global arduinodata
        if arduinodata != '':
           print(arduinodata) 
           self.le.append(arduinodata)
           arduinodata = ''
        
    def stop_bot(self):
        global breaker
        self.pushButton.setEnabled(True)
        self.stopButton.setEnabled(False)
        breaker = 1
            
    def window3(self):
       self.pushButton.setEnabled(False)
       self.stopButton.setEnabled(True)
       self.worker = WorkerThread()
       self.worker.start()       
       #self.worker.update_progress.connect(self.showtext)
       
class WorkerThread(QThread):
   #update_progress = pyqtSignal(str) 
   def run(self):
     global arduinodata, breaker
     ser = serial.Serial('com4', baudrate=9600, timeout=1)
     ser.open
     while True:
         if breaker == 1:
             breaker = 0
             ser.close
             break
         arduinodata1 = ser.readline().decode('ascii')
         
         if arduinodata1 != '':
                arduinodata += arduinodata1         
                print(arduinodata)
     arduinodata1 = ''          
            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window2()
    sys.exit(app.exec())

No comments:

Post a Comment