每当有来自串行端口 python 3.x 的新数据时,从串行数据更新 tkinter 标签

Posted

技术标签:

【中文标题】每当有来自串行端口 python 3.x 的新数据时,从串行数据更新 tkinter 标签【英文标题】:update tkinter label from serial data whenever there's new data from serial port python 3.x 【发布时间】:2013-07-04 06:45:00 【问题描述】:

我遇到了这个问题,我无法在标签上显示任何值,每当有新数据从串行端口传入时,我想不断更新它。我是python新手,真的需要帮助。

import tkinter
import tkinter.messagebox
import serial
import time

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=4)

class Menu:

    def __init__(self):    

        self.main_window = tkinter.Tk()
        self.main_window.title("Room Light System")
        self.main_window.geometry("1200x600")


        #Frames
        self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs



        #ReceiveLabel
        self.ReceiveLabel = tkinter.Label(self.frame_2,\
                                       text = 'Received DATAs',\
                                       bg = 'White',\
                                       height = 2, width = 20)


        #Temperature
        self.GetTempLabel = tkinter.Label(self.frame_2,\
                                       text='Temperature :')
        self.TempValue = tkinter.StringVar()

        self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
                                               textvariable = self.TempValue
                                               )



        #PACKING!!! F2

        self.frame_2.pack()
        self.frame_2.place(x=410, y=0, height=300, width=400)
        #ReceiveLabel
        self.ReceiveLabel.pack()
        self.ReceiveLabel.place(x=100, y=10)
        #Temperature
        self.GetTempLabel.pack()
        self.GetTempLabel.place(x=50, y=80, height=20, width=120)
        self.GetTempValueLabel.pack()
        self.GetTempValueLabel.place(x=200, y=80, height=20, width=50)


        #main loop and quit
        self.quitButton = tkinter.Button(self.main_window,\
                                          text = 'Quit',
                                          command = self.main_window.destroy,\
                                          height = 2, width = 6)
        self.quitButton.pack() 

        self.quitButton.place(x=200, y=500)


        tkinter.mainloop()

    def GetTemp(self):

        data = bytearray()
        while(1):
            readline = ser.read(size=10)
            if len(readline) > 0 : 
                data = readline
                v = memoryview(data)                
                P = v.tobytes() 
                P = P.decode(encoding='UTF-8')
                self.TempValue.set(P)

gui = Menu()
ser.close()

【问题讨论】:

【参考方案1】:

您可以在与_thread 模块分开的线程中运行您的GetTemp() 方法。使用Tkinter 方法after() 调用线程。在以下示例中,我将您的 GetTemp() 替换为生成的随机数。

import tkinter
import tkinter.messagebox
import time
import random
import _thread

class Menu:

    def __init__(self):    

        self.main_window = tkinter.Tk()
        self.main_window.title("Room Light System")
        self.main_window.geometry("1200x600")


        #Frames
        self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs



        #ReceiveLabel
        self.ReceiveLabel = tkinter.Label(self.frame_2,\
                                       text = 'Received DATAs',\
                                       bg = 'White',\
                                       height = 2, width = 20)


        #Temperature
        self.GetTempLabel = tkinter.Label(self.frame_2,\
                                       text='Temperature :')
        self.TempValue = tkinter.StringVar()

        self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
                                               textvariable = self.TempValue
                                               )



        #PACKING!!! F2

        self.frame_2.pack()
        self.frame_2.place(x=410, y=0, height=300, width=400)
        #ReceiveLabel
        self.ReceiveLabel.pack()
        self.ReceiveLabel.place(x=100, y=10)
        #Temperature
        self.GetTempLabel.pack()
        self.GetTempLabel.place(x=50, y=80, height=20, width=120)
        self.GetTempValueLabel.pack()
        self.GetTempValueLabel.place(x=200, y=80, height=20, width=50)


        #main loop and quit
        self.quitButton = tkinter.Button(self.main_window,\
                                          text = 'Quit',
                                          command = self.main_window.destroy,\
                                          height = 2, width = 6)
        self.quitButton.pack() 

        self.quitButton.place(x=200, y=500)


        self.main_window.after(2000, _thread.start_new_thread, self.GetTemp, ())
        tkinter.mainloop()

    def GetTemp(self):

        while(1):
            value = random.random()
            self.TempValue.set(str(value))
            time.sleep(0.5)

