Results 1 to 5 of 5

Thread: understanding threads / serial communication with thread

  1. #1
    Join Date
    Mar 2010
    Location
    Germany / South Africa
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default understanding threads / serial communication with thread

    Hello all,

    this is my first post in this forum, so please forgive me if there is something strange or against your rules.
    (You might also recognize that I am not a native speaker).

    I'm trying to learn Qt and practiced some tutorials. My first target is to build a serial-bus communication. For that I studied the Classes 'QextSerialPort' and 'qserialdevice' and decided finaly to try my luck with the 'QextSerialPort' in the 'one-class'-configuration. Finaly I want to communicate this program with my microcontrollers. (in the first step similarly like a terminal).

    In order to do this, I wanted to use a seperate thread to listen if there are any kind of data comming in. I had to leran, that it es useful to understand first how threads are working.
    Therefore I programmed a little Widget with a second Thread. Now my problems:

    When I start the thread from my widget, it starts successfully and works pretty fine. But I don't know how do quit this thread. In the Qt documentation the methods 'quit()' and 'exit(0)' are mentioned. But when I test this, the thread works on.
    A second Problem is that I need to communicate with the Widget where my QextSerialPort-Class is implemented. Unfortunately a communication between Widgets and Threads is impossible (Qt Help). So what is the best way to do it? Does it make sense to do the whole Serial-Communication in the thread or is there a different and better way to work with a thread?

    btw.: Qt 4.5 , linux (ubuntu 9.10), Qt Creator.

    This is the code of my little test-thread:
    Qt Code:
    1. void MyThread::run()
    2. {
    3. qDebug() << "In Thread";
    4. exec();
    5. }
    6.  
    7. void MyThread::testslot()
    8. {
    9. qDebug()<<"testslot opend";
    10. }
    11.  
    12. void MyThread::killslot()
    13. {
    14. qDebug()<<"Kill thread";
    15. exit(0);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your Help
    (if there is more code necessary, please let me know)

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: understanding threads / serial communication with thread

    QThread:run() : Returning from this method will end the execution of the thread

  3. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: understanding threads / serial communication with thread

    You can communicate with a widget (or several widgets) and a thread by using signal and slot.
    Just have a look at the Qt demo.

    The only thing you have to take into account is that the GUI thread (the one that is drawing yours widgets onto the screen) is independant.
    You can not ask, within a thread, to draw something into the screen.

  4. #4
    Join Date
    Mar 2010
    Location
    Germany / South Africa
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: understanding threads / serial communication with thread

    Quote Originally Posted by toutarrive View Post
    QThread:run() : Returning from this method will end the execution of the thread
    Hi, Thanks for your answer.

    A Returing (meaning 'return 0;' eg.) seems to be complicated under most circumstances.
    Just to make sure, that I am right: when I call the run-function, the thread starts to work and with the exec(); it stays running? So what about quit() or exit()?

  5. #5
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: understanding threads / serial communication with thread

    Create an instance of the thread object and call QThread::start().
    The code that appears in you run() reimplementation will then be executed in a separate thread.
    There is no need to call the run function directly

  6. The following user says thank you to toutarrive for this useful post:

    brott (7th March 2010)

Similar Threads

  1. serial communication
    By klitsuk in forum Qt Programming
    Replies: 10
    Last Post: 24th September 2009, 01:21
  2. serial communication
    By klitsuk in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 15:22
  3. serial communication
    By semden in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2009, 09:57
  4. C++ Library for serial communication
    By dec0ding in forum General Programming
    Replies: 7
    Last Post: 8th July 2007, 18:18
  5. serial port and USB communication
    By shamik in forum Qt Programming
    Replies: 5
    Last Post: 4th December 2006, 10:40

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.