QSerialPort does not wait for data



  • Hello!
    I'm using QSerialPort wich I've got from "here...":http://qt-project.org/wiki/QtSerialPort
    I'm successfully sending data to another PC, but after sending I want my code to wait for a response for further actions.
    Somewhere I found this code snippet but waitForReadyRead returns immediate with false result.
    What is that I'm missing? Where could I find more information about the library?.
    Thanks!!!

    @
    serial->write(outMsg);
    //wait for response
    forever{
    QByteArray inputMsg = serial->readAll();
    numRead = inputMsg.length();

    // Do whatever with the array

    numReadTotal += numRead;
    if (numRead == 0 && !serial->waitForReadyRead(mTimeOut*1000))
    break;
    }@



  • hmm, numRead isn't zero, so why should the compiler run the waitForReadyRead function? It will not be called at all. if you change the two arguments, the compiler might call the waitForReadyRead signal, but do not count on it!
    greetz



  • Tnxs!
    But numRead is zero and waitForReadyRead is actually executed but returns false



  • UlesQt, use the connect()! (See here: http://qt-project.org/doc/qt-4.8/signalsandslots.html)

    @
    connect(SingletonSession::getComunicaoSerial(), SIGNAL(signalResposta(Resposta*)), this, SLOT(respostaDetectarSensores(Resposta*)));
    SingletonSession::getComunicaoSerial()->enviarDados(protocolo);
    @

    I'm using on my project and works very nice! I used some other classes to manupilate data for me, but it's very simple to implement this!
    This signals and slots is very useful, and powerful resource, for a lot of stuff on application!



    • I'm using other API, but it's possible that Signals and Slots solve your problem!

    http://code.google.com/p/qextserialport/



  • Tnxs! That is other approach I'm using, but this time I wanted the code to be blocking, so the function won't return until receiving a valid response message.
    could I do that with qextserialport?



  • I not really sure about!
    I used on my project like I show you, and before I use the data received I use some methods to validate and transform the data received from USB/Serial.
    Works very well here, on linux and Windows!

    I think that use a loop for wait some data is worse, and can consume a lot of cpu time!
    The Qt has this very powerful feature (signals and slots), I recommend to use them! Easy to use, easy to maintain, lightweight resource...



  • @ulesQt

    bq. What is that I’m missing?

    About QtSerialPort:

    Use blocking I / O is necessary only in a separate thread.

    Also have some features using the methods read() / write(). The fact is that in blocking mode, these functions do not work correctly by themselves, because do not work event-loop.

    For their proper functioning is necessary to use methods such as waitForXXX (), that handle events that I / O.

    Should be the following guidelines on blocking I / O:

    1. After call write() need call waitForBytesWritten()
    2. Before call read() need call waitForReadyRead()

    At the moment, blocking I / O does not work in QtSerialPort on Windows, but the solution is available, but not yet merged to master branch.

    Please use this patch:

    https://codereview.qt-project.org/#change,39635

    Also exists examples of the use of blocking I / O:

    https://codereview.qt-project.org/#change,39343



  • Kuzulis very nive informations!
    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.