使用python tkinter编写一个可以选择串口,打开串口、关闭串口的串口助手
时间: 2024-05-03 09:17:03 浏览: 216
python3+Tkinter+GUI界面+pyserial+串口+串口调试助手
4星 · 用户满意度95%
好的,以下是一个简单的串口助手程序示例,可以选择串口、打开和关闭串口:
```python
import serial
import tkinter as tk
from tkinter import ttk
class SerialAssistant:
def __init__(self, root):
self.root = root
self.ser = None
self.frame = ttk.Frame(root, padding="3 3 12 12")
self.frame.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
self.frame.columnconfigure(0, weight=1)
self.frame.rowconfigure(0, weight=1)
self.port_var = tk.StringVar()
self.port_var.set("COM1")
self.baud_var = tk.StringVar()
self.baud_var.set("9600")
self.status_var = tk.StringVar()
self.status_var.set("Disconnected")
ttk.Label(self.frame, text="Port:").grid(column=1, row=1, sticky=tk.W)
self.port_combo = ttk.Combobox(self.frame, width=10, textvariable=self.port_var)
self.port_combo.grid(column=2, row=1, sticky=(tk.W, tk.E))
self.port_combo['values'] = tuple(["COM{}".format(i) for i in range(1, 21)])
self.port_combo.current(0)
ttk.Label(self.frame, text="Baud rate:").grid(column=1, row=2, sticky=tk.W)
self.baud_entry = ttk.Entry(self.frame, width=10, textvariable=self.baud_var)
self.baud_entry.grid(column=2, row=2, sticky=(tk.W, tk.E))
self.baud_entry.focus()
self.status_label = ttk.Label(self.frame, textvariable=self.status_var)
self.status_label.grid(column=1, row=3, columnspan=2, sticky=tk.W)
self.connect_button = ttk.Button(self.frame, text="Connect", command=self.connect)
self.connect_button.grid(column=1, row=4, sticky=tk.W)
self.disconnect_button = ttk.Button(self.frame, text="Disconnect", command=self.disconnect, state="disabled")
self.disconnect_button.grid(column=2, row=4, sticky=tk.E)
ttk.Separator(self.frame, orient="horizontal").grid(column=1, row=5, columnspan=2, sticky=(tk.W, tk.E))
self.send_label = ttk.Label(self.frame, text="Send:")
self.send_label.grid(column=1, row=6, sticky=tk.W)
self.send_entry = ttk.Entry(self.frame, width=30)
self.send_entry.grid(column=2, row=6, sticky=(tk.W, tk.E))
self.send_entry.bind("<Return>", self.send)
ttk.Separator(self.frame, orient="horizontal").grid(column=1, row=7, columnspan=2, sticky=(tk.W, tk.E))
self.receive_label = ttk.Label(self.frame, text="Receive:")
self.receive_label.grid(column=1, row=8, sticky=tk.W)
self.receive_text = tk.Text(self.frame, width=30, height=10)
self.receive_text.grid(column=2, row=8, sticky=(tk.W, tk.E))
def connect(self):
try:
self.ser = serial.Serial(self.port_var.get(), int(self.baud_var.get()), timeout=1)
self.status_var.set("Connected")
self.connect_button.config(state="disabled")
self.disconnect_button.config(state="enabled")
except serial.SerialException:
self.status_var.set("Connection failed")
def disconnect(self):
if self.ser:
self.ser.close()
self.ser = None
self.status_var.set("Disconnected")
self.connect_button.config(state="enabled")
self.disconnect_button.config(state="disabled")
def send(self, event):
if self.ser:
data = self.send_entry.get()
self.ser.write(data.encode())
self.send_entry.delete(0, tk.END)
self.receive_text.insert(tk.END, "> {}\n".format(data))
if __name__ == "__main__":
root = tk.Tk()
root.title("Serial Assistant")
SerialAssistant(root)
root.mainloop()
```
运行上述代码,即可打开一个简单的串口助手界面,可以选择串口,打开和关闭串口,发送和接收数据。
阅读全文