简体   繁体   English

Python中的“线程”,绘制接收到的数据并同时发送

[英]“Threading” in Python, plotting received data and sending simultaneously

I am asking for some high level advice here. 我在这里寻求一些高级建议。 I am using Python to plot data that is received constantly through serial. 我正在使用Python绘制通过串行不断接收的数据。 At the same time, I want the user to be able to input data through a prompt (such as the Python shell). 同时,我希望用户能够通过提示符(例如Python shell)输入数据。 That data will then be sent through the same serial port to talk to the device that is also sending the data. 然后,该数据将通过相同的串行端口发送,以与同时发送数据的设备进行通讯。

My problem is that the plotting app.MainLoop() "Thread" seems to block and it wont show my raw_input portion until the window is closed. 我的问题是,绘图app.MainLoop()“ Thread”似乎阻塞了,直到关闭窗口,它才会显示我的raw_input部分。 I've also tried putting those 4 lines inside my while loop but the same problem occurs- it lets me input all my information once, but once plotting starts it blocks forever until I close the graphing window. 我还尝试将这4条线放入while循环中,但是会出现相同的问题-它使我一次输入了所有信息,但是一旦开始绘制,它将永远阻塞直到关闭图形窗口。

if __name__ == '__main__':
    app = wx.App()
    window = DataLoggerWindow()
    window.Show()
    app.MainLoop()
    prompt_counter = "main"
    while(1):
        if prompt_counter == "main":
            ans = raw_input("Press f for freq, press a for amplitude: \n")
            if ans == "f":
                prompt_counter = "freq"
            elif ans == "a":
                prompt_counter = "amp"
            else:
                prompt_counter = "main"
        elif prompt_counter == "freq":
                freq = raw_input("Enter the frequency you want to sample at in Hz: \n")
                ser.write("f"+freq+"\n")
                prompt_counter = "main"
        elif prompt_counter == "amp":
                amp = raw_input("Type in selection")    
                ser.write("a"+amp+"\n")
                prompt_counter = "main"

All the plotting portion does is read the serial port, and print the data received. 绘图部分所做的全部工作就是读取串行端口,并打印接收到的数据。 Both portions work separately with the device on the backend. 这两部分均与后端的设备分开工作。 So I'm pretty sure this is a problem with how I wrote the Python code but I'm not sure why....any ideas? 所以我很确定这是我编写Python代码时遇到的问题,但是我不确定为什么...。有什么想法吗?

Disclaimer: I don't think that the following is good practice. 免责声明:我不认为以下是好的做法。

You can put the execution of the wx stuff inside a separate thread. 您可以将wx东西的执行放在单独的线程中。

app = wx.App()
window = DataLoggerWindow()
import threading
class WindowThread(threading.Thread):
    def run(self):
        window.Show()
        app.MainLoop()
WindowThread().start()

That way, the MainLoop is only blocking another thread and the main thread should still be usable. 这样, MainLoop仅阻塞另一个线程,并且主线程仍应可用。

However, I think that this is not the optimal approach and you should rather use something like the App.OnIdle hook. 但是,我认为这不是最佳方法,您应该使用诸如App.OnIdle挂钩之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM