学习两个月的Python ,经过千辛万苦,熬夜写的软件,含有源码
单片机源程序如下:
- from tkinter import *
- from tkinter import ttk
- from tkinter import messagebox
- import threading
- import serial
- import serial.tools.list_ports
- import time
- import sys
- data = None
- tx=None
- recv_end = False
- send_data=None
- send_data_hex=None
- txt_rece_show=None
- txt_send_show=None
- myser=None
- window=None
- th_receve=None
- ports=[]
- comvalue_port=None
- comvalue_baudrate=None
- comvalue_bytesize=None
- comvalue_prity=None
- comvalue_stopbits=None
- chekbuvalue_line = None
- chekbuvalue_time_show=None
- checkbuvalue_send_show=None
- combox_port=None
- combox_stopbits=None
- combox_prity=None
- combox_bytesize=None
- combox_baudrate=None
- radb_hex_ascii=None
- radb1_hex_ascii=None
- but_open_clos_port=None
- exit_mark=True
- port_list=[]
- def recv():
- global tx
- global data
- global recv_end
- global txt_rece_show
- print('---rec-start---')
- while exit_mark:
- # print(myser.isOpen())
- time.sleep(0.01)
- data = ''
- # data1=''
- data = data.encode('utf-8')
- try:
- if myser.isOpen():
- time.sleep(0.01)
- n = myser.inWaiting()
- # print(n)
- if n:
- for i in range(n):
- data = data+myser.read()
- print(data)
- print(n)
- print(len(data))
- insert_txt(txt_rece_show)
- except Exception as e:
- print('--异常--',e)
- def find_port_list():
- global ports
- global port_list
- global myser
- port_test = None
- port_list = serial.tools.list_ports.comports()
- for i in range(len(port_list)):
- ports.append(port_list[i][0])
- try:
- myser = serial.Serial(port_list[i][0])
- myser.close()
- except:
- print(port_list[i][0],'打开失败')
- return ports
- def open_close_port():
- global myser
- global th_receve
- global comvalue_port
- global comvalue_baudrate
- global comvalue_bytesize
- global comvalue_prity
- global comvalue_stopbits
- global but_open_clos_port
- global combox_port
- global combox_stopbits
- global combox_prity
- global combox_bytesize
- global combox_baudrate
- myport = comvalue_port.get()
- mybaudrate = comvalue_baudrate.get()
- mybytesize = comvalue_bytesize.get()
- myprity = comvalue_prity.get()
- mystopbits = comvalue_stopbits.get()
- myser.port=myport
- myser.baudrate = mybaudrate
- myser.bytesize=int(mybytesize)
- myser.parity=myprity
- if mystopbits == '1':
- myser.stopbits=serial.STOPBITS_ONE
- elif mystopbits == '1.5':
- myser.stopbits = serial.STOPBITS_ONE_POINT_FIVE
- elif mystopbits == '2':
- myser.stopbits = serial.STOPBITS_TWO
- try:
- if myser.isOpen():
- myser.close()
- combox_port['state'] = "normal"
- combox_baudrate['state'] = "normal"
- combox_bytesize['state'] = "normal"
- combox_prity['state'] = "normal"
- combox_stopbits['state'] = "normal"
- but_open_clos_port['text']='打开串口(串口已经关闭)'
- print(myser.name,'串口已经关闭')
- else:
- myser.open()
- combox_port['state']="disable"
- combox_baudrate['state']="disable"
- combox_bytesize['state']="disable"
- combox_prity['state']="disable"
- combox_stopbits['state']="disable"
- but_open_clos_port['text'] = '关闭串口(串口已经打开)'
- print(myser.name,'串口已经打开')
- except Exception as e:
- print('--串口打开异常—',e)
- str_mes = myser.name + " 打开失败"
- open_port_mesbox=messagebox.showinfo('提示',str_mes)
- def send_mydata(serial):
- global send_data
- global radb1_hex_ascii
- if serial.isOpen() :
- if radb1_hex_ascii.get()==1:
- # serial.write(send_data.encod('utf-8'))
- serial.write(send_data.encode('utf-8'))
- if radb1_hex_ascii.get()==2:
- serial.write(send_data_hex)
- # pass
- def get_txt_send_show():
- global send_data
- global txt_send_show
- global radb1_hex_ascii
- global send_data_hex
- if radb1_hex_ascii.get()==1:
- send_data = txt_send_show.get('1.0', 'end')
- print('send_data type',type(send_data))
- print(send_data)
- pass
- if radb1_hex_ascii.get()==2:
- send_data_hex = bytes.fromhex(txt_send_show.get('1.0', 'end'))
- print('send_data_hex type',type(send_data_hex))
- print(send_data_hex)
- # pass
- # send_data = txt_send_show.get('1.0','end')
- def button_sent_data():
- print(myser.isOpen())
- get_txt_send_show()
- send_mydata(myser)
- def insert_txt(tx):
- global radb_hex_ascii
- global chekbuvalue_line
- global chekbuvalue_send_show
- global data
- # timeadd=''
- if chekbuvalue_time_show.get()==1 & chekbuvalue_line.get()==1:
- timeadd=str(time.strftime("[%H:%M:%S]", time.localtime()))
- print('timeadd',timeadd)
- else:
- timeadd=''
- if radb_hex_ascii.get()==1:
- data = timeadd.encode('utf-8') + data
- tx.insert('end', data)
- if radb_hex_ascii.get()==2:
- hex_str=''
- for i in range(len(data)):
- # print('%x'%data[i])
- hex_str=hex_str+('%x'%data[i]).upper()+' '
- hex_str=timeadd+hex_str
- tx.insert('end', hex_str)
- if chekbuvalue_line.get()==1:
- print('自动换行已经选中')
- tx.insert('end', '\n')
- def clear_all_txt():
- global txt_rece_show
- txt_rece_show.delete('1.0','end')
- def all_exit():
- global window
- global exit_mark
- exit_mark=False
- if myser.isOpen:
- myser.close()
- time.sleep(0.1)
- sys.exit()
- def mygui():
- global myser
- global window
- global th_receve
- global comvalue_port
- global comvalue_baudrate
- global comvalue_bytesize
- global comvalue_prity
- global comvalue_stopbits
- global chekbuvalue_line
- global chekbuvalue_time_show
- global checkbuvalue_send_show
- global combox_port
- global combox_stopbits
- global combox_prity
- global combox_bytesize
- global combox_baudrate
- global checkbu
- global port_list
- find_port_list()
- # port_list[0][0]
- try:
- # myser = serial.Serial(port_list[0][0])
- # myser.close()
- th_receve = threading.Thread(target=recv)
- th_receve.start()
- except Exception as e:
- print('--串口异常--',e)
- window = Tk()
- window.title('HaiLang serial tools')
- window.geometry('700x400')
- window.resizable(width=False,height=False) # 窗口大小不可更改
- window.protocol('WM_DELETE_WINDOW',all_exit)
- # window.bind('<QUIT>',all_exit)
- frame_set = Frame(window) # 设置单元框架
- frame_set.pack(side="left",padx=10)
- frame_disply =Frame(window) # 显示单元框架
- frame_disply.pack(side='right')
- # 设置单元框架
- # 串口设置 LableFrame
- lf_port_set = LabelFrame(frame_set,text="串口设置")
- lf_port_set.pack()
- lf_port_set.rowconfigure([0,1,2,3,4],minsize=30)
- lf_port_set.rowconfigure(5, minsize=10)
- #*****************************************************
- label_port = Label(lf_port_set,text="端 口:")
- label_port.grid(row=0,column=0,padx=10)
- global ports
- comvalue_port=StringVar()
- combox_port = ttk.Combobox(lf_port_set,textvariable=comvalue_port)
- combox_port['values']=ports
- combox_port.current(0)
- combox_port.grid(row=0,column=1,padx=10)
- #*****************************************************
- label_baudrate = Label(lf_port_set, text="波特率:")
- label_baudrate.grid(row=1,column=0)
- comvalue_baudrate = StringVar()
- combox_baudrate = ttk.Combobox(lf_port_set, textvariable=comvalue_baudrate)
- combox_baudrate['values'] = [2400, 4800,9600, 19200,
- 38400, 57600, 115200]
- combox_baudrate.current(2)
- combox_baudrate.grid(row=1, column=1)
- #*****************************************************
- label_bytesize = Label(lf_port_set, text="数据位:")
- label_bytesize.grid()
- comvalue_bytesize = StringVar()
- combox_bytesize = ttk.Combobox(lf_port_set,
- textvariable=comvalue_bytesize)
- combox_bytesize['values'] = [serial.FIVEBITS, serial.SIXBITS, serial.SEVENBITS, serial.EIGHTBITS]
- combox_bytesize.current(3)
- combox_bytesize.grid(row=2, column=1)
- #*****************************************************
- label_prity = Label(lf_port_set, text="校验位:")
- label_prity.grid()
- comvalue_prity = StringVar()
- combox_prity = ttk.Combobox(lf_port_set,
- textvariable=comvalue_prity)
- combox_prity['values'] = [serial.PARITY_EVEN, serial.PARITY_NONE, serial.PARITY_ODD]
- combox_prity.current(1)
- combox_prity.grid(row=3, column=1)
- #*****************************************************
- label_stopbits = Label(lf_port_set, text="停止位:")
- label_stopbits.grid()
- comvalue_stopbits = StringVar()
- combox_stopbits = ttk.Combobox(lf_port_set,
- textvariable=comvalue_stopbits)
- combox_stopbits['values'] = [serial.STOPBITS_ONE, serial.STOPBITS_TWO]
- combox_stopbits.current(0)
- combox_stopbits.grid(row=4, column=1)
- #*****************************************************
- global but_open_clos_port
- but_open_clos_port =Button(frame_set,text='打开串口' ,command=open_close_port)
- but_open_clos_port.pack(fill='x')
- #*****************************************************
- lf_receve_set =LabelFrame(frame_set,text="接收设置")
- lf_receve_set.pack(fill='x')
- global radb_hex_ascii
- radb_hex_ascii = IntVar()
- radb_hex_ascii.set(1)
- radb_ascii = Radiobutton(lf_receve_set,text='ASCII',variable=radb_hex_ascii,value=1)
- radb_ascii.grid(row=0,column=0)
- radb_hex = Radiobutton(lf_receve_set,text='HEX',variable=radb_hex_ascii,value=2)
- radb_hex.grid(row=0,column=1)
- radb_hex_ascii.get()
- chekbuvalue_line = IntVar()
- checkbu_line = Checkbutton(lf_receve_set,text='自动换行',variable=chekbuvalue_line)
- checkbu_line.grid(row=1,column=0,padx=10)
- # checkbuvalue_send_show=IntVar()
- # checkbu_send_show = Checkbutton(lf_receve_set, text='显示发送',variable=checkbuvalue_send_show)
- # checkbu_send_show.grid(row=2, column=0)
- chekbuvalue_time_show = IntVar()
- checkbu_time_show = Checkbutton(lf_receve_set, text='显示时间',variable=chekbuvalue_time_show)
- checkbu_time_show.grid(row=3, column=0)
- but_clear_txt=Button(lf_receve_set,text='清空接收',command=clear_all_txt)
- but_clear_txt.grid(row=0,column=2,padx=20)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
mypythonserialtools.rar
(8.13 MB, 下载次数: 191)
|