吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4812|回复: 9
收起左侧

[Python 转载] 串口调试工具源码分享

[复制链接]
729 发表于 2020-2-28 19:49
本帖最后由 729 于 2021-1-15 14:23 编辑

最近在学习单片机,然后在论坛找的到了一个串口调试的工具,但是用的时候发现数据出错。所以干脆自己写一个吧!
(第一次写的Tkinter 别吐槽太厉害了,两个库都是第一次使用.)
经过自己的测试发现忘记添加16进制的发送了 所以使用的时候会出现一点问题(已修改添加进去)



[Python] 纯文本查看 复制代码
#encoding:utf-8
import serial
import serial.tools.list_ports
from GUI import *
import re
##读取数据
def tread_RXD():
    global ser
    try:
        if ser!=0:
            Read_2()
        return 0
    except Exception as e :
        print(e)
        return 0
#读取方式2
def Read_2():
    if ser.in_waiting:
        if app.div_right.var_3.get()==1:
            data=str(ser.read().hex())
            app.div_left.Message.insert(END, data+' ')
        else:
            data=str(ser.read().decode('utf-8'))
            app.div_left.Message.insert(END,data)
        app.div_left.Message.see(END)
##读取串口列表
def port(event):
    port_list_1 = list(serial.tools.list_ports.comports())
    port_list_2=[]
    if port_list_1:
        for p in range(0,len(port_list_1)):
            port_list_2.append(str(port_list_1[p])[:4])
        app.div_right.list_1['value']=port_list_2
    return 0
##打开串口
def start_RXD(event):
    timex=0
    global ser
    if ser==0:
        try:
            rxd_ser = serial.Serial(
                app.div_right.list_1.get(),
                app.div_right.list_2.get(),
                timeout=timex,
                stopbits=int(app.div_right.list_3.get()),
                bytesize=int(app.div_right.list_4.get()),
                parity=app.div_right.list_5.get()[0]
            )
            print(rxd_ser)
            if rxd_ser.is_open:
                ser=rxd_ser
                app.div_right.butoon_1['text']="串口关闭"
                app.div_right.butoon_1['bg']="yellow"
                app.div_right.butoon_1['foreground']="black"
                app.div_right.list_1['state']='disabled'
                app.div_right.list_2['state']='disabled'
                app.div_right.list_3['state']='disabled'
                app.div_right.list_4['state']='disabled'
                app.div_right.list_5['state']='disabled'
        except Exception as e:
                ser=0
                print('异常:--------',e)
        return 0
    ser.close()
    ser=0
    app.div_right.butoon_1['text'] = "打开串口"
    app.div_right.butoon_1['bg'] = "green"
    app.div_right.butoon_1['foreground'] = "red"
    app.div_right.list_5['state'] = 'readonly'
    app.div_right.list_4['state'] = 'readonly'
    app.div_right.list_3['state'] = 'readonly'
    app.div_right.list_2['state'] = 'readonly'
    app.div_right.list_1['state'] = 'readonly'
    return 0
##接收信息清理
def Message_clear(event):
    app.div_left.Message.delete('1.0','end')
    return

def SetRTS(event):
    if ser!=0:
        ser.rtscts=bool(1-app.div_right.var_1.get())
        print(ser)
def SetDTR(event):
    if ser!=0:
        ser.dsrdtr=bool(1-app.div_right.var_2.get())

##事件的绑定
def Incident():
    app.div_right.list_1.bind("<Button-1>",port)
    app.div_right.butoon_1.bind('<ButtonRelease-1>',start_RXD)
    app.div_right.butoon_2.bind('<ButtonRelease-1>',Message_clear)
    app.div_right.button_3.bind('<ButtonRelease-1>',Send_Message)
    app.div_right.rad_1.bind('<ButtonRelease-1>',SetRTS)
    app.div_right.rad_2.bind('<ButtonRelease-1>',SetDTR)

def Send_Message(event):
    if ser!=0:##串口被打开  16进制发送格式是0x??
        data=app.div_left.Send.get(0.0,END)##获取需要发送的数据
        if app.div_right.var_6.get()==1:##是否为行发送
            if app.div_right.var_4.get()==1:##是否是16进制发送
                ser.write(bytes([eval(data)]))##16进制发送
            else:
                ser.write(data.encode("utf-8"))
        else:
            if app.div_right.var_4.get()==1:##是否是16进制发送
                ser.write(bytes([eval(data[:-1])]))
            else:
                ser.write(data[:-1].encode("utf-8"))#主函数
if __name__=='__main__':
    ser=0
    ser_id=[]
    app = App()
    Incident()
    while (True):
        tread_RXD()##因为不会多线程 感觉好卡 ,所以去找了方法 用这个方法代替了mainloop的方法 然后就单线程了
        app.update_idletasks()##这行代码与下行代码合起来与app.mainloop()等同.
        app.update()##这行代码与上行代码合起来与app.mainloop()等同.



[Python] 纯文本查看 复制代码
#encoding:utf-8

