PyQt下多线程使用案例

Posted Tequila Sunrise

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt下多线程使用案例相关的知识,希望对你有一定的参考价值。

程序的主要功能
   1.串口接收数据
   2.解析串口指令帖并得到命令参数
   3。根据命令参数更新GUI
  其中1和2在一个线程,3在一个线程。
指令格式  [int(\'7e\',16),int(\'7e\',16),0,0,0,0,0,0,int(\'8e\',16),int(\'8e\',16)]。中间的为数据,两端为帖头和帧尾,没有加检验。

GUI如图

tmp

  1 ##-*- coding: utf-8-*
  2 #!D:\\ProgramData\\Anaconda2\\python.exe
  3 """
  4 Author : LEON
  5 EMAIL:yangli0534@yahoo.com
  6 程序的主要功能
  7 1.串口接收数据
  8 2.解析串口指令帖并得到命令参数
  9 3。根据命令参数更新GUI
 10 其中1和2在一个线程,3在一个线程
 11 """
 12 import sys
 13 import math
 14 import time
 15 import serial
 16 from PyQt5 import QtCore, QtGui, uic, QtPrintSupport, QtWidgets
 17 import serial.tools.list_ports
 18 from  threading import Thread
 19 from PyQt5.QtCore import *
 20 from PyQt5.QtGui import *
 21 from PyQt5.QtWidgets import *
 22  
 23 
 24 qtCreatorFile = "panel.ui"# GUI file
 25 Ui_MainWindow, QtBaseClass=uic.loadUiType(qtCreatorFile)
 26 
 27 class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
 28     angle = pyqtSignal(dict) # thread signal 
 29     
 30     def __init__(self):
 31          
 32         QtWidgets.QMainWindow.__init__(self)
 33         Ui_MainWindow.__init__(self)
 34         QThread.__init__(self)
 35         # bkg = QtGui.QPalette()
 36         # bkg.setColor(self.backgroundRole(),QColor(192,253,123))
 37         # self.setPalette(bkg)
 38         self.setupUi(self)
 39         self.ser =serial.Serial()#serial port handle
 40         port_list = list(serial.tools.list_ports.comports())#find serial port avaliable
 41         NoSerial = len(port_list)#number of avaliable serial port 
 42         if  NoSerial> 0:
 43             for port in port_list:
 44                 self.comboBox_serial_No.addItem(port[0])#add to comboBox
 45         baudrate = [9600,14400, 19200, 38400, 56000, 57600, 115200]#baudrate
 46         for baud in baudrate:
 47             self.comboBox_serial_baudrate.addItem(str(baud))
 48         self.comboBox_serial_baudrate.setCurrentIndex(len(baudrate)-1)#default baudrate
 49         self.comboBox_serial_No.setCurrentIndex(NoSerial-1)#default port
 50         self.pushButton_open.clicked.connect(self.open)    #serial port open event connted to button clicked
 51         
 52         self.pushButton_send.clicked.connect(self.send)    #send data event
 53         #self.lcdNumber.display(ord(\'a\'))
 54         #self.param = {"p1":0,"p2":0,"p3":0}
 55         self.para1 = 0 # cmd initialization
 56         self.para2 = 0
 57         self.para3 = 0
 58         self.para4 = 0
 59         self.para5 = 0
 60         self.para6 = 0
 61         #self.angle.connect(self.lcdNumber.display)
 62         self.angle.connect(self.paraP)
 63         #self.angle.connect(self.label_3.setText)
 64         self.sec = 0
 65         #self.dial.setValue(90)
 66         #self.label_tip_2.valueChanged.connect(self.dial.setValue())
 67         #self.dial.setFocusPolicy(Qt.StrongFocus)
 68         #self.angle.connect(self.dial.setValue)
 69         #self.dial.valueChanged.connect(self.slider.setValue)
 70         #con = threading.Condition()
 71         #rece=threading.Thread(target=self.receive,args=())
 72         #rece.start()
 73         self.threads =[]
 74         t1 = Thread(target=self.receive)
 75         t1.setDaemon(True) #
 76         t1.start()#start receiving data thread
 77         self.threads.append(t1) 
 78         t2 =Thread(target=self.updateP) 
 79         t2.setDaemon(True)
 80         t2.start() # start update GUI thread
 81         self.threads.append(t2) 
 82         #self.updateP()
 83                     
 84     def open(self):#open serial port
 85         port = self.comboBox_serial_No.currentText()
 86         baudrate = int(self.comboBox_serial_baudrate.currentText())
 87         if not self.ser.isOpen():
 88             #self.label_tip.setText(\'this port has been opened\')
 89             #self.ser.close()
 90             #self.pushButton_open.setText(\'已断开\')
 91             #self.label_tip.setText(\'closed\')
 92         #else:
 93             self.ser = serial.Serial(port, baudrate, timeout = 10)
 94             if self.ser.isOpen():
 95                 self.pushButton_open.setText(\'已连接\')
 96                 self.label_tip.setText(\'open sucessed!\')
 97                 self.ser.write(\'roger!\')
 98             else:
 99                 self.label_tip.setText(\'open failed!\')        
100     def send(self):    #send data
101         if self.ser.isOpen():
102             text = self.lineEdit_sendBuffer.text()
103             self.ser.write(str(text))
104     def receive(self):# receive data from uart and analysis the protocol frame to get the parameter 
105         #, and then emit to main thread
106         #self.sec += 1
107         #self.lcdNumber.display(self.sec)
108         fsm = 0 
109         info = 0
110         while True:
111              if self.ser.isOpen():
112                  try:
113                     text = self.ser.read(1) #read one byte from uart
114                     if not text :
115                          break
116                     else:
117                         #self.label_3.setText(str(fsm))
118                         text = ord(text)
119                         if fsm == 0:#frame analysis FSM
120                             if text ==int(\'7e\',16):
121                                 fsm = 1
122                             else:
123                                 fsm = 0
124                         elif fsm == 1:
125                             info = {"p1":0,"p2":0,"p3":0,"p4":0,"p5":0,"p6":0}
126                             if text ==int(\'7e\',16):
127                                 fsm = 2
128                             else:
129                                 fsm = 0
130                         elif fsm == 2:
131                             info["p1"]=text
132                             fsm = 3
133                         elif fsm == 3:
134                             info["p2"]=text
135                             fsm = 4
136                         elif fsm == 4:
137                             info["p3"]=text
138                             fsm = 5    
139                         elif fsm == 5:
140                             info["p4"]=text
141                             fsm = 6
142                         elif fsm == 6:
143                             info["p5"]=text
144                             fsm = 7
145                         elif fsm == 7:
146                             info["p6"]=text
147                             fsm = 8        
148                         elif fsm == 8:
149                             if text ==int(\'8e\',16):
150                                 fsm = 9
151                             elif text ==int(\'7e\',16):
152                                 fsm = 1
153                             else:
154                                 fsm = 0
155                         elif fsm ==9:
156                             if text ==int(\'8e\',16):
157                                 fsm = 0
158                                 self.angle.emit(dict(info))#emit signal to main programe
159                             elif text ==int(\'7e\',16):
160                                 fsm = 1
161                             else:
162                                 fsm = 0
163                          #self.angle.emit(str(text))
164                          #self.dial.setValue(int(text))
165                          #self.angle.emit(str((text)))
166                          
167                          #self.angle1.emit(int(text))
168                  except:
169                      break
170             
171             #if self.ser.isOpen():
172              # try:
173                 # text = self.ser.read(1) 
174                 # if not text :
175                      # break
176                 # else:
177                     # self.angle.emit(str(ord(text)))
178                     # #self.dial.setValue(int(text))
179                     # #self.angle.emit(str((text)))
180                     
181                     # #self.angle1.emit(int(text))
182              # except:
183                  # break
184     def paraP(self,r_dict):#update GUI
185         self.para1 = r_dict["p1"]*256+r_dict["p2"]
186         self.para2 = r_dict["p3"]*256+r_dict["p4"]
187         self.para3 = r_dict["p5"]*256+r_dict["p6"]
188         #self.para4 = r_dict["p4"]
189         #self.para5 = r_dict["p5"]
190         #self.para6 = r_dict["p6"]
191     def updateP(self):
192         #self.sec += 1
193         #self.lcdNumber.display(self.sec)
194         while True:
195             # if self.sec < 180:
196                 # self.sec += 1
197             # else:
198                 # self.sec = 0
199             #time.sleep(0.5)
200             #print("Thread update--sec="),
201             #print(self.sec),
202             #print("tmp="),
203             time.sleep(1.0/10.00)
204             tmp =str(1.0*self.para1/100.0)
205             self.lcdNumber_2.display(tmp)
206             tmp =str(1.0*self.para2/100.0)
207             self.lcdNumber_3.display(tmp)
208             tmp =str(1.00*self.para3/100.00)
209             self.lcdNumber.display(tmp)
210             #print(tmp)
211             #self.label_tip_3.setText(tmp)
212             #print(len(tmp))
213             #if len(tmp) <4:
214             tmp = int(str(self.para3),10)
215                 #print("Number = "),
216                 #print(tmp)
217                 #self.label_3.setText(str(tmp))
218                 #if tmp > 0 and tmp < 180:
219             self.dial.setValue(tmp)
220                     
221                     
222 if __name__ == "__main__":
223     app = QtWidgets.QApplication(sys.argv)
224     w = MyApp()
225     w.show()
226     #for t in w.threads:
227         #t.setDaemon(True)
228         #t.start()
229     #w.updateP()    
230     # while True:
231         # time.sleep(0.1)
232         # tmp =str(w.label_tip_2.text())
233         # if tmp.isdigit():
234             # tmp = int(tmp,10)
235             # if tmp > 0 and tmp < 180 :
236                  # w.dial.setValue(tmp)
237 #                self.lcdNumber.display(tmp)
238     sys.exit(app.exec_())
239     

以上是关于PyQt下多线程使用案例的主要内容,如果未能解决你的问题,请参考以下文章

python下多线程是鸡肋,推荐使用多进程 代码示例

爬虫之多线程案例

OpenCV-PyQT项目实战(12)项目案例08:多线程视频播放

OpenCV-PyQT项目实战(12)项目案例08:多线程视频播放

多线程糗事百科案例

zeromq学习记录vc下多线程