├── LICENSE ├── README.md └── serialDebug ├── SerialClass.py └── MainSerial.py /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python串口调试程序 2 | 3 | * 本程序主要是用来调试串口的,由于写的比较匆忙,所以程序中还有很多问题,如果你对这个感兴趣,欢迎一起来完善它。 4 | 5 | * CSDN博客地址:[工程解析](https://blog.csdn.net/CHQC388/article/details/104292183) 6 | 7 | * 博客园地址:[工程解析](https://www.cnblogs.com/zhicungaoyuan-mingzhi/p/12303229.html) 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /serialDebug/SerialClass.py: -------------------------------------------------------------------------------- 1 | ''' 2 | @ author: summer 3 | @ tools: pycharm 4 | @ content: 串口通讯实现类 5 | @ date: 2020.2.12 6 | ''' 7 | import serial 8 | import serial.tools.list_ports 9 | 10 | class SerialAchieve: 11 | def __init__(self,band=115200,check="无校验位",data=8,stop=1): 12 | self.port = None 13 | # 获取可用串口 14 | self.port_list = list(serial.tools.list_ports.comports()) 15 | assert (len(self.port_list) != 0),"无可用串口" 16 | 17 | self.bandRate = band 18 | self.checkbit = check 19 | self.databit = data 20 | self.stopbit = stop 21 | 22 | # 读写的数据 23 | self.read_data = None 24 | self.write_data = None 25 | 26 | pass 27 | def show_port(self): 28 | for i in range(0,len(self.port_list)): 29 | print(self.port_list[i]) 30 | 31 | def show_other(self): 32 | print("波特率:"+self.bandRate) 33 | print("校验位:" + self.checkbit) 34 | print("数据位:" + self.databit) 35 | print("停止位:" + self.stopbit) 36 | # 返回串口 37 | def get_port(self): 38 | return self.port_list 39 | # 打开串口 40 | def open_port(self,port): 41 | self.port = serial.Serial(port, self.bandRate,timeout = None) 42 | 43 | def delete_port(self): 44 | if self.port != None: 45 | self.port.close() 46 | print("关闭串口完成") 47 | else: 48 | pass 49 | 50 | def Read_data(self): # self.port.read(self.port.in_waiting) 表示全部接收串口中的数据 51 | self.read_data = self.port.read(self.port.in_waiting) # 读取数据 52 | return self.read_data.decode("utf-8") 53 | 54 | def Write_data(self,data): 55 | if self.port.isOpen() == False: 56 | print("串口打开错误") 57 | else: 58 | self.port.write(data.encode("utf-8")) # 返回的是写入的字节数 59 | 60 | if __name__ == '__main__': 61 | myser = SerialAchieve() 62 | myser.open_port("COM7") 63 | myser.delete_port() 64 | myser.show_port() 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /serialDebug/MainSerial.py: -------------------------------------------------------------------------------- 1 | ''' 2 | @ author: summer 3 | @ tools: pycharm 4 | @ content: 实现串口通讯主类 5 | @ date: 2020.2.12 6 | ''' 7 | import tkinter 8 | from tkinter import ttk 9 | from SerialClass import SerialAchieve # 导入串口通讯类 10 | 11 | class MainSerial: 12 | def __init__(self): 13 | # 定义串口变量 14 | self.port = None 15 | self.band = None 16 | self.check = None 17 | self.data = None 18 | self.stop = None 19 | self.myserial = None 20 | 21 | # 初始化窗体 22 | self.mainwin = tkinter.Tk() 23 | self.mainwin.title("串口调试工具") 24 | self.mainwin.geometry("600x400") 25 | 26 | # 标签 27 | self.label1 = tkinter.Label(self.mainwin,text = "串口号:",font = ("宋体",15)) 28 | self.label1.place(x = 5,y = 5) 29 | self.label2 = tkinter.Label(self.mainwin, text="波特率:", font=("宋体", 15)) 30 | self.label2.place(x=5, y=45) 31 | self.label3 = tkinter.Label(self.mainwin, text="校验位:", font=("宋体", 15)) 32 | self.label3.place(x=5, y=85) 33 | self.label4 = tkinter.Label(self.mainwin, text="数据位:", font=("宋体", 15)) 34 | self.label4.place(x=5, y=125) 35 | self.label5 = tkinter.Label(self.mainwin,text = "停止位:",font = ("宋体",15)) 36 | self.label5.place(x = 5,y = 165) 37 | 38 | # 文本显示,清除发送数据 39 | self.label6 = tkinter.Label(self.mainwin, text="发送数据:", font=("宋体", 15)) 40 | self.label6.place(x=230, y=5) 41 | 42 | self.label7 = tkinter.Label(self.mainwin, text="接收数据:", font=("宋体", 15)) 43 | self.label7.place(x=230, y=200) 44 | 45 | # 串口号 46 | self.com1value = tkinter.StringVar() # 窗体中自带的文本,创建一个值 47 | self.combobox_port = ttk.Combobox(self.mainwin, textvariable=self.com1value, 48 | width = 10,font = ("宋体",13)) 49 | # 输入选定内容 50 | self.combobox_port["value"] = [""] # 这里先选定 51 | 52 | self.combobox_port.place(x = 105,y = 5) # 显示 53 | 54 | # 波特率 55 | self.bandvalue = tkinter.StringVar() # 窗体中自带的文本,创建一个值 56 | self.combobox_band = ttk.Combobox(self.mainwin, textvariable=self.bandvalue, width=10, font=("宋体", 13)) 57 | # 输入选定内容 58 | self.combobox_band["value"] = ["4800","9600","14400","19200","38400","57600","115200"] # 这里先选定 59 | self.combobox_band.current(6) # 默认选中第0个 60 | self.combobox_band.place(x=105, y=45) # 显示 61 | 62 | # 校验位 63 | self.checkvalue = tkinter.StringVar() # 窗体中自带的文本,创建一个值 64 | self.combobox_check = ttk.Combobox(self.mainwin, textvariable=self.checkvalue, width=10, font=("宋体", 13)) 65 | # 输入选定内容 66 | self.combobox_check["value"] = ["无校验位"] # 这里先选定 67 | self.combobox_check.current(0) # 默认选中第0个 68 | self.combobox_check.place(x=105, y=85) # 显示 69 | 70 | # 数据位 71 | self.datavalue = tkinter.StringVar() # 窗体中自带的文本,创建一个值 72 | self.combobox_data = ttk.Combobox(self.mainwin, textvariable=self.datavalue, width=10, font=("宋体", 13) ) 73 | # 输入选定内容 74 | self.combobox_data["value"] = ["8", "9", "0"] # 这里先选定 75 | self.combobox_data.current(0) # 默认选中第0个 76 | self.combobox_data.place(x=105, y=125) # 显示 77 | 78 | # 停止位 79 | self.stopvalue = tkinter.StringVar() # 窗体中自带的文本,创建一个值 80 | self.combobox_stop = ttk.Combobox(self.mainwin, textvariable=self.stopvalue, width=10, font=("宋体", 13)) 81 | # 输入选定内容 82 | self.combobox_stop["value"] = ["1", "0"] # 这里先选定 83 | self.combobox_stop.current(0) # 默认选中第0个 84 | self.combobox_stop.place(x=105, y=165) # 显示 85 | 86 | # 按键显示,打开串口 87 | self.button_OK = tkinter.Button(self.mainwin, text="打开串口", 88 | command=self.button_OK_click, font = ("宋体",13), 89 | width = 10,height = 1) 90 | self.button_OK.place(x = 5,y = 210) # 显示控件 91 | # 关闭串口 92 | self.button_Cancel = tkinter.Button(self.mainwin, text="关闭串口", # 显示文本 93 | command=self.button_Cancel_click, font = ("宋体",13), 94 | width=10, height=1) 95 | self.button_Cancel.place(x = 120,y = 210) # 显示控件 96 | 97 | # 清除发送数据 98 | self.button_Cancel = tkinter.Button(self.mainwin, text="清除发送数据", # 显示文本 99 | command=self.button_clcSend_click, font=("宋体", 13), 100 | width=13, height=1) 101 | self.button_Cancel.place(x=400, y=2) # 显示控件 102 | 103 | # 清除接收数据 104 | self.button_Cancel = tkinter.Button(self.mainwin, text="清除接收数据", # 显示文本 105 | command=self.button_clcRece_click, font=("宋体", 13), 106 | width=13, height=1) 107 | self.button_Cancel.place(x=400, y=197) # 显示控件 108 | 109 | # 发送按键 110 | self.button_Send = tkinter.Button(self.mainwin, text="发送", # 显示文本 111 | command=self.button_Send_click, font=("宋体", 13), 112 | width=6, height=1) 113 | self.button_Send.place(x=5, y=255) # 显示控件 114 | 115 | # 接收按键 116 | self.button_Send = tkinter.Button(self.mainwin, text="接收", # 显示文本 117 | command=self.button_Rece_click, font=("宋体", 13), 118 | width=6, height=1) 119 | self.button_Send.place(x=5, y=310) # 显示控件 120 | 121 | # 显示框 122 | # 实现记事本的功能组件 123 | self.SendDataView = tkinter.Text(self.mainwin,width = 40,height = 9, 124 | font = ("宋体",13)) # text实际上是一个文本编辑器 125 | self.SendDataView.place(x = 230,y = 35) # 显示 126 | 127 | self.ReceDataView = tkinter.Text(self.mainwin, width=40, height=9, 128 | font=("宋体", 13)) # text实际上是一个文本编辑器 129 | self.ReceDataView.place(x=230, y=230) # 显示 130 | 131 | # 发送的内容 132 | test_str = tkinter.StringVar(value="Hello") 133 | self.entrySend = tkinter.Entry(self.mainwin, width=13,textvariable = test_str,font = ("宋体",15)) 134 | self.entrySend.place(x = 80,y = 260) # 显示 135 | 136 | # 获取文件路径 137 | test_str = tkinter.StringVar(value="Hello") 138 | self.entrySend = tkinter.Entry(self.mainwin, width=13, textvariable=test_str, font=("宋体", 15)) 139 | self.entrySend.place(x=80, y=260) # 显示 140 | 141 | # 获取界面的参数 142 | self.band = self.combobox_band.get() 143 | self.check = self.combobox_check.get() 144 | self.data = self.combobox_data.get() 145 | self.stop = self.combobox_stop.get() 146 | print("波特率:"+self.band) 147 | self.myserial = SerialAchieve(int(self.band),self.check,self.data,self.stop) 148 | 149 | # 处理串口值 150 | self.port_list = self.myserial.get_port() 151 | port_str_list = [] # 用来存储切割好的串口号 152 | for i in range(len(self.port_list)): 153 | # 将串口号切割出来 154 | lines = str(self.port_list[i]) 155 | str_list = lines.split(" ") 156 | port_str_list.append(str_list[0]) 157 | self.combobox_port["value"] = port_str_list 158 | self.combobox_port.current(0) # 默认选中第0个 159 | 160 | def show(self): 161 | self.mainwin.mainloop() 162 | 163 | def button_OK_click(self): 164 | ''' 165 | @ 串口打开函数 166 | :return: 167 | ''' 168 | if self.port == None or self.port.isOpen() == False: 169 | self.myserial.open_port(self.combobox_port.get()) 170 | print("打开串口成功") 171 | else: 172 | pass 173 | 174 | def button_Cancel_click(self): 175 | self.myserial.delete_port() 176 | print("关闭串口成功") 177 | 178 | def button_clcSend_click(self): 179 | self.SendDataView.delete("1.0","end") 180 | 181 | def button_clcRece_click(self): 182 | self.ReceDataView.delete("1.0", "end") 183 | 184 | def button_Send_click(self): 185 | try: 186 | if self.myserial.port.isOpen() == True: 187 | print("开始发送数据") 188 | send_str1 = self.entrySend.get() 189 | self.myserial.Write_data(send_str1) 190 | self.SendDataView.insert(tkinter.INSERT, send_str1+" ") 191 | print("发送数据成功") 192 | else: 193 | print("串口没有打开") 194 | except: 195 | print("发送失败") 196 | def button_Rece_click(self): 197 | try: 198 | readstr = self.myserial.Read_data() 199 | self.ReceDataView.insert(tkinter.INSERT, readstr + " ") 200 | except: 201 | print("读取失败") 202 | if __name__ == '__main__': 203 | my_ser1 = MainSerial() 204 | my_ser1.show() 205 | 206 | 207 | --------------------------------------------------------------------------------