Results 1 to 9 of 9

Thread: [Python] Read from a serial connection every few seconds and update a wxPython window

  1. #1
    Join Date
    Nov 2007
    Beans
    25
    Distro
    Ubuntu 11.04 Natty Narwhal

    [Python] Read from a serial connection every few seconds and update a wxPython window

    I have a python program that reads a set chunk of data from a serial connection, cleans it up, and displays in a gui window.

    What is the best way to get the program to read from the connection every 10 seconds or so and update the window? I want this to be a status window that stays on all the time.

    The program seems to be working great right now but it only reads once. I would be happy to provide any code or further information if it would help.

    thanks.

  2. #2
    Join Date
    Nov 2007
    Beans
    25
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    Still trying to work this out. I've been playing with while loops and the module reload but it's not working yet.

    Whenever I have a while loop in a wxPython gui window I can't close the window by clicking the close "X". Is there an exception I can add to handle this?

    I'm hoping a simple loop can be the answer because all the reading I've done seems to be pointing to Idle events and threads which are WAY beyond me right now.

  3. #3
    Join Date
    Oct 2005
    Location
    Finland
    Beans
    14
    Distro
    Kubuntu 6.06

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    You have to set up a WxPython Timer that triggers every 10 seconds. Instructions here: http://wiki.wxpython.org/Timer. Note that the time value is in milliseconds.
    Last edited by finnjimm; December 13th, 2008 at 07:24 PM.

  4. #4
    Join Date
    Nov 2007
    Beans
    25
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    Thanks for the tip. I'm going to see if I can figure that out.

  5. #5
    Join Date
    Feb 2005
    Location
    Turkey
    Beans
    178
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    And starting a new Python thread with a timer which controls the serial port every ten seconds might be another solution. This might help about threeads in Python:

    http://www.devshed.com/c/a/Python/Ba...ding-in-Python

  6. #6
    Join Date
    Dec 2008
    Beans
    5

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    seems like this may be an application for threads, and consider a blocking approach to reading the serial port (pyserial?). That way you aren't burning CPU time if you are just reading every few seconds non-blocking.

    I am not very familiar with wxPython but I have used pyQT and tkinter. Typically I would set up a "listener" thread that would handle the serial buffer do some sort of callback to update the GUI widget display. You don't want this running in your main thread (unless reading a serial port is the only thing your app does).

  7. #7
    Join Date
    Nov 2007
    Beans
    25
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    Thank you for the suggestions.

    So could I have a thread that reads 1000 bytes from the serial connection every 10 seconds and then returns a string to be displayed in a window?

    Once I start a thread does it keep running until I stop it or does it run just once?

    Thanks for bearing with me. I'm trying to figure all this stuff out as fast as I can.

  8. #8
    Join Date
    Jan 2007
    Location
    Atlanta, GA
    Beans
    Hidden!
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    Quote Originally Posted by buttertoad View Post
    Thank you for the suggestions.

    So could I have a thread that reads 1000 bytes from the serial connection every 10 seconds and then returns a string to be displayed in a window?

    Once I start a thread does it keep running until I stop it or does it run just once?

    Thanks for bearing with me. I'm trying to figure all this stuff out as fast as I can.

    the wx timer will continuously run unless you set the oneShot flag...i think you want to keep it running so just point the timer to your function and call the timer start in your wx class __init__

    you can also run multiple timers simultaneously if you have other things that need to be updated. off the top of my head it would look something like this:

    Code:
    ID_TIMER = 100
    
    class MyFrame(wx.Frame):
      def __init__(self,*args,*kwargs):
        wx.Frame.__init__(self,None,-1,'Report Serial')
        self.timer=wx.Timer(self, ID_TIMER)
        self.Bind(wx.EVT_TIMER, self.ReadSerial, self.timer)
        self.timer.Start(10000)
        #more wx stuff
     
      def ReadSerial(self,event):
        #code to read the serial port and update statictext

    this should loop the ReadSerial every 10 seconds indefinitely or until you stop the program.
    Last edited by iQuizzle; December 17th, 2008 at 06:36 AM.

  9. #9
    Join Date
    Feb 2005
    Location
    Turkey
    Beans
    178
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Python] Read from a serial connection every few seconds and update a wxPython wi

    You start a thread and then go into an infinite loop. Like this:

    Code:
    while "1" == "1":
        control the serial port
        time.sleep(10)
    Because it is in a different thread, the infinite loop will not effect the main program.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •