Загрузка данных


import tkinter as tk
import serial
import time

ser = None
connected = False

def connect():
    global ser, connected
    try:
        ser = serial.Serial('COM4', 9600, timeout=1)
        time.sleep(2)
        connected = True
        status_label.config(text="Подключено к COM4", fg="green")
        connect_btn.config(state=tk.DISABLED)
    except:
        status_label.config(text="Ошибка подключения", fg="red")

def send_color(color):
    global ser, connected
    if connected and ser:
        ser.write(f"{color}\n".encode())

def on_close():
    global ser, connected
    if ser:
        ser.close()
    root.destroy()

root = tk.Tk()
root.title("RGB LED Control")
root.geometry("300x400")

status_label = tk.Label(root, text="Не подключено", fg="red")
status_label.pack(pady=10)

connect_btn = tk.Button(root, text="Подключиться к COM4", command=connect)
connect_btn.pack(pady=10)

tk.Button(root, text="Красный", command=lambda: send_color("RED")).pack(pady=5)
tk.Button(root, text="Зеленый", command=lambda: send_color("GREEN")).pack(pady=5)
tk.Button(root, text="Синий", command=lambda: send_color("BLUE")).pack(pady=5)
tk.Button(root, text="Желтый", command=lambda: send_color("YELLOW")).pack(pady=5)
tk.Button(root, text="Голубой", command=lambda: send_color("BLUE1")).pack(pady=5)
tk.Button(root, text="Фиолетовый", command=lambda: send_color("PURPLE")).pack(pady=5)
tk.Button(root, text="Оранжевый", command=lambda: send_color("ORANGE")).pack(pady=5)
tk.Button(root, text="Белый", command=lambda: send_color("WHITE")).pack(pady=5)
tk.Button(root, text="Выключить", command=lambda: send_color("OFF")).pack(pady=5)

root.protocol("WM_DELETE_WINDOW", on_close)
root.mainloop()