基于上一篇python TK之串口工具制作(1)的博文,我在这里做一些功能的完善,添加文本框数据编写发送功能。
首先我们在TK界面制作页面(按键初始化)增加文本框以及数据发送按钮:
#按钮初始化
def Button_Init():
global root
global com_list_tk
global Test_Log
global Entry_SN
#串口读取写入
Button(text='扫描串口', command=Scan_Com, width=24, height=3,font=('宋体', 11)).grid(row=0, column=0)
Button(text='选择串口', command=Select_Com, width=24, height=3,font=('宋体', 11)).grid(row=1, column=0)
#Button(text='选择串口', command=send_Com, width=24, height=3, font=('宋体', 11)).grid(row=0, column=1)
Button(text='删除文本框', command=Delete_Text, width=24, height=3, font=('宋体', 11)).grid(row=0, column=1)
Button(text='关闭串口', command=close_serial, width=24, height=3, font=('宋体', 11)).grid(row=1, column=1)
Button(text='发送', command=send_data, width=10, height=2, font=('宋体', 11)).grid(row=3, column=2)
Entry_SN = Entry(root)
Entry_SN.grid(row=3, column=0, ipadx=131, ipady=10,columnspan=2)
com_list_tk = Listbox(root)
com_list_tk.grid(row=2, column=0, ipadx=28, ipady=60)
Test_Log = scrolledtext.ScrolledText(root, width=23, height=20, font=('宋体', 11), bg='Linen')
Test_Log.grid(row=2, column=1)
再增加数据发送函数
#发送数据
def send_data():
global ser
global root
global cmd
global Test_Log
global Test_Log_flag
send_entry = Entry_SN.get()
ser.write(binascii.a2b_hex(str(send_entry)))
Test_Log.tag_config('s', background='springgreen')
Test_Log.insert("end", datetime.datetime.now().strftime('%H:%M:%S ') +'send_data:' +send_entry + "\r\n", "s")
Entry_SN.delete(0, END)
这样,完整的串口数据收发工具制作就完成了,下面是完整的代码以及功能演示:
# -*- coding:utf-8 -*-
import serial
import serial.tools.list_ports
from tkinter import *
import threading
import binascii
from tkinter import scrolledtext
import time
import datetime
#扫描串口
def Scan_Com():
global root
global com_list_tk
global Test_Log
com_list_tk.delete(0, END)
port_list = list(serial.tools.list_ports.comports())
if len(port_list) == 0:
Test_Log.tag_config('t', background='tomato')
Test_Log.insert("end", "请插入串口!!!\r\n", "t")
Test_Log.see(END)
for i in range(0,len(port_list)):
com_list_tk.insert(END, port_list[i])
#选择串口
def Select_Com():
global com_list_tk
global ser
global Test_Log
try:
com = com_list_tk.get(com_list_tk.curselection())
except:
Test_Log.tag_config('t', background='tomato')
Test_Log.insert("end", "请先扫描串口!!!\r\n", "t")
Test_Log.see(END)
return
if com.find('-') != -1:
com = com[:com.find('-')]
try:
ser = serial.Serial(com,115200)
serial_recv_threading = threading.Thread(target=Serial_Recv,daemon=True)
serial_recv_threading.start()
#Heartbeat()
#Test_Log.delete(0.0,END)
Test_Log.tag_config('s', background='springgreen')
Test_Log.insert("end", com + "连接成功!\r\n", "s")
Test_Log.see(END)
except:
Test_Log.delete(0.0,END)
Test_Log.tag_config('t', background='tomato')
Test_Log.insert("end", com+"连接失败!!!\r\n", "t")
Test_Log.see(END)
#关闭串口
def Select_Com_close():
global ser
global Test_Log
ser.close()
Test_Log.tag_config('t', background='tomato')
Test_Log.insert("end", "串口已关闭!\r\n", "t")
Test_Log.see(END)
#按钮初始化
def Button_Init():
global root
global com_list_tk
global Test_Log
global Entry_SN
#串口读取写入
Button(text='扫描串口', command=Scan_Com, width=24, height=3,font=('宋体', 11)).grid(row=0, column=0)
Button(text='选择串口', command=Select_Com, width=24, height=3,font=('宋体', 11)).grid(row=1, column=0)
#Button(text='选择串口', command=send_Com, width=24, height=3, font=('宋体', 11)).grid(row=0, column=1)
Button(text='删除文本框', command=Delete_Text, width=24, height=3, font=('宋体', 11)).grid(row=0, column=1)
Button(text='关闭串口', command=close_serial, width=24, height=3, font=('宋体', 11)).grid(row=1, column=1)
Button(text='发送', command=send_data, width=10, height=2, font=('宋体', 11)).grid(row=3, column=2)
Entry_SN = Entry(root)
Entry_SN.grid(row=3, column=0, ipadx=131, ipady=10,columnspan=2)
com_list_tk = Listbox(root)
com_list_tk.grid(row=2, column=0, ipadx=28, ipady=60)
Test_Log = scrolledtext.ScrolledText(root, width=23, height=20, font=('宋体', 11), bg='Linen')
Test_Log.grid(row=2, column=1)
#发送数据
def send_data():
global ser
global root
global cmd
global Test_Log
global Test_Log_flag
send_entry = Entry_SN.get()
ser.write(str(send_entry).encode("gbk"))
Test_Log.tag_config('s', background='springgreen')
Test_Log.insert("end", datetime.datetime.now().strftime('%H:%M:%S ') +'send_data:' +send_entry + "\r\n", "s")
Entry_SN.delete(0, END)
#刪除文本框
def Delete_Text():
global Test_Log
Test_Log.delete(0.0,END)
Test_Log.tag_config('n', background='green')
Test_Log.insert('end','文本框清除成功'+'\r\n','n')
Test_Log.see(END)
#关闭串口
def close_serial():
global ser
ser.close()
Test_Log.tag_config('n', background='red')
Test_Log.insert("end", "串口已关闭!\r\n", "n")
Test_Log.see(END)
def Serial_Recv():
global ser
global root
global cmd
global Test_Log
global recv_data
global Test_Log_flag
try:
while True:
time.sleep(0.05)
data_len = ser.inWaiting()
if data_len == 0:
continue
print(data_len)
recv_data = str(binascii.b2a_hex(ser.read(data_len)))[2:-1]
print(datetime.datetime.now().strftime('%H:%M:%S'),'要处理的数据', recv_data)
Test_Log.tag_config('s', background='springgreen')
Test_Log.insert("end", datetime.datetime.now().strftime('%H:%M:%S ') + 'rec_data:'+recv_data +"\r\n", "s")
except:
ser.close()
Test_Log.tag_config('t', background='tomato')
Test_Log.insert("end", datetime.datetime.now().strftime('%H:%M:%S ') + "串口已关闭,请重新选择串口! \r\n", "t")
Test_Log.see(END)
def main():
global root
root = Tk()
root.title('串口工具 V1.01')
root.geometry('500x500')
Button_Init()
root.mainloop()
if __name__ == '__main__':
main()
功能演示:
*關注我,之後會有更多的實用小工具,完整代碼送給你哦