# -*- coding: utf-8 -*-
# @Time : 2022/11/29 14:49
# @Author : Administrator
# @Email : dolphintt@qq.com
# @File : 串口调试助手.py
# @Project : sdudy1
# 这个是网上抄别人的
import time
from tkinter.ttk import *
from tkinter import *
import datetime
import serial # 导入模块
import serial.tools.list_ports
import threading
from tkinter import messagebox
from ttkbootstrap import Style
global UART #全局型式保存串口句柄
global RX_THREAD #全局型式保存串口接收函数
global gui #全局型式保存GUI句柄
tx_cnt=0 #发送字符数统计
rx_cnt=0 #接收字符数统计
def ISHEX(data): #判断输入字符串是否为十六进制
if len(data)%2:
return False
for item in data:
if item not in '0123456789ABCDEFabcdef': #循环判断数字和字符
return False
return True
def uart_open_close(fun,com,bund): #串口打开关闭控制
global UART
global RX_THREAD
if fun==1:#打开串口
try:
UART = serial.Serial(com, bund, timeout=0.2) # 提取串口号和波特率并打开串口
if UART.isOpen(): #判断是否打开成功
lock = threading.Lock()
RX_THREAD = UART_RX_TREAD('URX1',lock) #开启数据接收进程
RX_THREAD.setDaemon(True) #开启守护进程 主进程结束后接收进程也关闭 会报警告 不知道咋回事
RX_THREAD.start()
RX_THREAD.resume()
return True
except:
return False
return False
else: #关闭串口
print("关闭串口")
RX_THREAD.pause()
UART.close()
def uart_tx(data,isHex=False): #串口发送数据
global UART
try:
if UART.isOpen(): #发送前判断串口状态 避免错误
print("uart_send=" + data)
gui.tx_rx_cnt(tx=len(data)) #发送计数
if isHex: #十六进制发送
data_bytes = bytes.fromhex(data)
return UART.write(bytes(data_bytes))
else: #字符发送
return UART.write(data.encode('gb2312'))
except:#错误返回
messagebox.showinfo('错误', '发送失败')
class UART_RX_TREAD(threading.Thread): #数据接收进程 部分重构
global gui
def __init__(self, name, lock):
threading.Thread.__init__(self)
self.mName = name
self.mLock = lock
self.mEvent = threading.Event()
def run(self): #主函数
print('开启数据接收\r')
while True:
self.mEvent.wait()
self.mLock.acquire()
if UART.isOpen():
rx_buf = UART.read()
if len(rx_buf) >0:
rx_buf += UART.readall() #有延迟但不易出错
gui.tx_rx_cnt(rx=len(rx_buf))
if gui.ascii_hex_get() == False:
print('收到hex数据', rx_buf.hex().upper())
gui.txt_rx.insert(END, rx_buf.hex().upper())
else:
str_data = str(rx_buf, encoding='gb2312')
print("串口收到消息:", len(rx_buf), str_data)
gui.txt_rx.insert(END,str_data)
# self.txt_rx.insert(END,str_data)
self.mLock.release()
#time.sleep(3)
def pause(self): #暂停
self.mEvent.clear()
def resume(self):#恢复
self.mEvent.set()
'''GUI'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
class GUI:
def __init__(self):
self.root = Tk()
self.root.title('梵德觅串口调试助手') #窗口名称
self.root.geometry("800x360+500+150") #尺寸位置
self.interface()
Style(theme='pulse') #主题修改 可选['cyborg', 'journal', 'darkly', 'flatly' 'solar', 'minty', 'litera', 'united', 'pulse', 'cosmo', 'lumen', 'yeti', 'superhero','sandstone']
def interface(self):
""""界面编写位置"""
#--------------------------------操作区域-----------------------------#
self.fr1=Frame(self.root)
self.fr1.place(x=0,y=0,width=180,height=360) #区域1位置尺寸
self.lb1 =Label(self.fr1, text='端口号:',font="微软雅黑",fg='red') #点击可刷新
self.lb1.place(x=0,y=5,width=100,height=35)
self.var_cb1 = StringVar()
self.comb1 = Combobox(self.fr1,textvariable=self.var_cb1)
self.comb1['values'] = list(serial.tools.list_ports.comports()) #列出可用串口
self.comb1.current(0) # 设置默认选项 0开始
self.comb1.place(x=10,y=40,width=150,height=30)
com=list(serial.tools.list_ports.comports())
print('**********可用串口***********')
for i in range(0, len(com)):
print(com[i])
print('***************************')
self.lb2 = Label(self.fr1, text='波特率:')
self.comb2 = Combobox(self.fr1,values=['2400','9600','57600','115200'])
self.comb2.current(3) #设置默认选项 115200
self.lb2.place(x=5,y=75,width=60,height=20)
self.comb2.place(x=10,y=100,width=100,height=25)
self.var_bt1 = StringVar()
self.var_bt1.set("打开串口")
self.btn1 = Button(self.fr1,textvariable=self.var_bt1,command=self.uart_opn_close) #绑定 uart_opn_close 方法
self.btn1.place(x=10,y=140,width=60,height=30)
self.var_cs = IntVar() #定义返回类型
self.rd1 = Radiobutton(self.fr1,text="Ascii",variable=self.var_cs,value=0,command = self.txt_clr) #选择后清除显示内容
self.rd2 = Radiobutton(self.fr1,text="Hex",variable=self.var_cs,value=1,command = self.txt_clr)
self.rd1.place(x=5,y=180,width=60,height=30)
self.rd2.place(x=5,y=210,width=60,height=30)
self.btn3 = Button(self.fr1, text='清空',command = self.txt_clr) #绑定清空方法
self.btn4 = Button(self.fr1, text='保存',command=self.savefiles) #绑定保存方法
self.btn3.place(x=10,y=260,width=60,height=30)
self.btn4.place(x=100,y=260,width=60,height=30)
self.btn5 = Button(self.fr1, text='功能',command=self.ascii_hex_get) #测试用
self.btn6 = Button(self.fr1, text='发送',command=self.uart_send) #绑定发送方法
self.btn5.place(x=10,y=315,width=60,height=30)
self.btn6.place(x=100,y=315,width=60,height=30)
#-------------------------------文本区域-----------------------------#
self.fr2=Frame(self.root) #区域1 容器 relief groove=凹 ridge=凸
self.fr2.place(x=180,y=0,width=620,height=360) #区域1位置尺寸
self.txt_rx = Text(self.fr2)
self.txt_rx.place(relheight=0.6,relwidth=0.9,relx=0.05,rely=0.01) #比例计算控件尺寸和位置
self.txt_tx = Text(self.fr2)
self.txt_tx.place(relheight=0.25,relwidth=0.9,relx=0.05,rely=0.66) #比例计算控件尺寸位置
self.lb3 =Label(self.fr2, text='接收:0 发送:0',bg="yellow",anchor='w') #字节统计
self.lb3.place(relheight=0.05,relwidth=0.3,relx=0.045,rely=0.925)
self.lb4 = Label(self.fr2, text=' ', anchor='w',relief=GROOVE) #时钟
self.lb4.place(relheight=0.05, relwidth=0.1, relx=0.85, rely=0.935)
#------------------------------------------方法-----------------------------------------------
def gettim(self):#获取时间 未用
timestr = time.strftime("%H:%M:%S") # 获取当前的时间并转化为字符串
self.lb4.configure(text=timestr) # 重新设置标签文本
# tim_str =
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SimpleRead_Uart.zip (3个子文件)
pro.py 2KB
uart.py 2KB
test.py 10KB
共 3 条
- 1
资源评论
不会创建对象
- 粉丝: 520
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功