Results 1 to 14 of 14

Thread: QThread implementation for serial port monitoring

  1. #1
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default QThread implementation for serial port monitoring

    I am writing a program for control of a machine using serial port. At one point, I am required to run a process on another thread. For implementing the same I am following the tutorial given here.
    http://mayaposch.wordpress.com/2011/...l-explanation/


    My application is a GUI based application. The serial port and its functions are being handled by a separate class which has a port object (using qextserialport library). This port object is opened and closed at the start and ending of the application.
    Here, when I press one button on the FORM, I require to start a thread that will monitor the serial port for a particular input and execute a slot when the input is recieved from the serial port.
    Following is excerpts from the code written when the button is pressed.

    QThread*thread=newQThread;

    ports->moveToThread(thread);

    connect(thread,SIGNAL(started()),ports,SLOT(start()));

    connect(ports,SIGNAL(dataFound(QString)),this,SLOT(ondatafound(QString)));

    connect(ports,SIGNAL(finished()),thread,SLOT(quits()));


    connect(ports,SIGNAL(finished()),ports,SLOT(deleteLater()));

    connect(ports,SIGNAL(finished()),thread,SLOT(deleteLater()));

    thread->start();
    Here, the ports (Worker) class is the class which is controlling all the functions related to the serial port.
    Here, the start slot executes a Qtimer that reads the serial port after a timeout till the required data is not received. And after receiving data, emits the datafound signal and also emits the finished signal.



    This works for one time execution. However, when I press the Button again for re-executing which I am required to do again and again, after the ondatafound slot is executed, The QT gives an error and does not execute the movetothread.


    What I see in the tutorial is that a new instance of the worker class is created and movetothread is called and I am not doing it that way... but dont know the right way of doing it either for the thing to work.

    Secondly, I also need to have a provision of terminating this thread from the main thread in case I do not receive the data. So if i dont include the data from the serial port, then i need to push another button and continue the process further. I dont know how to terminate this thread from the main thread.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread implementation for serial port monitoring

    Please use the code tags instead of coloring your code text.
    The code tags will color the code for you and will make it possible for others to copy and paste the code in their posts when they answer you.

    the QT gives an error and does not execute the movetothread.
    What error do you get?

    I dont know how to terminate this thread from the main thread.
    Did you consult QThread documentation for that?
    Its exactly like starting the thread - with the exception, that you have to make sure in your thread, that it shuts down nicely and not leave stuff "half backed".
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs down Re: QThread implementation for serial port monitoring

    argh! time wasting cross forum poster
    http://www.qtforum.org/article/38210...threading.html
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    OK... thanks for your comments and kind consideration.

    I am attaching the testing code along with this post.
    Also, I am attaching the screenshot which shows two things... Right side bottom, the program window shows the message I recieve from the machine when I send the command for the first time. For sending the command, first I have to press the CONNECT button to open the port. Then I just go to the LineEdit and press ENTER key.. you can see the code.
    When I press the ENTER key again, I get the error which is seen in the screenshot at the center. This error is seen at line no. 64 which is movetothread line..

    I think with this data, it should be simpler to understand the problem.
    Screenshot1.jpg & thread-example.tar.gz

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    Still you havent shown any (useful) code... Your pic only shows seg fault, you don't mention any code line.

    Did you just attach a binary file? Err, no-one in their right mind is going to run that outside of a vm/sandbox.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread implementation for serial port monitoring

    I think with this data, it should be simpler to understand the problem.
    Its not.
    http://www.catb.org/~esr/faqs/smart-....html#symptoms
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    Sorry... the example program somehow got corrupted... trying to attach a zip file again so that you can download the example and see the complete code.

    thread-example.zip

    And Now I have verified that the zip file is getting downloaded properly and you can browse the contents.

    Thanks for your replies...

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread implementation for serial port monitoring

    However, when I press the Button again for re-executing which I am required to do again and again, after the ondatafound slot is executed, The QT gives an error and does not execute the movetothread.
    Show the slot connected to your button clicked() signal.
    No, I don't have the time to install your project and debug it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    that article is simply superb... thanks for that information. I am trying to assimilate and absorb the same..

    OK

    Here is the code

    The ports is defined public for this class as I need to use the same for other work related to the serial port.

    Qt Code:
    1. PortListener* ports = new PortListener;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::on_lineEdit_returnPressed()
    2. {
    3. QThread* thread = new QThread;
    4. ports->moveToThread(thread);
    5. connect(thread, SIGNAL(started()), ports, SLOT(start()));
    6. connect(ports, SIGNAL(dataFound(QString)),this, SLOT(ondatafound(QString)));
    7. connect(ports, SIGNAL(finished()), thread, SLOT(quits()));
    8. connect(ports, SIGNAL(finished()), ports, SLOT(deleteLater()));
    9. connect(ports, SIGNAL(finished()), thread, SLOT(deleteLater()));
    10. thread->start();
    11. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread implementation for serial port monitoring

    Based on the last snippet I can assume you have a member variable called "ports" but then you create a local variable (in first snippet of your last post) that shadows the member variable and you assign a new instance of PortListener to that, so your member variable remains invalid when you access it in line #4.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    kpkale (11th July 2012)

  12. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread implementation for serial port monitoring

    In addition to what wysota said:
    Its not clear how and when your thread gets destroyed.
    So it could be well that you have multiple threads running, all working on the same 'ports' object (in case you are not shadowing it the way wysota said) which can lead to undefined behavior when two treads are trying to do something with 'ports'.

    But why do you use threads and not a QTimer?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. The following user says thank you to high_flyer for this useful post:

    kpkale (11th July 2012)

  14. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    connect(ports, SIGNAL(finished()), ports, SLOT(deleteLater()));


    and then you try and use it later... of course it is deleted.


    By the way, using QTimer and PortListener as globals like you do is pretty poor practice.

    Quote Originally Posted by amleto View Post
    argh! time wasting cross forum poster
    http://www.qtforum.org/article/38210...threading.html
    I still believe this will solve your problem.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  15. The following user says thank you to amleto for this useful post:

    kpkale (11th July 2012)

  16. #13
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    SOLVED !!!

    Based on the comments by wysota, I could understand that the ports class was getting destroyed in the process of calling this thread. Hence it worked only once and then for the next time it gave me that error.
    Implemented a separate class which calls the respective slots from the ports object and executes it and it worked.

    Thanks all of you for your kind help.

  17. #14
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QThread implementation for serial port monitoring

    Quote Originally Posted by wysota View Post
    Based on the last snippet I can assume you have a member variable called "ports" but then you create a local variable (in first snippet of your last post) that shadows the member variable and you assign a new instance of PortListener to that, so your member variable remains invalid when you access it in line #4.
    ports is a global in that compilation unit
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 11:19
  2. serial port issues
    By jhowland in forum Qt Programming
    Replies: 7
    Last Post: 12th January 2009, 14:38
  3. serial port communiction
    By jagadish in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2007, 13:04
  4. serial port communiction
    By jagadish in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2007, 12:52
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 03:05

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.