4、python簡單程序讀取串口信息的方法
具體分析如下:
這段代碼需要調用serial模塊,通過while循環不斷讀取串口數據
5、Python簡單串口收發GUI界面
以下是一個具有基本功能的串口通信PC機的GUI界面,簡單地實現了下位機與PC的通信界面,下位機還是用的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()
#串口設置相關變量
self.port = 0
self.baudrate = 9600
#串口號提示
self.lab1 = Label(frame,text = ‘Serial Number’)
self.lab1.grid(row = 0,column = 0,sticky = W)
#串口號選擇下拉菜單
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)
評論
查看更多