Results 1 to 10 of 10

Thread: qextserialport waitForReadyRead and timeOut

  1. #1
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default qextserialport waitForReadyRead and timeOut

    Hello.

    I am implementing a kind of waitForReadyRead datas from the serial port, where if after 10 seconds without bytesAvailable the program continues running.

    This is the code:
    Qt Code:
    1. QTimer *timer2= new QTimer();
    2.  
    3. timer2->start(10000);
    4. while(port->bytesAvailable()<=0 && timer2->isActive());
    5.  
    6. if(port->bytesAvailable()<=0){
    7. //There arent bytesAvailable and the timer has expired ( timeOut)
    8.  
    9. }else{
    10. port->read();
    11. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that if it isnt bytesAvailable the program is all the time in an infinite loop, in the while loop, but it should stop the loop after 10 seconds.

    Maybe I am using the timer in a wrong way, do you think it is this?

    Cheers.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    Hi,

    Qt Code:
    1. while(port->bytesAvailable()<=0 || timer2->isActive());
    To copy to clipboard, switch view to plain text mode 

    You have to exit the bucle when there are bytes available OR the timer is active. When there are no bytes but the timer is stoped you have to exit the bucle.

    Note also that when exiting the bucle you have to stop the timer because maybe data has arrived and the timer is active and will be fired at time.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    I dont understand why do you say the while loop with AND
    Qt Code:
    1. while(port->bytesAvailable()<=0 && timer2->isActive());
    To copy to clipboard, switch view to plain text mode 
    is wrong? because I am waiting until receive datas in the port or the timer has expired? I mean:

    If there are bytes availabled, port->bytesAvailabe<=0 is false, so it goes out the loop.
    and if the there arent bytes but the timer has expired timer->isActive return false, so it is going out from the loop too.

  4. #4
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    I have another question in relation with this:

    I am testing the code, and I am running the code without sending any data to the port. And I dont know why but I am obtaining port->bytesAvailable > 0 and I dont have sent datas. But this is only when I have the conection done.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    Hi,

    Sorry, so when the timer is fired you have to stop it.
    Also you can set the timer to be "singleShot"

    On the second question, you can use a Serial Port monitor to obtain the data that is recived. Maybe you can then know what are you receiving. Mybe the reciver is sending some data when the connection is stablished.
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Thanks
    6
    Thanked 9 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    Hi,

    Just a sideways query to all following this thread (I am coding a Windows comport program and checking on things to include).

    Is the waitForReadyRead signal widely used ? (possibly because the readyRead signal in qextserialport is unreliable ?)

  7. #7
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    Hello.

    What I know about waitForReadyRead in serial port is that it is not working with windows, so for this reason i used the while loop.

    You can check this thread
    http://www.qtcentre.org/forum/f-qt-p...ait-18560.html

    Cheers.

  8. #8
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    I have another question about qextserialport. When I want to write only a character in the port can I use the write function like this?:

    char *a;
    a[0]='b';
    port->write(a);

    Or do i have to write a const char* and the size as parameters?

    And another thing, do i have to reset the port before use it? Because maybe there are bytes still written in the port.

  9. #9
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    And about to use a singleshot if the program is running in a while loop, it is never to go to the slot in the singleshot or i should to have another thread..

    or is there another way to go to the slot althoug it is running the while loop?

  10. #10
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qextserialport waitForReadyRead and timeOut

    Hi,

    I recommend you to write your code into a QThread so the while loop will not hang the main thread. In the constructor of the QThread you can create the Timer(or define it as a member variable) and start it on the "run" method.

    Maybe another way without using a timer is to use the thread to calculate the time at start and at every loop iteration and if 10 seconds elapsed you can exit.

    These are just someways that I think you can use to get it works as you expected.

    On the other question about how to write on the serial port. It's easier to use a QByteArray and write it directly. The serial port will take care of the size of the data to be sended.

    The SIGNAL "waitForReadyRead" works only on Windows using Alpha version of QextSerialPort and I don't know if it really works as expected.
    Òscar Llarch i Galán

Similar Threads

  1. QextSerialPort and waitForReadyRead problem
    By Ivan Wagner in forum Qt Programming
    Replies: 12
    Last Post: 20th August 2010, 08:52

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.