|
- # coding=utf-8
- import sys
- from PyQt4 import QtCore, QtGui, uic
- import serial
- import serial.tools.list_ports
- from thread_1 import Threadq
- import threading
- qtCreatorFile = "serial.ui" # Enter file
- Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
- class MyApp(QtGui.QMainWindow, Ui_MainWindow):
- def __init__(self):
- QtGui.QMainWindow.__init__(self)
- Ui_MainWindow.__init__(self)
- self.setupUi(self)
- self.setWindowTitle(u'终端配置软件') #修改窗口名字
-
- self.comboBox1.addItems(self.port_list())
- self.comboBox2.addItems(['1200','2400','4800','9600','14400','19200','38400','56000','57600','115200'])
- self.comboBox2.setCurrentIndex(3)
- self.comboBox3.addItems(['5','6','7','8'])
- self.comboBox3.setCurrentIndex(3)
- self.comboBox4.addItems(['1','1.5','2'])
- self.comboBox4.setCurrentIndex(0)
- self.comboBox5.addItems(['NONE','EVEN','ODD','MARK','SPACE'])
- self.comboBox5.setCurrentIndex(0)
- self.pushButton1.clicked.connect(self.open_serial)
- self.pushButton2.clicked.connect(self.close_serial)
- self.pushButton1.setDisabled(False)
- self.pushButton2.setDisabled(True)
- self.ser=serial.Serial()
- self.thread=None
- self.alive=threading.Event()
- def port_list(self):
- com_list=[]
- port_list=list(serial.tools.list_ports.comports())
- for port in port_list:
- com_list.append(port[0])
- return com_list
- def open_serial(self):
- self.ser.port=str(self.comboBox1.currentText())#端口
- self.ser.baudrate = self.comboBox2.currentText()#波特率
- self.ser.bytesize=int(self.comboBox3.currentText())#数据位
- self.ser.stopbits = int(self.comboBox4.currentText())
- self.ser.parity=str(self.comboBox5.currentText())[0]#字符串的第一个字母
- self.ser.timeout=None
- self.ser.open()
- self.pushButton1.setDisabled(True)
- self.pushButton2.setDisabled(False)
- print(self.ser.isOpen())
- self.thread=Threadq(target=self.read_serial,name='read_serial')
- self.thread.setDaemon(1)
- self.alive.set()
- self.thread.start()
- def close_serial(self):
- self.ser.close()
- self.pushButton1.setDisabled(False)
- self.pushButton2.setDisabled(True)
- print(self.ser.isOpen())
- if self.thread is not None:
- self.alive.clear()
- self.thread.terminate()
- self.thread.join()
- def send_serial(self,text):
- if self.alive.isSet():
- self.ser.write(text)
-
-
- def read_serial(self):
-
-
- while self.alive.isSet():
-
-
- text=self.ser.read(30)#读取一个字符
- if text:
- print(text)
- self.send_serial(text)
-
-
- if __name__=='__main__':
- app = QtGui.QApplication(sys.argv)
- window = MyApp()
- window.show()
- sys.exit(app.exec_())
复制代码 |
|