用于在窗口上打印串行数据的 python 代码. [英] python code for serial data to print on window.

查看:34
本文介绍了用于在窗口上打印串行数据的 python 代码.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 python 和 pyserial 很陌生.我的电脑安装了带有 pyserial 的 python 2.7.4,我想在我的电脑上的单独窗口上打印串行接收的数据.首先必须打开窗口,然后在该窗口上打印串行数据之后.这里窗口必须打开一次,串行数据必须连续打印在窗口上,直到设备停止传输数据.我试过这段代码,但它毫无价值.请有人帮我写代码.

I am quite new to python and pyserial. My pc was installed with python 2.7.4 with pyserial and I want to print the serially received data on a seperate window on my pc. First the window has to be opened, then after the serial data should print on that window. Here the window has to be opened once and the serial data has to be continously print on the window until the device stops tramsmitting the data. I tried with this code, but its worthless. please someone help me with the code.

import serial
import Tkinter
from Tkinter import *
s = serial.Serial('COM10',9600)    # open serial port
master = Tk()
master.geometry("1360x750")        # a window pop up with width (1360) and height(750)     which exatly fits my monitor screen..

while 1:
if s.inWaiting():
text = s.readline(s.inWaiting())
frameLabel = Frame( master, padx=40, pady =40)
frameLabel.pack()
w = Text( frameLabel, wrap='word', font="TimesNewRoman 37")
w.insert(12.0,text )
w.pack()
w.configure( bg=master.cget('bg'), relief='flat', state='Normal' )

mainloop()

推荐答案

这里的问题是您有两个应该持续运行的循环:GUI 的主循环和传输串行数据的循环.你可以做的解决这个问题是启动一个新线程来接收串口的内容,把它放在一个 Queue,并在 GUI 线程中定期检查此队列的内容:

The problem here is that you have two loops that should be constantly running: The mainloop for the GUI and the loop for transmitting the serial data. What you can do to solve this is to start a new thread to receive the content of the serial port, put it in a Queue, and check periodically in the GUI thread the content of this queue:

import serial
import threading
import time
import Queue
import Tkinter as tk


class SerialThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        s = serial.Serial('/dev/ttyS0',9600)
        s.write(str.encode('*00T%'))
        time.sleep(0.2)
        while True:
            if s.inWaiting():
                text = s.readline(s.inWaiting())
                self.queue.put(text)

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("1360x750")
        frameLabel = tk.Frame(self, padx=40, pady =40)
        self.text = tk.Text(frameLabel, wrap='word', font='TimesNewRoman 37',
                            bg=self.cget('bg'), relief='flat')
        frameLabel.pack()
        self.text.pack()
        self.queue = Queue.Queue()
        thread = SerialThread(self.queue)
        thread.start()
        self.process_serial()

    def process_serial(self):
        value=True
        while self.queue.qsize():
            try:
                new=self.queue.get()
                if value:
                 self.text.delete(1.0, 'end')
                value=False
                 self.text.insert('end',new)
            except Queue.Empty:
                pass
        self.after(100, self.process_serial)

app = App()
app.mainloop()

这段代码是用我的 Pi3 ttyS0 串口和串口连接的 PC 和从设备测试的:它 100% 使用串行连接的单个设备

This code is tested with my Pi3 ttyS0 serial port and serially connected PC and slave device: its 100% working with single device connected serially

这篇关于用于在窗口上打印串行数据的 python 代码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