Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. import os
  2. import queue
  3. import sys
  4. import threading
  5. import mysql.connector
  6. from time import sleep
  7.  
  8. import serial
  9. from PyQt5 import QtWidgets
  10. from PyQt5 import uic
  11. from PyQt5.QtWidgets import QMessageBox
  12. from datetime import datetime
  13.  
  14. port = '/dev/pts/5'  # define the com port name (to be added in UI)
  15. baud = 9600  # define baud rate
  16. connected = False  # standard connected flag as global
  17. hb_retry=10
  18.  
  19. Serial_Com = serial.Serial(port, baud, timeout=1)  # define serial communication object
  20. WorkQ = queue.Queue(maxsize=100)  # define communication queue
  21.  
  22.  
  23. mydb = mysql.connector.connect(host="localhost",user="root",passwd="hellroot007")
  24. print (mydb)
  25.  
  26. class Ui(QtWidgets.QMainWindow):  # UI class
  27.     def __init__(self):
  28.         super(Ui, self).__init__()
  29.         uic.loadUi('tty_content.ui', self)  # Load UI file
  30.         self.statusBar().showMessage('Message in statusbar.')
  31.  
  32.         self.show()
  33.  
  34.         # starting thread for reading from serial connection
  35.         thread_read_from_com = threading.Thread(target=self.read_from_com, args=(
  36.             Serial_Com,))  # start the thread for getting the payload from COM conn
  37.         thread_read_from_com.daemon = True
  38.         thread_read_from_com.start()
  39.  
  40.         # starting thread for handling incoming data
  41.         thread_read_q_data = threading.Thread(target=self.read_q_data,
  42.                                               args=())  # start the thread to get the actual data from the comm queue and present it in the UI
  43.         thread_read_q_data.daemon = True
  44.         thread_read_q_data.start()
  45.  
  46.         self.show_dialog.clicked.connect(self.clickDialog)
  47.         self.show_values.clicked.connect(self.clickValues)
  48.  
  49.     def clickDialog(self):
  50.         QMessageBox.about(self, "Title", "Message")
  51.  
  52.     def clickValues(self):
  53.         QMessageBox.critical(self, "Testing Values",
  54.                              "Current values: " + str(self.adc_bar_1.value()) + " & " + str(self.adc_bar_2.value()))
  55.  
  56.     def handle_rcvd_data(self,
  57.                          data):  # define function to be called after data is received / payload is added to the queue
  58.         if data == '':  # if payload is empty do nothing
  59.             pass
  60.         else:
  61.             print(data)  # print received data for debug purpose
  62.             WorkQ.put(data)  # put payload in the queue
  63.  
  64.     def read_from_com(self, ser):  # define the function that will get the data from com connection on a separate thread
  65.         global connected
  66.         while not connected:
  67.             connected = True
  68.             while True:
  69.                 read_line = ser.readline().strip()
  70.                 value = read_line.decode('utf-8')
  71.                 if value == 'exit':
  72.                     os._exit(0)
  73.                 else:
  74.                     self.handle_rcvd_data(value)  # call the function to handle the incoming data
  75.                 # sleep(0.5)                                #sleep 1 second (you don't wanna keep the CPU to 100%)
  76.  
  77.     def read_q_data(self):  # define the function that would handle the payload and present it in the UI
  78.         global hb_retry
  79.         while True:
  80.             if WorkQ.empty():
  81.                 hb_retry = hb_retry+1
  82.                 if hb_retry<10:
  83.                     self.status_label.setStyleSheet("QLabel {background-color:yellow;color:black;border-radius: 10px}")
  84.                     self.status_label.setText("WAIT")
  85.                     print (hb_retry)
  86.                 if hb_retry==10:
  87.                     self.status_label.setStyleSheet("QLabel {background-color:red;color:black;border-radius: 10px}")
  88.                     self.status_label.setText("HB NOK")
  89.                     timestampStr = datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)")
  90.                     self.textEdit.append("Error reading value @ " + timestampStr)
  91.                     print(timestampStr)
  92.                     print(hb_retry)
  93.                     hb_retry=0
  94.             else:
  95.                 Payload = WorkQ.get()
  96.                 Values = Payload.split(':')
  97.  
  98.                 # print(Values)
  99.                 if Values[0] == 'V':
  100.                     # self.textEdit.append(WorkQ.get())
  101.                     self.adc_1.display(Values[1])
  102.                     self.adc_2.display(Values[2])
  103.                     sleep(0.01)
  104.                     self.adc_bar_1.setValue(int(Values[1]))
  105.                     self.adc_bar_2.setValue(int(Values[2]))
  106.                 else:
  107.                     self.textEdit.append(Values[0])
  108.                 self.status_label.setStyleSheet("QLabel {background-color:green;color:white;border-radius: 10px;}")
  109.                 self.status_label.setText("HB OK")
  110.                 print(hb_retry)
  111.                 hb_retry=0
  112.             sleep(1)
  113.  
  114.     def Cleanup_App():
  115.         # Ui.thread_read_q_data._exit()
  116.         # thread_read_from_com._exit()
  117.         print("About to quit the app")
  118.  
  119.  
  120. if __name__ == '__main__':
  121.     app = QtWidgets.QApplication(sys.argv)
  122.     window = Ui()
  123.  
  124.     app.aboutToQuit.connect(Ui.Cleanup_App)
  125.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement