使用python中的tkinter模块写一个串口助手窗体程序,需要包括以下几个功能:1、能够自定义先择串口、波特率、数据为、校验位、停止位2、能够发送、接收数据
时间: 2023-10-20 21:12:20 浏览: 88
好的,下面是一个简单的串口助手窗体程序,基于Python的tkinter模块实现,包含了选择串口、波特率、数据位、校验位、停止位、发送数据和接收数据的功能:
```
import tkinter as tk
import serial
class SerialAssistant:
def __init__(self, master):
self.master = master
self.master.title("串口助手")
self.master.geometry("400x300")
# 创建串口选择标签和下拉框
self.serial_label = tk.Label(self.master, text="串口")
self.serial_label.grid(row=0, column=0)
self.serial_var = tk.StringVar()
self.serial_var.set("COM1")
self.serial_option = tk.OptionMenu(self.master, self.serial_var, "COM1", "COM2", "COM3", "COM4")
self.serial_option.grid(row=0, column=1)
# 创建波特率选择标签和下拉框
self.baudrate_label = tk.Label(self.master, text="波特率")
self.baudrate_label.grid(row=1, column=0)
self.baudrate_var = tk.StringVar()
self.baudrate_var.set("9600")
self.baudrate_option = tk.OptionMenu(self.master, self.baudrate_var, "9600", "115200")
self.baudrate_option.grid(row=1, column=1)
# 创建数据位选择标签和下拉框
self.bytesize_label = tk.Label(self.master, text="数据位")
self.bytesize_label.grid(row=2, column=0)
self.bytesize_var = tk.StringVar()
self.bytesize_var.set("8")
self.bytesize_option = tk.OptionMenu(self.master, self.bytesize_var, "5", "6", "7", "8")
self.bytesize_option.grid(row=2, column=1)
# 创建校验位选择标签和下拉框
self.parity_label = tk.Label(self.master, text="校验位")
self.parity_label.grid(row=3, column=0)
self.parity_var = tk.StringVar()
self.parity_var.set("N")
self.parity_option = tk.OptionMenu(self.master, self.parity_var, "N", "E", "O")
self.parity_option.grid(row=3, column=1)
# 创建停止位选择标签和下拉框
self.stopbits_label = tk.Label(self.master, text="停止位")
self.stopbits_label.grid(row=4, column=0)
self.stopbits_var = tk.StringVar()
self.stopbits_var.set("1")
self.stopbits_option = tk.OptionMenu(self.master, self.stopbits_var, "1", "1.5", "2")
self.stopbits_option.grid(row=4, column=1)
# 创建发送数据输入框和按钮
self.send_label = tk.Label(self.master, text="发送数据")
self.send_label.grid(row=5, column=0)
self.send_entry = tk.Entry(self.master)
self.send_entry.grid(row=5, column=1)
self.send_button = tk.Button(self.master, text="发送", command=self.send_data)
self.send_button.grid(row=5, column=2)
# 创建接收数据文本框
self.receive_label = tk.Label(self.master, text="接收数据")
self.receive_label.grid(row=6, column=0)
self.receive_text = tk.Text(self.master, height=10, width=30)
self.receive_text.grid(row=6, column=1)
# 创建打开串口按钮
self.open_button = tk.Button(self.master, text="打开串口", command=self.open_serial)
self.open_button.grid(row=7, column=0)
# 创建关闭串口按钮
self.close_button = tk.Button(self.master, text="关闭串口", command=self.close_serial, state="disabled")
self.close_button.grid(row=7, column=1)
# 初始化串口对象
self.ser = None
def open_serial(self):
# 获取串口参数
port = self.serial_var.get()
baudrate = int(self.baudrate_var.get())
bytesize = int(self.bytesize_var.get())
parity = self.parity_var.get()
stopbits = float(self.stopbits_var.get())
try:
# 创建串口对象
self.ser = serial.Serial(port=port, baudrate=baudrate, bytesize=bytesize, parity=parity, stopbits=stopbits)
self.ser.timeout = 0.1
# 打开串口
self.ser.open()
# 设置打开串口按钮状态为disabled,关闭串口按钮状态为normal
self.open_button.configure(state="disabled")
self.close_button.configure(state="normal")
# 启动接收线程
self.receive_thread_running = True
self.receive_thread = threading.Thread(target=self.receive_data)
self.receive_thread.start()
except Exception as e:
tk.messagebox.showerror("错误", str(e))
def close_serial(self):
try:
# 关闭串口
self.ser.close()
# 设置打开串口按钮状态为normal,关闭串口按钮状态为disabled
self.open_button.configure(state="normal")
self.close_button.configure(state="disabled")
# 停止接收线程
self.receive_thread_running = False
except Exception as e:
tk.messagebox.showerror("错误", str(e))
def send_data(self):
# 获取发送数据
data = self.send_entry.get()
try:
# 发送数据
self.ser.write(data.encode())
except Exception as e:
tk.messagebox.showerror("错误", str(e))
def receive_data(self):
while self.receive_thread_running:
try:
# 接收数据
data = self.ser.read_all().decode()
if data:
# 将接收到的数据显示在接收数据文本框中
self.receive_text.insert("end", data)
self.receive_text.see("end")
except Exception as e:
pass
if __name__ == "__main__":
root = tk.Tk()
app = SerialAssistant(root)
root.mainloop()
```
在程序中,选择串口、波特率、数据位、校验位、停止位都是使用tkinter的OptionMenu控件实现的,发送数据和接收数据分别使用了tkinter的Entry和Text控件。打开串口和关闭串口分别使用了Python的serial模块中的Serial类的open和close方法实现,接收数据使用了Python的serial模块中的Serial类的read_all方法实现。同时,为了避免串口读取阻塞GUI线程,接收数据部分使用了一个独立的线程来完成。
阅读全文