Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort does not wait for data
Forum Updated to NodeBB v4.3 + New Features

QSerialPort does not wait for data

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 8.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    ulesQt
    wrote on last edited by
    #1

    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;
    }@

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • U Offline
        U Offline
        ulesQt
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dcbasso
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dcbasso
            wrote on last edited by
            #5
            • I'm using other API, but it's possible that Signals and Slots solve your problem!

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

            1 Reply Last reply
            0
            • U Offline
              U Offline
              ulesQt
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dcbasso
                wrote on last edited by
                #7

                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...

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on last edited by
                  #8

                  @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

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dcbasso
                    wrote on last edited by
                    #9

                    Kuzulis very nive informations!
                    Thanks!

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved