使用serial庫加TK界面製作庫完成簡單的串口工具,實現串口數據收發。製作完成之後界面大概就這樣子,我們可以看到這個工具可以自動監測你電腦接了幾個串口,并顯示在左側的文本框裏,點擊選擇即可連上串口,并實現接收數據。*關注我,之後會有更多的實用小工具,完整代碼送給你哦
具體代碼如下:
# -*- 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
#串口读取写入
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)
com_list_tk = Listbox(root)
com_list_tk.grid(row=2, column=0, ipadx=28, ipady=60, rowspan=3)
Test_Log = scrolledtext.ScrolledText(root, width=24, height=20, font=('宋体', 11), bg='Linen')
Test_Log.grid(row=2, column=1, columnspan=2, rowspan=3)
#刪除文本框
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 join_network_flag
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 ') + 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('500x450')
Button_Init()
root.mainloop()
if __name__ == '__main__':
main()