Tkinter处理STM32串口发送的数据(self)

操作步骤:

1、输入正确的com口,然后回车

python tkinter 输入内容保存 tkinter输出结果_Text


2、出现下图所示的上位机界面

python tkinter 输入内容保存 tkinter输出结果_Text_02

注意:
使用 data = serial.readline()函数,一直读取到\n才会结束,没有\n就会一直读取
使用下面代码自动获取当前所有串口信息

import serial.tools.list_ports
port_list = list(serial.tools.list_ports.comports())
if len(port_list) == 0:
   print('找不到串口')
else:
    for i in range(0,len(port_list)):
    print(port_list[i])

源程序代码:

#import os
#import sys
import serial
from tkinter import *
# import time

jishu1 = 0


def recv(serial):     #从串口获取数据
    while True:
        global jishu1
        # print(jishu1)

        # if jishu1 < 3:  #前三次主要是将数据长度稳定下来
        #     # data1 = serial.read_all()
        #     data2 = serial.readline()  #读取printf中的数据,遇到\n结束,否则一直读取,会陷入死循环
        #     # print('data2的长度为:{}'.format(data2))
        #     data3 = serial.readline()  #小技巧:需要读取两次,第一次读取的可能信息不全,不够11个数
        #     # print('data3的长度为:{}'.format(data3))
        # else:
        #     data3 = serial.readline()  # 小技巧:需要读取两次,第一次读取的可能信息不全,不够11个数
        #     # print(len(data3))
        # print(data3)
        # print(len(data3))
        # jishu1 = jishu1 + 1
        # if jishu1 == 1000:
        #     jishu1 = 5

        data3 = serial.readline()
        data_str = str(data3)
        if data_str.count(',') != 10:
            data3 = b'0,0,1,0.0,0.00,0.000,0.000,0.00,0.0,0,0.00\n'
            jishu1 = 1
        elif data_str.count(',') == 10:
            data3 = data3
            jishu1 = 0

        # if data2 == '':
        #     continue
        # elif data2 == '\n':
        #     print('串口出错')
        #     continue

        return data3


def exit_pro():
    try:
        exit("sorry")
    except:
        print("Exit Error")    


