关于python3.4和PyQt4以及pyserial的串口小工具

1、首先安装python3.4的工具,下载python-3.4.1.msi文件,点击安装即可
2、下载PyQt4-4.11-gpl-Py3.4-Qt5.3.0-x32.exe文件点击安装
3、下载pyserial-py3k-2.5.win32.exe文件点击安装
代码如下:

# -*- coding: utf-8 -*
import sys
import serial
import serial.tools.list_ports

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class SerialDlg(QDialog):
    def __init__(self,parent=None):
        super(SerialDlg,self).__init__(parent)
        SerialCOMLabel = QLabel(u'串口号')
        self.SerialCOMComboBox = QComboBox()
        self.SerialCOMComboBox.addItems(self.Port_List())


        SerialBaudRateLabel = QLabel(u'波特率')
        self.SerialBaudRateComboBox = QComboBox()
        self.SerialBaudRateComboBox.addItems(['100','300','600','1200','2400','4800','9600','14400','19200','38400','56000','57600','115200','128000','256000'])
        self.SerialBaudRateComboBox.setCurrentIndex(6)

        SerialDataLabel = QLabel(u'数据位')
        self.SerialDataComboBox = QComboBox()
        self.SerialDataComboBox.addItems(['5','6','7','8'])
        self.SerialDataComboBox.setCurrentIndex(3)

        SerialSTOPBitsLabel = QLabel(u'停止位')
        self.SerialStopBitsComboBox = QComboBox()
        self.SerialStopBitsComboBox.addItems(['1','1.5','2'])
        self.SerialStopBitsComboBox.setCurrentIndex(0)

        SerialParityLabel = QLabel(u'奇偶校验位')
        self.SerialParityComboBox = QComboBox()
        self.SerialParityComboBox.addItems(['NONE','EVEN','ODD','MARK','SPACE'])
        self.SerialParityComboBox.setCurrentIndex(0)

        self.OpenButton = QPushButton(u'打开串口')
        self.CloseButton = QPushButton(u'关闭串口')
        self.CloseButton.setDisabled(True)

        buttonLayout = QHBoxLayout()
        buttonLayout.addWidget(self.OpenButton)
        buttonLayout.addWidget(self.CloseButton)
        buttonLayout.addStretch()

        layout = QGridLayout()
        layout.addWidget(SerialCOMLabel,0,0)
        layout.addWidget(self.SerialCOMComboBox,0,1)
        layout.addWidget(SerialBaudRateLabel,1,0)
        layout.addWidget(self.SerialBaudRateComboBox,1,1)
        layout.addWidget(SerialDataLabel,2,0)
        layout.addWidget(self.SerialDataComboBox,2,1)
        layout.addWidget(SerialSTOPBitsLabel,3,0)
        layout.addWidget(self.SerialStopBitsComboBox,3,1)
        layout.addWidget(SerialParityLabel,4,0)
        layout.addWidget(self.SerialParityComboBox,4,1)

        mainlayout = QVBoxLayout()
        mainlayout.addLayout(layout)
        mainlayout.addLayout(buttonLayout)

        self.setLayout(mainlayout)
        self.setWindowTitle(u'串口调试工具')

        self.connect(self.OpenButton,SIGNAL("clicked()"),self.OpenSerial)
     #获取COM号列表  
    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 OpenSerial(self):
        ser = serial.Serial()
        ser.port = self.SerialCOMComboBox.currentText()
        ser.baudrate = self.SerialBaudRateComboBox.currentText()
        ser.bytesize = int(self.SerialDataComboBox.currentText())

        ParityValue = self.SerialParityComboBox.currentText()
        ser.parity = ParityValue[0]
        ser.stopbits = int(self.SerialStopBitsComboBox.currentText())
        ser.open()
        print(ser.isOpen())
        ser.close()
        print(ser.isOpen())


app = QApplication(sys.argv)
SerialDialog = SerialDlg()
SerialDialog.show()
app.exec()

运行过程中会提示一个错误说没有模块serial.tools;也就是无法获取串口列表了,在网上找了很久也没有发现问题所在,后来下载了一个源码pyserial-code-508-trunk文件,文件里有pyserial\serial\tools目录,将此目录拷贝到python3.4的安装目录下的D:\Python34\Lib\site-packages\serial目录下即可,主要tools这个里面的*.py文件是python2中的语法,需要修改部分才能正常运行。
1、list_ports.py文件的83行修改成print (“Filtered list with regexp: %r” % (args[0],))或者注释掉
2、list_ports_windows.py文件的第10行修改成from serial.win32 import *,第240行修改成print (“%s: %s [%s]” % (port, desc, hwid))或者注释点。
这样就可以运行成功了
结果如下:
关于python3.4和PyQt4以及pyserial的串口小工具_第1张图片

你可能感兴趣的:(python)