溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

對(duì)Python 簡(jiǎn)單串口收發(fā)GUI界面的實(shí)例詳解

發(fā)布時(shí)間:2020-09-03 14:58:31 來源:腳本之家 閱讀:228 作者:freedom098 欄目:開發(fā)技術(shù)

忙活了三個(gè)多小時(shí),連學(xué)帶做,總算是搞出來了一個(gè)具有基本功能的串口通信PC機(jī)的GUI界面,Tkinter在python中確實(shí)很好用,而且代碼量確實(shí)也很少,不足的是Tkinter不自帶combox,但是幸運(yùn)的是我下載的2.7版本自帶了包含有combox的ttk模塊,于是乎問題就順利解決了。

下面是源代碼,一些錯(cuò)誤提示功能還沒有做,目前只是簡(jiǎn)單地實(shí)現(xiàn)了下位機(jī)與PC的通信界面,下位機(jī)還是用的STM32F103

#encoding=utf-8
__author__ = 'freedom'
 
from Tkinter import *
from serial import *
import ttk
 
class GUI(Frame):
  def __init__(self,master):
    frame = Frame(master)
    frame.pack()
    #串口設(shè)置相關(guān)變量
    self.port = 0
    self.baudrate = 9600
    #串口號(hào)提示
    self.lab1 = Label(frame,text = 'Serial Number')
    self.lab1.grid(row = 0,column = 0,sticky = W)
    #串口號(hào)選擇下拉菜單
    self.boxValue = StringVar()
    self.boxChoice = ttk.Combobox(frame,textvariable = self.boxValue,state = 'readonly')
    self.boxChoice['value'] = ('COM1','COM2','COM3','COM4')
    self.boxChoice.current(0)
    self.boxChoice.bind('<<ComboboxSelected>>',self.Choice)
    self.boxChoice.grid(row = 1,column = 0,sticky = W)
    #波特率選擇提示
    self.lab2 = Label(frame,text = 'Baudrate Set')
    self.lab2.grid(row = 2,column = 0,sticky = W)
    #波特率選擇下拉菜單
    self.boxValueBaudrate = IntVar()
    self.BaudrateChoice = ttk.Combobox(frame,textvariable = self.boxValueBaudrate,state = 'readonly')
    self.BaudrateChoice['value'] = (9600,115200)
    self.BaudrateChoice.current(0)
    self.BaudrateChoice.bind('<<ComboboxSelected>>',self.ChoiceBaudrate)
    self.BaudrateChoice.grid(row = 3,column = 0,sticky = W)
    #輸出框提示
    self.lab3 = Label(frame,text = 'Message Show')
    self.lab3.grid(row = 0,column = 1,sticky = W)
    #輸出框
    self.show = Text(frame,width = 40,height = 5,wrap = WORD)
    self.show.grid(row = 1,column = 1,rowspan = 4,sticky = W)
    #輸入框提示
    self.lab4 = Label(frame,text = 'Input here,please!')
    self.lab4.grid(row = 5,column = 1,sticky = W)
    #輸入框
    self.input = Entry(frame,width = 40)
    self.input.grid(row = 6,column = 1,rowspan = 4,sticky = W)
    #輸入按鈕
    self.button1 = Button(frame,text = "Input",command = self.Submit)
    self.button1.grid(row = 11,column = 1,sticky = E)
    #串口開啟按鈕
    self.button2 = Button(frame,text = 'Open Serial',command = self.open)
    self.button2.grid(row = 7,column = 0,sticky = W)
    #串口關(guān)閉按鈕
    self.button3 = Button(frame,text = 'Close Serial',command = self.close)
    self.button3.grid(row = 10,column = 0,sticky = W)
    #串口信息提示框
    self.showSerial = Text(frame,width = 20,height = 2,wrap = WORD)
    self.showSerial.grid(row = 12,column = 0,sticky = W)
    #串口初始化配置
    self.ser = Serial()
    self.ser.setPort(self.port)
    #self.ser.setBaudrate(self.baudrate)
    #self.ser.open()
    #print self.ser.isOpen()
    #print self.ser
  def Choice(self,event):
    context = self.boxValue.get()
    list = ["COM1",'COM2','COM3','COM4']
    if context in list:
      self.port = list.index(context)
      self.ser.setPort(self.port)
    print self.port
  def ChoiceBaudrate(self,event):
    self.baudrate = self.boxValueBaudrate.get()
    self.ser.setBaudrate(self.baudrate)
    print self.baudrate
  def Submit(self):
    context1 = self.input.get()
    n = self.ser.write(context1)
    output = self.ser.read(n)
    print output
    self.show.delete(0.0,END)
    self.show.insert(0.0,output)
  def open(self):
    self.ser.open()
    if self.ser.isOpen() == True:
      self.showSerial.delete(0.0,END)
      self.showSerial.insert(0.0,"Serial has been opend!")
  def close(self):
    self.ser.close()
    if self.ser.isOpen() == False:
      self.showSerial.delete(0.0,END)
      self.showSerial.insert(0.0,"Serial has been closed!")
root = Tk()
root.title("Serial GUI")
#root.geometry("3000x4000")
app = GUI(root)
root.mainloop()

以上這篇對(duì)Python 簡(jiǎn)單串口收發(fā)GUI界面的實(shí)例詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI