Serial connections and threads in a GUI

A

Aaron Hoover

I have a wx-based GUI application that I'd like to have read
"streaming" data from a serial port and plot that data using
matplotlib and a figure canvas. It's a robotic application where the
workflow goes something like this:

1) Connect to robot
2) Configure robot settings
3) Command robot to do something for time, t
- While robot does something, read sensor data and plot
4) End of t seconds, resume at (2)

I have the application reading from the serial port in a separate
thread, but my problem is this - I'd like the user to be able to write
to the serial port by interacting with the GUI between runs. However,
the infinite loop running inside the thread responsible for receiving
data has exclusive access to the serial connection object.

Because of how the application works, I know that I won't need to
write to the serial port when I'm in the middle of receiving data, so
that simplifies things somewhat. But, how can I allow the thread
access to the connection for reading, but then have it give up control
over the connection when it's done so the main GUI thread can access
the connection again for writing?

Currently, I have the thread's run() method exit when it's done
receiving data. But, that means that I can only receive data again
using the same thread by calling run(), blocking the main GUI thread
from updating the plot until it completes.

This is my first go at using threads in Python, so I'm sure there's a
straightforward way to do this that I'm just missing. At any rate,
thanks in advance for your patience and assistance.


Thanks,
Aaron
 
S

sturlamolden

how can I allow the thread  
access to the connection for reading, but then have it give up control  
over the connection when it's done so the main GUI thread can access  
the connection again for writing?

Protect read/write access to the serial port with a lock.

Or use Candygram to pass Erlang-style messages to and from the thread
owning the serial port.
http://candygram.sourceforge.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top