I am asking for some high level advice here. I am using Python to plot data that is received constantly through serial. At the same time, I want the user to be able to input data through a prompt (such as the 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. 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.
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?
Disclaimer: I don't think that the following is good practice.
You can put the execution of the wx stuff inside a separate thread.
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.
However, I think that this is not the optimal approach and you should rather use something like the App.OnIdle hook.