Results 1 to 4 of 4

Thread: while loop question..

  1. #1
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default while loop question..

    G'Day,

    I have the following code ( which works ) but during run time can potentially crash. It uses the qextserialport library.

    Qt Code:
    1. recflag = true;
    2. rec = serialPort->bytesAvailable();
    3. while ( recflag )
    4. {
    5. serialPort->read( retdata, 60 );
    6. if ( retdata[39] == 0x4F && retdata[40] == 0x04B )
    7. {
    8. sendwin->label_2->setText("Switch "+swnum+" succeeded" );
    9. recflag = false;
    10. }
    11. else
    12. {
    13. errorlist << "\nswitch "+swnum+" failed";
    14. sendwin->label_2->setText( "Switch "+swnum+" FAILED" );
    15. errorflag = true;
    16. recflag = false;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    If no data is seen on the serial port, the app just sits there until something comes in or I crash the app. My question is how can I implement a timer of, say 500ms, to break the loop. I have tried using a timer but they don't seem to trigger the slot.

    Any suggestions would be greatly appreciated.

    Thank, B1.

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    Quote Originally Posted by b1 View Post
    G'Day,
    If no data is seen on the serial port, the app just sits there until something comes in or I crash the app. My question is how can I implement a timer of, say 500ms, to break the loop. I have tried using a timer but they don't seem to trigger the slot.

    Any suggestions would be greatly appreciated.

    Thank, B1.
    simple .. u have the answer in your code ...
    declare the recFlag globally in your .h file
    Qt Code:
    1. privare:
    2. bool recFlag;
    3. QTimer *timer;
    To copy to clipboard, switch view to plain text mode 
    in .cpp
    Qt Code:
    1. recFlag = false;
    2. timer = new QTimer();
    3. timer->setSingleShot(true);
    4. connect(timer, SIGNAL(timeout()), this, SLOT(breakLoop()));
    5. timer->start(500);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MyWidger:: breakLoop(){
    2. recFlag = true;
    3. }
    To copy to clipboard, switch view to plain text mode 

    see the blocking fortune client example :
    http://doc.trolltech.com/4.5/network...hread-cpp.html

    they are using your method only ...

    i missed that ... dont forget to call your function again in breakLoop()


    while ( recflag )
    {
    serialPort->read( retdata, 60 );
    }
    Last edited by wysota; 25th May 2009 at 08:10.
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: while loop question..

    Thanks for the prompt reply, wagmare.

    I will follow your suggestion and will also look at the example code.

    Thanks again, b1.
    Last edited by b1; 25th May 2009 at 07:51. Reason: bad grammar!!

  4. #4
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: while loop question..

    Qt Code:
    1. serialPort->read( retdata, 60 );
    To copy to clipboard, switch view to plain text mode 
    Reads at most 60 bytes from the device into retdata, but retdata can be smaller then 60 byte.

    You should use the return value of the function so you can check if retdata[39] and retdata[40] exists before access to them.

  5. The following user says thank you to PaceyIV for this useful post:

    b1 (25th May 2009)

Similar Threads

  1. How to reuse a QImage in a While loop?
    By JoseTlaseca in forum Qt Programming
    Replies: 9
    Last Post: 5th June 2008, 07:19
  2. Background Question about QTcpSocket
    By sm3.142 in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 22:32
  3. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  5. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.