def lka_all():
    data1 = recv(serial)  #将数据进行解析
    global jishu1
    if data1 != b'':
        lka_cs_in = data1.split(str.encode(','))[0]  #车速信息
        # lka_cs_in = data1[0]  # 车速信息
        text_cs.delete('1.0', 'end')
        text_cs.insert(END, lka_cs_in)

        lka_sign_timer_in = data1.split(str.encode(','))[1]  #程序执行到那一步的标志位.
        # lka_sign_timer_in = data1[1]  # 程序执行到那一步的标志位
        text_sign_timer.delete('1.0', 'end')
        text_sign_timer.insert(END, lka_sign_timer_in)

        lka_on_in = data1.split(str.encode(','))[2]  #LKA的总开关是否开启
        # lka_on_in = data1[2]  # LKA的总开关是否开启
        text_lka_on_in.delete('1.0', 'end')
        text_lka_on_in.insert(END, lka_on_in)

        torque_in = data1.split(str.encode(','))[3]  #方向盘的扭矩,驾驶员驾驶的扭矩
        # torque_in = data1[3]  # 方向盘的扭矩,驾驶员驾驶的扭矩
        text_torque_in.delete('1.0', 'end')
        text_torque_in.insert(END, torque_in)

        angle_main_in = data1.split(str.encode(','))[4]  #方向盘的转角
        # angle_main_in = data1[4]  #方向盘的转角
        text_angle_main_in.delete('1.0', 'end')
        text_angle_main_in.insert(END, angle_main_in)

        c0_l_in = data1.split(str.encode(','))[5]   #距离左边线的距离
        # c0_l_in = data1[5]   #距离左边线的距离
        text_c0_l_in.delete('1.0', 'end')
        text_c0_l_in.insert(END, c0_l_in)

        c0_r_in = data1.split(str.encode(','))[6]  #距离右边线的距离
        # c0_r_in = data1[6]  # 距离右边线的距离
        text_c0_r_in.delete('1.0', 'end')
        text_c0_r_in.insert(END, c0_r_in)

        azi_main_in = data1.split(str.encode(','))[7]  #车身的方位角
        # azi_main_in = data1[7]  # 车身的方位角
        text_azi_main_in.delete('1.0', 'end')
        text_azi_main_in.insert(END, azi_main_in)

        deviation_reminder = data1.split(str.encode(','))[8]  #是否满足偏离条件,左偏为1,右偏为-1
        # deviation_reminder = data1[8]  # 是否满足偏离条件,左偏为1,右偏为-1
        text_deviation_reminder.delete('1.0', 'end')
        text_deviation_reminder.insert(END, deviation_reminder)
        # print(deviation_reminder)
        # print(type(deviation_reminder))

        touchline_sign = data1.split(str.encode(','))[9]  # 是否压线
        # touchline_sign = data1[9]  # 是否压线
        text_touchline_sign.delete('1.0', 'end')
        text_touchline_sign.insert(END, touchline_sign)
        # print(touchline_sign)

        Tor_Value = data1.split(str.encode(','))[10]  # 是否压线
        # Tor_Value = data1[10]  # 是否压线
        text_Tor_Value.delete('1.0', 'end')
        text_Tor_Value.insert(END, Tor_Value)

        if deviation_reminder == b'1.0' and jishu1 == 0:
            text_alert.delete('1.0', 'end')
            text_alert.insert(END, '左偏离')
            if touchline_sign == b'1':
                text_alert.delete('1.0', 'end')
                text_alert.insert(END, '左偏离\n压线')
        elif deviation_reminder == b'-1.0' and jishu1 == 0:
            text_alert.delete('1.0', 'end')
            text_alert.insert(END, '右偏离')
            if touchline_sign == b'1':
                text_alert.delete('1.0', 'end')
                text_alert.insert(END, '右偏离\n压线')
        elif touchline_sign == b'1':
            text_alert.delete('1.0', 'end')
            text_alert.insert(END, '压线')
        elif jishu1 == 1:
            text_alert.delete('1.0', 'end')
            text_alert.insert(END, '数据错误')
        else:
            text_alert.delete('1.0', 'end')

        root.after(1, lka_all)


if __name__ == '__main__':
    # start = time.clock()
    print('请输入串口:(比如:COM4 (不区分大小写))')
    com_kou = input()  #default = 'COM4'
    # com_kou = 'COM4'
    print('STM32波特率设置为:115200')
    com_bps = 115200  #default = 115200
    serial = serial.Serial(com_kou, com_bps)  # /dev/ttyUSB0

    # print(serial.port)
    # print(serial.baudrate)

    if serial.isOpen():
        print("Open Success")
    else:
        print("Open Failed")

    root = Tk()
    root.geometry('700x500')
    root.title('豫北LKA辅助驾驶数据读取')

    frame1 = Frame(root, relief=RAISED)
    frame1.place(relx=0.8)

    #btn1 = Button(root, text='退出程序', command=exit_pro)
    #btn1.place(relx=0.85, rely=0.1, relwidth=0.1, relheight=0.1)

    text_cs = Text(root)
    lb_cs = Label(root, text='车速:', fg='blue', font=("宋体", 10))

    text_sign_timer = Text(root)
    lb_sign = Label(root, text='sign_timer:', fg='blue', font=("宋体", 10))

    text_lka_on_in = Text(root)
    lb_lka_on = Label(root, text='LKA_ON:', fg='blue', font=("宋体", 10))

    text_torque_in = Text(root)
    lb_torque = Label(root, text='Torque:', fg='blue', font=("宋体", 10))

    text_angle_main_in = Text(root)
    lb_angle_main = Label(root, text='angle_main:', fg='blue', font=("宋体", 10))

    text_c0_l_in = Text(root)
    lb_c0_l = Label(root, text='C0_L:', fg='blue', font=("宋体", 10))

    text_c0_r_in = Text(root)
    lb_c0_r = Label(root, text='C0_R:', fg='blue', font=("宋体", 10))

    text_azi_main_in = Text(root)
    lb_azi_main = Label(root, text='azi_main:', fg='blue', font=("宋体", 10))

    text_deviation_reminder = Text(root)
    lb_deviation_reminder = Label(root, text='deviation:', fg='blue', font=("宋体", 10))

    text_touchline_sign = Text(root)
    lb_touchline_sign = Label(root, text='touchline:', fg='blue', font=("宋体", 10))

    text_Tor_Value = Text(root)
    lb_text_Tor_Value = Label(root, text='Tor_Value:', fg='blue', font=("宋体", 10))

    text_alert = Text(root, fg='red', font=("宋体", 30))
    lb_text_alert = Label(root, text='警告:', fg='red', font=("宋体", 30))


