Serial connections and threads in a GUI

Discussion in 'Python' started by Aaron Hoover, Sep 29, 2009.

  1. Aaron Hoover

    Aaron Hoover Guest

    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
     
    Aaron Hoover, Sep 29, 2009
    #1
    1. Advertising

  2. Aaron Hoover

    sturlamolden Guest

    On 29 Sep, 23:56, Aaron Hoover <> wrote:
    > 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/
     
    sturlamolden, Sep 30, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Silke

    serial and threads

    Silke, Aug 17, 2004, in forum: Python
    Replies:
    1
    Views:
    297
    Peter Hansen
    Aug 17, 2004
  2. Silke

    serial and threads

    Silke, Aug 17, 2004, in forum: Python
    Replies:
    5
    Views:
    520
    Silke
    Aug 18, 2004
  3. jobs
    Replies:
    2
    Views:
    915
  4. Max Kotasek
    Replies:
    4
    Views:
    1,030
    Max Kotasek
    Apr 9, 2010
  5. Chris Finch
    Replies:
    3
    Views:
    324
    Harry
    Sep 1, 2006
Loading...

Share This Page