当前位置: 首页> 文旅> 文化 > 哈尔滨市建设工程交易_注册网站会员需要详细_合肥建站公司seo_网址怎么弄

哈尔滨市建设工程交易_注册网站会员需要详细_合肥建站公司seo_网址怎么弄

时间:2025/8/30 3:26:49来源:https://blog.csdn.net/WU1707640034/article/details/146079740 浏览次数:0次
哈尔滨市建设工程交易_注册网站会员需要详细_合肥建站公司seo_网址怎么弄
"""
串口调试助手 v1.2
功能:支持RS232/485通信、HEX收发、自动检测端口、数据记录
开发环境:Python 3.8 + Tkinter + pyserial
"""
import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox, filedialog
import serial
import serial.tools.list_ports
from threading import Thread, Event
import timeclass SerialDebugger:def __init__(self, master):self.master = masterself.serial_port = Noneself.receive_thread = Noneself.stop_event = Event()self.init_ui()self.auto_detect_ports()def init_ui(self):"""初始化用户界面"""self.master.title("嵌入式串口调试助手")self.master.geometry("800x600")# 主布局框架main_frame = ttk.Frame(self.master)main_frame.pack(fill=tk.BOTH, expand=True)# 串口配置区域config_frame = ttk.LabelFrame(main_frame, text="串口配置")config_frame.pack(fill=tk.X, padx=5, pady=5)# 端口选择ttk.Label(config_frame, text="端口:").grid(row=0, column=0, padx=5)self.port_combo = ttk.Combobox(config_frame, width=15)self.port_combo.grid(row=0, column=1, padx=5)ttk.Button(config_frame, text="刷新端口", command=self.auto_detect_ports).grid(row=0, column=2, padx=5)# 参数配置params = [("波特率", ["9600", "115200", "57600", "38400", "19200"]),("数据位", ["8", "7", "6", "5"]),("校验位", ["无", "奇校验", "偶校验"]),("停止位", ["1", "1.5", "2"])]for idx, (text, values) in enumerate(params, start=1):ttk.Label(config_frame, text=f"{text}:").grid(row=idx, column=0, padx=5)combo = ttk.Combobox(config_frame, values=values, width=8)combo.set(values[0])combo.grid(row=idx, column=1, padx=5)setattr(self, f'combo_{text}', combo)# 操作按钮self.btn_connect = ttk.Button(config_frame, text="打开串口", command=self.toggle_connection)self.btn_connect.grid(row=4, column=2, rowspan=3, padx=10, pady=5)# 接收显示区域recv_frame = ttk.LabelFrame(main_frame, text="接收数据")recv_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)self.recv_text = scrolledtext.ScrolledText(recv_frame, wrap=tk.WORD)self.recv_text.pack(fill=tk.BOTH, expand=True)# 发送控制区域send_frame = ttk.Frame(main_frame)send_frame.pack(fill=tk.X, padx=5, pady=5)self.send_entry = ttk.Entry(send_frame)self.send_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=5)self.hex_send = tk.BooleanVar()ttk.Checkbutton(send_frame, text="HEX发送", variable=self.hex_send).pack(side=tk.LEFT)ttk.Button(send_frame, text="发送", command=self.send_data).pack(side=tk.RIGHT, padx=5)# 状态栏self.status_var = tk.StringVar()status_bar = ttk.Label(main_frame, textvariable=self.status_var, relief=tk.SUNKEN)status_bar.pack(side=tk.BOTTOM, fill=tk.X)def auto_detect_ports(self):"""自动检测可用串口"""ports = [port.device for port in serial.tools.list_ports.comports()]self.port_combo['values'] = portsif ports:self.port_combo.current(0)self.update_status(f"检测到{len(ports)}个可用串口")def toggle_connection(self):"""切换串口连接状态"""if self.serial_port and self.serial_port.is_open:self.close_serial()else:self.open_serial()def open_serial(self):"""打开串口"""try:self.serial_port = serial.Serial(port=self.port_combo.get(),baudrate=int(self.combo_波特率.get()),bytesize=int(self.combo_数据位.get()),parity=self.get_parity(),stopbits=self.get_stopbits(),timeout=0.1)self.btn_connect.config(text="关闭串口")self.start_receive_thread()self.update_status(f"已连接 {self.port_combo.get()}")except Exception as e:messagebox.showerror("连接错误", f"无法打开串口:\n{str(e)}")def get_parity(self):"""获取校验位配置"""parity_map = {"无": serial.PARITY_NONE,"奇校验": serial.PARITY_ODD,"偶校验": serial.PARITY_EVEN}return parity_map[self.combo_校验位.get()]def get_stopbits(self):"""获取停止位配置"""stopbits_map = {"1": serial.STOPBITS_ONE,"1.5": serial.STOPBITS_ONE_POINT_FIVE,"2": serial.STOPBITS_TWO}return stopbits_map[self.combo_停止位.get()]def start_receive_thread(self):"""启动数据接收线程"""self.stop_event.clear()self.receive_thread = Thread(target=self.receive_data, daemon=True)self.receive_thread.start()def receive_data(self):"""接收数据线程"""while not self.stop_event.is_set() and self.serial_port.is_open:try:if self.serial_port.in_waiting > 0:data = self.serial_port.read(self.serial_port.in_waiting)self.display_data(data)except Exception as e:self.update_status(f"接收错误: {str(e)}")breakdef display_data(self, data):"""显示接收数据"""text = data.hex(' ') + ' ' if self.hex_send.get() else data.decode('utf-8', 'replace')self.recv_text.insert(tk.END, text)self.recv_text.see(tk.END)self.update_status(f"接收字节数: {len(data)}")def send_data(self):"""发送数据"""if not self.serial_port or not self.serial_port.is_open:messagebox.showwarning("警告", "请先打开串口连接!")returndata = self.send_entry.get()try:if self.hex_send.get():data = bytes.fromhex(data.replace(' ', ''))else:data = data.encode('utf-8')self.serial_port.write(data)self.update_status(f"发送字节数: {len(data)}")except ValueError:messagebox.showerror("格式错误", "HEX数据格式不正确")except Exception as e:messagebox.showerror("发送错误", str(e))def close_serial(self):"""关闭串口连接"""self.stop_event.set()if self.serial_port and self.serial_port.is_open:self.serial_port.close()self.btn_connect.config(text="打开串口")self.update_status("串口已关闭")def update_status(self, message):"""更新状态栏"""self.status_var.set(f"状态: {message}")def on_closing(self):"""窗口关闭事件处理"""self.close_serial()self.master.destroy()if __name__ == "__main__":root = tk.Tk()app = SerialDebugger(root)root.protocol("WM_DELETE_WINDOW", app.on_closing)root.mainloop()

关键字:哈尔滨市建设工程交易_注册网站会员需要详细_合肥建站公司seo_网址怎么弄

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: