title: Qt QSerialPort Read Write
body: <p>I have issue with reading and writing from/to QSerialPort. I read what I have written. Here is some example code that proves that. Maybe I need to write and read from different channels. Code's provided below thx in advance. Provided log of the program with image. </p>
  
  <p>Log of the program here</p>
  
  <p><img src="https://i.stack.imgur.com/vB1Na.png" alt=""></p>
  
  <h2>MainWindow.h</h2>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  #include &lt;QSerialPort&gt;
  #include &lt;QTimer&gt;
  #include &lt;QDebug&gt;
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  private:
      QSerialPort *serialPort;
      QTimer* sendTimer;
      QByteArray receiveDataBuffer;
  
  private slots:
      void onReadyRead();
      void onSendDataTimeout();
  
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <h2>MainWindows.cpp</h2>
  
  <pre><code>#include "mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent),
        serialPort(new QSerialPort),
        sendTimer(new QTimer)
  {
      sendTimer-&gt;setInterval(1000);
      serialPort-&gt;setPortName("COM5");
      serialPort-&gt;setBaudRate(QSerialPort::Baud115200);
      serialPort-&gt;setParity(QSerialPort::Parity::NoParity);
      serialPort-&gt;setDataBits(QSerialPort::DataBits::Data8);
      serialPort-&gt;setStopBits(QSerialPort::StopBits::TwoStop);
      serialPort-&gt;open(QSerialPort::OpenModeFlag::ReadWrite);
  
      connect(serialPort,&amp;QSerialPort::readyRead,this,&amp;MainWindow::onReadyRead);
      connect(sendTimer,&amp;QTimer::timeout,this,&amp;MainWindow::onSendDataTimeout);
      sendTimer-&gt;start();
  }
  
  MainWindow::~MainWindow()
  {
  
  }
  
  void MainWindow::onReadyRead()
  {
      receiveDataBuffer.append(serialPort-&gt;readAll());
      if (receiveDataBuffer.contains('^') &amp;&amp; receiveDataBuffer.contains('$') &amp;&amp; receiveDataBuffer.lastIndexOf('$') &lt; receiveDataBuffer.lastIndexOf('^')) {
          QByteArray extractedByteArray;
  
          for(int i = receiveDataBuffer.lastIndexOf("$") ; i &lt; receiveDataBuffer.lastIndexOf("^") + 1 ; i++){
              extractedByteArray.append(receiveDataBuffer[i]);
          }
          qDebug()&lt;&lt;"Received:"&lt;&lt;extractedByteArray&lt;&lt;endl;
          receiveDataBuffer.clear();
      }}
  
  void MainWindow::onSendDataTimeout()
  {
      qint64 result = serialPort-&gt;write("$10,0,123,123^");
      bool flush = serialPort-&gt;flush();
      result != -1 &amp;&amp; flush ? qDebug()&lt;&lt;"Data sent"&lt;&lt;endl:qDebug()&lt;&lt;"Failed to send data"&lt;&lt;endl;
  }
  </code></pre>
  
  <h2>main.cpp</h2>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>