Pyserial and pyQt

Discussion in 'Python' started by Seth, Jul 21, 2009.

  1. Seth

    Seth Guest

    I have used pyserial in the past but this is my first experience with
    pyQt. I am using the Python xy package for windows current but might
    move to linux. I have a small device that is outputting a basic text
    string. I want to be able to read this string(from the comm port) and
    update a text box and eventually a graph in pyQt. I can't find any
    documentation or tutorials on how to do this. If anyone can point me
    in the right direction or give me some tips I would be grateful.

    Seth, Jul 21, 2009
    1. Advertisements

  2. Seth

    David Boddie Guest

    It seems that someone has already asked a similar question on Stack
    Overflow, though perhaps you should start with a simpler solution
    and look at more advanced ones later:

    One starting point is this list of tutorials on the PyQt and PyKDE Wiki:

    Later, when you want to draw graphs, you might find PyQwt useful:

    You may already be aware that there's also a mailing list for PyQt and

    Another way to get answers to questions is to join the #pyqt IRC channel


    David Boddie, Jul 22, 2009
    1. Advertisements

  3. Seth

    Seth Guest

    Thanks for the response. I have gone through a lot of the tutorials.
    All of them(that I saw) seem to just deal will event-based
    applications ie calculator, image viewer, etc. How do I run pyserial
    in the background and pass the information to PyQT and refresh the
    screen? Is there a way to have pyserial run in another thread and
    pass the information to the UI?

    Seth, Jul 22, 2009
  4. So far as I've experienced (which isn't all that much), most all GUI
    frameworks support some method of defining either: 1) a work routine
    which will be called whenever the input event queue is empty -- work
    routines should be coded to do a very short, fast, bit of processing and
    return, so the event dispatcher can continue (collect one character at a
    time, or one line using a non-blocking I/O operation; or use a thread to
    collect and post via a Queue) (wx.EVT_IDLE [bind to your idle handler] &
    wx.WakeUpIdle() [call from thread after posting to queue]); 2) a
    timed-event which the dispatcher can call at periodic intervals (use a
    thread to collect lines from the serial port, post the lines via a
    Queue, and have the timed event check for queued data) (wx.Timer() ); 3)
    a callback interface to the GUI event dispatcher which can be invoked
    from a separate thread (wx.CallAfter() )

    You'll have to track down the equivalent QT functions (I have to
    confess, I found the above from "wxPython in Action"; my last real GUI
    coding that needed such was nearly 20 years ago, using xt and DECWindows
    for the interface, and GKS on X for the data drawing, emulating an
    archaic Ramtek graphics engine command set. The idle/work routine
    collected commands sent via VMS mailbox to the "display" and generated
    GKS operations for them).
    Wulfraed Dennis Lee Bieber KD6MOG

    (Bestiaria Support Staff: )
    Dennis Lee Bieber, Jul 23, 2009
  5. Seth

    David Boddie Guest

    I think I can say that idle processing isn't a common idiom in PyQt
    applications. After all, you can't guarantee that your application will be
    idle or be able to use up idle time in a way that you can rely on, and
    perhaps it isn't a good situation to be in if your framework is using up
    CPU time by idling.
    In PyQt, the easiest way to do this is with a timer, connecting its
    timeout() signal to a slot (method) to perform some processing. A widget
    class might be adapted to do this in the following way:

    class MyWindow(QWidget):
    def __init__(self):
    # ... other initialisation ...

    timer = QTimer(self)
    timer.timeout.connect(self.processData) # PyQt 4.5 signal notation
    timer.start(100) # every 100 milliseconds

    def processData(self):
    # Do your processing here.

    There are various ways to use timers with PyQt, but this is a fairly simple
    pattern to use.
    I think at this point I would recommend using a separate thread. The idiom
    for using threads with PyQt is quite simple, though it helps to go with the
    framework and use QThread instead of Python's thread class.
    The people demand screenshots! ;-)

    David Boddie, Jul 23, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.