title: Qt serial communication timeout at waitforreadyread
body: <p>I try to get serial communication working, but struggle at waiting for the response. The logic should be to send data, wait for a response, get the response and then repeat.</p>
  
  <p>To speed up my code and preventing it from blocking other independent parts of the code I run the serial communication in a separate thread.</p>
  
  <p>The problem is in the send function, where I always get a "Timeout" message instead of an "Success" message.</p>
  
  <p>serial.h</p>
  
  <pre><code>#ifndef SERIAL_H
  #define SERIAL_H
  
  #include &lt;QThread&gt;
  #include &lt;QDebug&gt;
  #include &lt;QSerialPort&gt;
  #include &lt;QSerialPortInfo&gt;
  #include &lt;QStringList&gt;
  
  class Serial : public QThread
  {
      Q_OBJECT
  public:
      Serial();
      bool send(QString text);  
      bool connectSerial();
      void disconnectSerial();
  
  signals:
      void dataReceived(QByteArray receivedData);
  
  protected:
      void run();
  
  private slots:
      void onGetData();
  
  private:
      void readAngles();
  
      QSerialPort qsp;
      QSerialPortInfo qspi;
  
      QByteArray receivedData;
  };
  
  #endif // SERIAL_H
  </code></pre>
  
  <p>serial.cpp</p>
  
  <pre><code>#include "serial.h"
  
  Serial::Serial()
  {    
      connect(&amp;qsp, SIGNAL(readyRead()), this, SLOT(onGetData()));
  }
  
  bool Serial::send(QString text)
  {
      if (qsp.isWritable()) {
          QByteArray buffer = text.toLatin1();
  
          if (buffer.size() != qsp.write(buffer))
              qDebug() &lt;&lt; "Send does not work";
  
          qsp.flush();
          if(!qsp.waitForReadyRead(2500)) {
              qDebug() &lt;&lt; "Timeout";
          } else {
              qDebug() &lt;&lt; "Success";
          }
  
          return true;
      } else {
          return false;
      }
  }
  
  void Serial::onGetData()
  {
      qDebug() &lt;&lt; "onGetData called";
      qDebug() &lt;&lt; qsp.readAll();
  }
  
  bool Serial::connectSerial(int port)
  {
      qDebug() &lt;&lt; qspi.availablePorts().count();
      for(int i=0; i&lt;qspi.availablePorts().count(); i++) {
          qDebug() &lt;&lt; qspi.availablePorts().at(i).description();
      }
  
      qsp.setPort(qspi.availablePorts().at(port));
      qsp.setBaudRate(QSerialPort::Baud9600);
      qsp.setDataBits(QSerialPort::Data7);
      qsp.setFlowControl(QSerialPort::NoFlowControl);
      qsp.setParity(QSerialPort::NoParity);
      qsp.setStopBits(QSerialPort::OneStop);
  
      return qsp.open(QIODevice::ReadWrite);
  }
  
  /**
   * @brief Disconnecting from serial port.
   */
  void Serial::disconnectSerial()
  {
      if (qsp.isOpen()) {
          qsp.close();
      }
  }
  
  void Serial::readAngles()
  {
      send("012345");
  }
  </code></pre>