gui = Menu()

【讨论】:

你好,它适用于随机生成的数字,但是当我在串口上使用它时,它仍然没有在标签上显示任何东西 我不知道你的串口模块。是pyserial.sourceforge.net 吗?我想你应该先尝试让它在没有 Tkinter 的情况下工作,然后将它应用到你的 GUI 应用程序中。【参考方案2】:

在方法中调用serial..它会起作用..这是我使用Holger先生概念的serial代码.. 这段代码是用树莓派3串口ttyS0作为master和串口连接的传感器测试的.....

import tkinter
import tkinter.messagebox
import time
import _thread
import serial

class Menu:

    def __init__(self):    

        self.main_window = tkinter.Tk()
        self.main_window.title("Serial Data monitor")
        self.main_window.geometry("1000x600")


        #Frames
        self.frame_2 = tkinter.Frame(self.main_window, bg='Orange') # Receiving DATAs



        #ReceiveLabel
        self.ReceiveLabel = tkinter.Label(self.frame_2,\
                                       text = 'Received DATAs',\
                                       bg = 'White',\
                                       height = 2, width = 20)


        #RPM
        self.GetTempLabel = tkinter.Label(self.frame_2,\
                                       text='RPM :')
        self.TempValue = tkinter.StringVar()

        self.GetTempValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\
                                               textvariable = self.TempValue
                                               )


        #torque
        self.GetTemppLabel = tkinter.Label(self.frame_2,\
                                       text='TORQUE :')
        self.TemppValue = tkinter.StringVar()

        self.GetTemppValueLabel = tkinter.Label(self.frame_2,bg = 'Green',\textvariable = self.TempValue)

PACKING!!! F2

        self.frame_2.pack()
        self.frame_2.place(x=410, y=0, height=300, width=400)
        #ReceiveLabel
        self.ReceiveLabel.pack()
        self.ReceiveLabel.place(x=100, y=10)
        #rpm
        self.GetTempLabel.pack()
        self.GetTempLabel.place(x=50, y=80, height=20, width=80)
        self.GetTempValueLabel.pack()
        self.GetTempValueLabel.place(x=200, y=80, height=20, width=120)

        #torque
        self.GetTemppLabel.pack()
        self.GetTemppLabel.place(x=50, y=120, height=20, width=80)
        self.GetTemppValueLabel.pack()
        self.GetTemppValueLabel.place(x=200, y=120, height=20, width=120)

        #main loop and quit
        self.quitButton = tkinter.Button(self.main_window,\
                                          text = 'Quit',
                                          command = self.main_window.destroy,\
                                          height = 2, width = 6)
        self.quitButton.pack() 

        self.quitButton.place(x=200, y=500)


        self.main_window.after(2000, _thread.start_new_thread, self.GetTemp, ())
        self.main_window.after(4000, _thread.start_new_thread, self.GetTempp, ())
        tkinter.mainloop()

    def GetTemp(self):
        s=serial.Serial(port='/dev/ttyS0',baudrate=9600)
        string='*00T%'
        while True:
            s.write(str.encode(string))
            print(string)
            time.sleep(2)
            if s.inWaiting():
               temp=s.readline(s.inWaiting())
               value=temp.decode('utf-8')
               value=value[5:-1]
    def GetTempp(self):
        s=serial.Serial(port='/dev/ttyS0',baudrate=9600)
        string1='*01T%'
        while True:
            s.write(str.encode(string1))
            print(string1)
            time.sleep(2)
            if s.inWaiting():
               tempp=s.readline(s.inWaiting())
               value1=tempp.decode('utf-8')
               value1=value1[5:-1]
               print(value1)
               self.TemppValue.set(str(value1))
               time.sleep(0.5)



gui = Menu()

【讨论】:

以上是关于每当有来自串行端口 python 3.x 的新数据时,从串行数据更新 tkinter 标签的主要内容,如果未能解决你的问题,请参考以下文章

Qt 串行端口错误 - 未读取数据

从串行端口发送十六进制数据的问题

asyncio:等待来自其他线程的事件

Python和C应用程序之间的串行端口数据传输(在Microblaze上运行)

使用 Python 最大化来自 Teensy 3.2 的实时绘图数据的串行通信速度

串行端口未正确刷新