久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1925|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)的串口通信上位機(jī)代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1031401 發(fā)表于 2022-5-31 23:03 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
就想蹭黑幣


import serial
from serial.tools import list_ports
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import *


class MySerial:
    def __init__(self):
        self.ser = serial.Serial()

    # 顯示連接的端口

    # 建立連接
    def connect(self, port, baudrate, bytesize, stopbits, parity, timeout):
        # 如果之前打開的串口沒有關(guān)閉,這里自動關(guān)閉
        if self.ser.is_open:
            self.ser.close()
        self.ser = serial.Serial(port=port, baudrate=baudrate,bytesize=bytesize, stopbits=stopbits, parity=parity, timeout=timeout)
        print("連接成功")

    # 發(fā)送數(shù)據(jù)
    def send(self, data):
        self.ser.write(data.encode())

    # 接受數(shù)據(jù)
    def rcve(self):
        pass

    def close(self):
        self.ser.close()


class Interface:
    def __init__(self):
        self.my_serial = MySerial()

        self.window =tk.Tk()
        self.window.title("LED上位機(jī)")
        self.window.geometry("400x300")
        self.port_name_var = tk.Variable()
        self.baudrate_var = tk.Variable()
        self.button_connect_var = tk.Variable()
        self.button_connect_var.set("打開串口")

        self.serial_status = "OFF"

        self.combobox_port = ttk.Combobox(self.window, textvariable=self.port_name_var)
        self.combobox_baudrate = ttk.Combobox(self.window, textvariable=self.baudrate_var)
        self.button_connect = tk.Button(self.window, textvariable=self.button_connect_var, command=self.open_close_serial)
        self.button_led1 = tk.Button(self.window, text="LED1", command=lambda: self.command(str(chr(0x01))))
        self.button_led2 = tk.Button(self.window, text="LED2", command=lambda: self.command(str(chr(0x02))))
        self.button_led3 = tk.Button(self.window, text="LED3", command=lambda: self.command(str(chr(0x03))))
        self.button_led4 = tk.Button(self.window, text="LED4", command=lambda: self.command(str(chr(0x04))))
        self.button_led5 = tk.Button(self.window, text="LED5", command=lambda: self.command(str(chr(0x05))))
        self.button_led6 = tk.Button(self.window, text="LED6", command=lambda: self.command(str(chr(0x06))))
        self.button_led7 = tk.Button(self.window, text="LED7", command=lambda: self.command(str(chr(0x07))))
        self.button_led8 = tk.Button(self.window, text="LED8", command=lambda: self.command(str(chr(0x08))))

        self.combobox_port.place(relx=1/9, rely=1/8, relwidth=2/9, relheight=1/10)
        self.combobox_baudrate.place(relx=6/9, rely=1/8, relwidth=2/9, relheight=1/10)
        self.button_connect.place(relx=3/9, rely=5/16, relwidth=3/9, relheight=1/8)
        self.button_led1.place(relx=1 / 9, rely=2 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led2.place(relx=3 / 9, rely=2 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led3.place(relx=5 / 9, rely=2 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led4.place(relx=7 / 9, rely=2 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led5.place(relx=1 / 9, rely=3 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led6.place(relx=3 / 9, rely=3 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led7.place(relx=5 / 9, rely=3 / 4, relwidth=1 / 9, relheight=1 / 8)
        self.button_led8.place(relx=7 / 9, rely=3 / 4, relwidth=1 / 9, relheight=1 / 8)

    # 顯示串口列表
    def show_list(self):
        # 顯示串口列表
        self.combobox_port['values'] = [file:///C:\Users\21039\AppData\Roaming\Tencent\QQ\Temp\%W@GJ$ACOF(TYDYECOKVDYB.pngx.name for x in list(list_ports.comports())]
        # 顯示波特率列表
        self.combobox_baudrate['values'] = [600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200]

    def open_close_serial(self):
        var = self.button_connect_var.get()
        if var == "打開串口":
            port = self.combobox_port.get()
            baudrate = self.baudrate_var.get()
            print(port)
            if port == "":
                return
            try:
                self.my_serial.connect(port=port, baudrate=baudrate, bytesize=8, stopbits=1, parity="N", timeout=1)
                self.button_connect_var.set("關(guān)閉串口")
                self.combobox_baudrate.configure(state="disable")
                self.combobox_port.configure(state="disable")
            except:
                showerror("串口錯誤", "該串口被占用或不存在!")
        else:
            self.my_serial.close()
            self.button_connect_var.set("打開串口")
            self.combobox_baudrate.configure(state="enable")
            self.combobox_port.configure(state="enable")

    def command(self, data):
        self.my_serial.send(data=data)

    def run(self):
        self.show_list()


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 在线免费黄色小视频 | 欧产日产国产精品视频 | 国产日韩欧美一区二区 | 精品亚洲一区二区三区 | 激情小视频 | 免费成人高清在线视频 | 成人精品系列 | 日本不卡免费新一二三区 | 亚洲综合视频一区 | 欧美久久一区二区 | 久久一区二区三区四区五区 | 中文字幕一区二区三区四区五区 | 国外激情av | 精品视频一区二区三区在线观看 | 亚洲激情在线 | 国产精品视频一区二区三区 | 成人亚洲片 | 欧美性video| 中文字幕免费 | 精品免费视频一区二区 | 国产精品久久一区二区三区 | 青青艹在线视频 | 午夜天堂 | 久久99久久98精品免观看软件 | 国产最新精品视频 | 日本欧美在线 | 一区二区三区在线免费看 | 欧美一区在线视频 | 久久国产美女视频 | 孰女乱色一区二区三区 | 国产精品视频一区二区三区 | 九九视频在线观看视频6 | 久久一区精品 | 国产日韩精品一区 | 久久久久久国产免费视网址 | 91一区二区 | 国产人成精品一区二区三 | 国产成在线观看免费视频 | 四虎影院在线观看av | 午夜资源 | 精品免费在线 |