####上面为数据的显示,下面为数据放置的位置##############################

    text_cs.place(relx=0.15, rely=0.05, relwidth=0.1, relheight=0.04)
    lb_cs.place(relx=0.02, rely=0.05, relwidth=0.1, relheight=0.04)

    text_sign_timer.place(relx=0.15, rely=0.15, relwidth=0.1, relheight=0.04)
    lb_sign.place(relx=0.02, rely=0.15, relwidth=0.1, relheight=0.04)

    text_lka_on_in.place(relx=0.15, rely=0.25, relwidth=0.1, relheight=0.04)
    lb_lka_on.place(relx=0.02, rely=0.25, relwidth=0.1, relheight=0.04)

    text_torque_in.place(relx=0.15, rely=0.35, relwidth=0.1, relheight=0.04)
    lb_torque.place(relx=0.02, rely=0.35, relwidth=0.1, relheight=0.04)

    text_angle_main_in.place(relx=0.15, rely=0.45, relwidth=0.1, relheight=0.04)
    lb_angle_main.place(relx=0.02, rely=0.45, relwidth=0.1, relheight=0.04)

    text_c0_l_in.place(relx=0.15, rely=0.55, relwidth=0.1, relheight=0.04)
    lb_c0_l.place(relx=0.02, rely=0.55, relwidth=0.1, relheight=0.04)

    text_c0_r_in.place(relx=0.15, rely=0.65, relwidth=0.1, relheight=0.04)
    lb_c0_r.place(relx=0.02, rely=0.65, relwidth=0.1, relheight=0.04)

    text_azi_main_in.place(relx=0.15, rely=0.75, relwidth=0.1, relheight=0.04)
    lb_azi_main.place(relx=0.02, rely=0.75, relwidth=0.1, relheight=0.04)

    text_deviation_reminder.place(relx=0.15, rely=0.85, relwidth=0.1, relheight=0.04)
    lb_deviation_reminder.place(relx=0.02, rely=0.85, relwidth=0.1, relheight=0.04)

    text_touchline_sign.place(relx=0.15, rely=0.93, relwidth=0.1, relheight=0.04)
    lb_touchline_sign.place(relx=0.02, rely=0.93, relwidth=0.1, relheight=0.04)

    text_Tor_Value.place(relx=0.45, rely=0.05, relwidth=0.1, relheight=0.04)
    lb_text_Tor_Value.place(relx=0.32, rely=0.05, relwidth=0.1, relheight=0.04)

    text_alert.place(relx=0.7, rely=0.7, relwidth=0.25, relheight=0.2)
    lb_text_alert.place(relx=0.55, rely=0.7, relwidth=0.15, relheight=0.2)

    # try:
    #     lka_all()
    # except IndexError as s:
    #     pass
    lka_all()
    # end = time.clock()  #测试本程序运行一次需要的时间
    # print(end - start)
    root.mainloop()

    while True:
        data = recv(serial)
        if data != b'':
            # print("receive : ", data)

            # data0 = data.split(str.encode(','))[0]
            # print('车速:{}'.format(data0))
            lka_all()

        else:
            print('failedopenw')

最后使用pyinstaller -F xxx.py命令将文件封装成上位机形式