主要功能有:扫描并列出串口名,设置上下电时间,保存及自动保存log文件等。应用于自己做的一个上下电设备,下位机是arduino控制的大功率mos管。用于设备上下电老化测试。代码是根据别人的串口程序改的,自己不太懂的地方已经加了注释。
#encoding=utf-8
__author__ = 'pxj'
from tkinter import *
from serial import *
import serial.tools.list_ports
#from tkinter.ttk import *
from tkinter import ttk #加上ttk美化界面
import time
from tkinter.scrolledtext import ScrolledText
#import ttk
class GUI(Frame):
def __init__(self,master):
frame = Frame(master)
frame.pack()
#串口设置相关变量
## self.port = "COM12"
## self.baudrate = 9600
#串口号提示
self.lab1 = Label(frame,text = '串口号:')
self.lab1.grid(row = 0,column = 0,sticky = W)
#串口号选择下拉菜单
self.port_list = list(serial.tools.list_ports_windows.comports())
serial_num=len(self.port_list)
print(serial_num)
serial_list=[] #串口列表
#信息显示栏
self.show = ScrolledText(frame,width = 40,height = 5,wrap = WORD)
self.show.grid(row = 1,column = 1,rowspan = 3,columnspan=3,sticky = W)
for i in range(0,serial_num):
## print(list(self.port_list[i])[0])
serial_list.append(list(self.port_list[i])[0]) #扫描串口,增加扫描到的串口“COM n”
self.show.insert(0.0,list(self.port_list[i])[1]+"
") #显示相关串口信息
serial_list.sort() #整理串口顺序
print(serial_list)
self.boxValue = StringVar()
self.boxChoice = ttk.Combobox(frame,textvariable = self.boxValue,state = 'readonly',width=10)
## self.boxChoice['value'] = ("COM1","COM2","COM3","COM4","COM5","COM6","COM7","COM8","COM9","COM10","COM12")
self.boxChoice['value']= (serial_list)
self.boxChoice.current(0)
self.boxChoice.bind('<>',self.Choice)
self.boxChoice.grid(row = 1,column = 0,sticky = W)
#波特率选择提示
self.lab2 = Label(frame,text = '波特率')
self.lab2.grid(row = 2,column = 0,sticky = W)
#波特率选择下拉菜单
self.boxValueBaudrate = IntVar()
self.BaudrateChoice = ttk.Combobox(frame,textvariable = self.boxValueBaudrate,state = 'readonly',width=10)
self.BaudrateChoice['value'] = (9600,19200,38400,115200)
self.BaudrateChoice.current(0)
self.BaudrateChoice.bind('<>',self.ChoiceBaudrate)
self.BaudrateChoice.grid(row = 3,column = 0,sticky = W)
#输出框提示
self.lab3 = Label(frame,text = '串口信息:')
self.lab3.grid(row = 0,column = 1,sticky = W)
#输出框
#滚动条
## self.S = Scrollbar(self.show)
## self.S.grid(row = 4,column = 2,rowspan = 2,sticky = E)
## self.S.config(command=self.show.yview)
## self.show.config(yscrollcommand=self.S.set)
#输入框提示
self.lab4 = Label(frame,text = '串口输入:')
self.lab4.grid(row = 4,column = 1,sticky = W)
#输入框
self.input = ttk.Entry(frame,width = 40)
self.input.grid(row = 5,column = 1,rowspan = 1,columnspan=3,sticky = W)
#输入按钮
self.button1 = ttk.Button(frame,text = "输入",width=8,command = self.Submit)
self.button1.grid(row = 6,column = 2)
#清空按钮
self.button4 = ttk.Button(frame,text = "清除",width=8,command = self.Clear)
self.button4.grid(row = 6,column = 3)
#保存log按钮
self.button5 = ttk.Button(frame,text = "保存log",width=8,command = self.SaveClick)
self.button5.grid(row = 7,column = 3)
## self.button5.bind("",self.SaveClick)
#串口开启按钮
self.button2 = ttk.Button(frame,text = '打开串口',command = self.open)
self.button2.grid(row = 5,column = 0)
#串口关闭按钮
self.button3 = ttk.Button(frame,text = '关闭串口',command = self.close)
self.button3.grid(row = 6,column = 0)
#串口信息提示框
## self.showSerial = Text(frame,width = 20,height = 2,wrap = WORD)
## self.showSerial.grid(row = 12,column = 1,sticky = W)
#串口初始化配置
self.ser = Serial()
## self.ser.setPort(self.port)
#输入开机时间
self.lab_open_time=Label(frame,text="开机时间/S:")
self.lab_open_time.grid(row=7,column=0,sticky=W)
self.input_open_time=ttk.Entry(frame,width=10)
self.input_open_time.grid(row=7,column=1,sticky=W)
self.input_open_time.insert(2,"1")
#输入关机时间
self.lab_close_time=Label(frame,text="关机时间/S:")
self.lab_close_time.grid(row=8,column=0,sticky=W)
self.input_close_time=ttk.Entry(frame,width=10)
self.input_close_time.grid(row=8,column=1,sticky=W)
self.input_close_time.insert(2,"1")
#输入开关次数
self.lab_times=Label(frame,text="开关次数/次:")
self.lab_times.grid(row=9,column=0,sticky=W)
self.input_times=ttk.Entry(frame,width=10)
self.input_times.grid(row=9,column=1,sticky=W)
self.input_times.insert(2,"5")
#开始测试
self.begin_button=ttk.Button(frame,text = '开始测试',command = self.begin)
self.begin_button.grid(row=11,column=0)
#self.ser.setBaudrate(self.baudrate)
#self.ser.open()
#print self.ser.isOpen()
#print self.ser
def Choice(self,event):
context = self.boxValue.get()
print(context,"is selected!")
self.ser.setPort(context)
## list = ["COM1","COM2","COM3","COM4","COM10"]
## if context in list:
## self.port = list.index(context)
## self.ser.setPort(self.port)
## print (self.port)
def ChoiceBaudrate(self,event):
self.baudrate = self.boxValueBaudrate.get()
## self.ser.setBaudrate(self.baudrate)
self.ser.baudrate=self.baudrate
print (self.baudrate)
def Submit(self):
if self.ser.isOpen() == False:
self.open()
context1 = self.input.get()
print(context1)
n = self.ser.write(context1.encode())
self.ser.flush()
#output = self.ser.read(n)
# print (output)
self.show.delete(0.0,END)
#self.show.insert(0.0,output)
self.close()
#开始测试
def begin(self):
if self.ser.isOpen() == False:
self.open()
self.Clear()
self.times=self.input_times.get()
print(self.times)
num=int(self.times)
open_time=int(self.input_open_time.get())
close_time=int(self.input_close_time.get())
for i in range(0,num):
#print("close")
n1=self.ser.write(b"close")
self.ser.flush()
## self.show.insert(0.0,"close")
self.print_close()
# print(self.ser.read(n1))
time.sleep(close_time)
#print("open")
## print(i)
n2=self.ser.write(b"open")
self.ser.flush()
# print(self.ser.read(n2))
self.print_open()
time.sleep(open_time)
self.show.insert(0.0,str(i+1)+"
") #打印测试的次数
self.show.update() #实时刷新
with open (str(os.getcwd())+'/auto_save_log.txt','w+') as fb:
fb.write(self.show.get(0.0,'end'))
print(num,"times test is over!")
## self.show.delete(0.0,END)
self.show.insert(0.0,str(num)+" times test is over!
")
self.show.update() #实时刷新
self.close()
def print_close(self):
self.show.insert(0.0,"close
")
self.show.update() #实时刷新
def print_open(self):
self.show.insert(0.0,"open
")
self.show.update() #实时刷新
def Clear(self):
self.show.delete(0.0,END)
def open(self):
self.ser.open()
if self.ser.isOpen() == True:
#self.show.delete(0.0,END)
self.show.insert(0.0,"Serial has been opend!
")
def close(self):
self.ser.close()
if self.ser.isOpen() == False:
# self.show.delete(0.0,END)
self.show.insert(0.0,"Serial has been closed!
")
def SaveClick(self):
## print(os.getcwd())
self.time_now=time.strftime("%Y%m%d_%H_%M_%S", time.localtime()) #为log文件打上时间戳
print(self.time_now)
with open (str(os.getcwd())+"/log_"+self.time_now+".txt",'w+') as fb: #新建文件
fb.write(self.show.get(0.0,'end'))
self.show.insert(0.0,"Log has been saved!
")
root = Tk()
root.title("Serial Communication")
#root.geometry("420x320")#设置窗口大小
app = GUI(root)
root.mainloop()