from tkinter import *
from tkinter import ttk

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.geometry('640x420')
        self.resizable(0, 0)
        self.title('串口工具(自用-51单片机)')
        self.div_left=Div_Left()
        self.div_right=Div_Right()

class Div_Left(Frame):
    def __init__(self,master=None):
        Frame.__init__(self)
        self.createWidgets()
        self.pack(side=LEFT,fill='y')

    def createWidgets(self):
        self.Message=Text(self,width=48,height=20,bg='Black',foreground='blue')
        self.Message.grid(row=0,column=0,pady=5,padx=5)
        self.scr_1=Scrollbar(self,orient=VERTICAL)
        self.scr_1.grid(row=0,column=1,sticky=N+S+W)
        self.scr_1.config(command=self.Message.yview)
        self.Message.config(yscrollcommand=self.scr_1.set)
        self.Send=Text(self,width=48,height=10)
        self.Send.grid(row=1,column=0)
        self.scr_2=Scrollbar(self,orient=VERTICAL)
        self.scr_2.grid(row=1,column=1,sticky=N+S+W)
        self.scr_2.config(command=self.Send.yview)
        self.Send.config(yscrollcommand=self.scr_2.set)

class Div_Right(Frame):
    def __init__(self,master=None):
        Frame.__init__(self)
        self.createWidgets()
        self.pack(side=LEFT,fill='y',)

    def createWidgets(self):
        #复选框的值
        self.var_1=IntVar()
        self.var_2=IntVar()
        self.var_3=IntVar()
        self.var_4=IntVar()
        self.var_6=IntVar()
        ###################
        self.lobel1=Label(self,text='串口选择:',padx=10)
        self.lobel1.grid(row=0,column=0,padx=5)
        self.list_1=ttk.Combobox(self,state='readonly')
        self.list_1.grid(row=0,column=1)
        self.lobel2=Label(self,text='波特率:',padx=10)
        self.lobel2.grid(row=1,column=0,padx=5,pady=15)
        self.list_2=ttk.Combobox(self,state='readonly')
        self.list_2.grid(row=1,column=1)
        self.list_2['value']=['1382400','460800','230400','115200','38400','9600']
        self.list_2.current(5)
        self.lobel3=Label(self,text='停止位:',padx=10)
        self.lobel3.grid(row=2,column=0,padx=5)
        self.list_3=ttk.Combobox(self,state='readonly')
        self.list_3.grid(row=2,column=1)
        self.list_3['value']=['1','1.5','2']
        self.list_3.current(0)
        self.lobel4=Label(self,text='数据位:',padx=10)
        self.lobel4.grid(row=3,column=0,padx=5,pady=15)
        self.list_4=ttk.Combobox(self,state='readonly')
        self.list_4.grid(row=3,column=1)
        self.list_4['value']=['8','7','6','5']
        self.list_4.current(0)
        self.lobel5=Label(self,text='奇偶校验位:',padx=10)
        self.lobel5.grid(row=4,column=0,padx=5)
        self.list_5=ttk.Combobox(self,state='readonly')
        self.list_5.grid(row=4,column=1)
        self.list_5['value']=['None','Odd','Even','Mark','Space']
        self.list_5.current(0)
        self.lobel6=Label(self,text='串口操作:',padx=10)
        self.lobel6.grid(row=5,column=0,padx=5,pady=15)
        self.butoon_1=Button(self,text='串口打开',width=20,bg='green',foreground='red')
        self.butoon_1.grid(row=5,column=1)
        self.rad_1=Checkbutton(self,text='RTS',variable=self.var_1)
        self.rad_1.grid(row=6,column=0)
        self.butoon_2=Button(self,text='接受清除',width=20)
        self.butoon_2.grid(row=6,column=1)
        self.rad_2=Checkbutton(self,text='DTR',variable=self.var_2)
        self.rad_2.grid(row=7,column=0)
        self.rad_3=Checkbutton(self,text='16位进制接收',variable=self.var_3)
        self.rad_3.grid(row=7,column=1)
        self.button_3=Button(self,text='发送',height=7,width=10)
        self.button_3.grid(row=8,column=0,rowspan=8)
        self.rad_4=Checkbutton(self,text='16位进制发送',variable=self.var_4)
        self.rad_4.grid(row=8,column=1)
        self.rad_6=Checkbutton(self,text='添加行发送',variable=self.var_6)
        self.rad_6.grid(row=10,column=1)

界面

界面

免费评分

参与人数 3吾爱币 +3 热心值 +3 收起 理由
huangxw + 1 + 1 谢谢@Thanks!
yanfei_km + 1 + 1 谢谢@Thanks!
星舞天涯 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

至尊丶夏末之刃 发表于 2020-2-28 20:05
牛逼啊,不过还是有挺多串口调试工具都不错的,给丫动手能力点赞~
头像被屏蔽
那年听风 发表于 2020-2-28 20:12
往事不如烟 发表于 2020-2-28 20:28
A00 发表于 2020-2-29 10:10
动手能力点赞
xfwanglxf 发表于 2020-11-19 17:17
先收藏了。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 23:02

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表