2022年 11月 4日

使用python实现一个简陋的上位机

import tkinter as tk
from tkinter import ttk
import serial  # 导入模块
import threading
import serial.tools.list_ports
import base64
import os

def serialRead(ser):
    # 有可能与串口测试部分冲突,需要将此设置为最高级别
    while True:  # 读是一直读,写是有限次
        count = ser.inWaiting()
        if count > 0:
            try:
                data = ser.read(count).decode(encoding='utf-8')
                accept_area.config(state='normal')
                accept_area.insert('end', data)
                accept_area.config(state='disabled')
            except Exception as e:
                error_label['text'] = str(e)


from icon import img
win = tk.Tk()
win.title("一个简陋的上位机")  # 添加标题
win.geometry('400x430')
win.resizable(False,False)

error_label = tk.Label(win, text="运行状态", )
error_label.grid(column=0, row=0, columnspan=4)
ttk.Label(win, text="波特率").grid(column=1, row=1)
ttk.Label(win, text="端口号").grid(column=0, row=1)


def add_n(message):
    message_list = list(message)
    for i in range(len(message)):
        if i % 30 == 0:
            message_list.insert(i, '\n')
    return ''.join(message_list)


def open_uart():
    try:
        global ser
        timex = 5
        # 打开串口,并得到串口对象
        ser = serial.Serial(number1.get(), number.get(), timeout=timex)
        error_label['text'] = "串口打开成功"
        ReadThread = threading.Thread(target=serialRead, args=(ser,))
        ReadThread.daemon = True
        ReadThread.start()
    except Exception as e:
        error_label['text'] = add_n(str(e))
        pass


def close():
    try:
        global ser
        ser.close()
        error_label.configure(text="串口关闭成功")
    except Exception as e:
        error_label.configure(text=add_n(str(e)))
        pass


button_open = ttk.Button(win, text="打开串口", command=open_uart)
button_open.grid(column=2, row=2)

button_close = ttk.Button(win, text="关闭串口", command=close)
button_close.grid(column=3, row=2)

port_list = list(serial.tools.list_ports.comports())
port_list_names = [i.name for i in port_list]

number1 = tk.StringVar()
numberChosen = ttk.Combobox(win, width=12, textvariable=number1, state='readonly')
numberChosen['values'] = port_list_names
numberChosen.grid(column=0, row=2)
numberChosen.current(0)

number = tk.StringVar()
numberChosen = ttk.Combobox(win, width=12, textvariable=number, state='readonly')
numberChosen['values'] = (
50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200) 
numberChosen.grid(column=1, row=2) 
numberChosen.current(12)

ttk.Label(win, text="发送区").grid(column=0, row=3, columnspan=2,)  # 添加一个标签0
send_area = tk.Text(win, width=30, height=10, )
send_area.grid(column=0, row=4, columnspan=2, rowspan=2)


def send():
    try:
        data = send_area.get('1.0', 'end')
        global ser

        # print(repr(data))
        ser.write(data[:-1].encode("ascii"))
        error_label['text'] = "%s 发送成功" % data
    except Exception as e:
        error_label['text'] = add_n(str(e))
        pass

def send_clear():
    try:
        send_area.delete('1.0', 'end')
        error_label['text'] = "发送区清空成功"
    except Exception as e:
        error_label['text'] = add_n(str(e))
        pass

button_send = ttk.Button(win, text="发送数据", command=send)
button_send.grid(column=2, row=4, columnspan=2)

button_send_clear = ttk.Button(win, text="清空发送区", command=send_clear)
button_send_clear.grid(column=2, row=5, columnspan=2)

ttk.Label(win, text="接收区").grid(column=0, row=6, columnspan=2,)
accept_area = tk.Text(win, width=30, height=10, )
accept_area.grid(column=0, row=7, columnspan=2, rowspan=2)
accept_area.config(state='disabled')

def accept_clear():
    try:
        accept_area.config(state='normal')
        accept_area.delete('1.0', 'end')
        accept_area.config(state='disabled')
        error_label['text'] = "接收区清空成功"
    except Exception as e:
        error_label['text'] = add_n(str(e))
        pass


button_accept_clear = ttk.Button(win, text="清空接收区", command=accept_clear)
button_accept_clear.grid(column=2, row=7, columnspan=2, rowspan=2)


win.mainloop()  # 当调用mainloop()时,窗口才会显示出来

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142