title: Not able to write data to Arduino using qtserial
body: <p>I am fairly new to qt programming. I am not able to send/write data to Arduino.</p>
  
  <p>Reading data from Arduino works expected.</p>
  
  <p>I am trying to turn-on pin13 led on when <code>Serial.available() &gt; 0</code>. It works expected when I send data using Arduino serial monitor. 
  But I am not able to do the same using qt-serial. I do not get any error messages.</p>
  
  <p>what approach one should take to debug such issues?</p>
  
  <p><strong>serialCom.h</strong></p>
  
  <pre><code>#ifndef SERIALCOM_H
  #define SERIALCOM_H
  
  #include &lt;QObject&gt;
  #include &lt;QSerialPort&gt;
  
  class SerialCom: public QObject
  {
      Q_OBJECT
  public:
      SerialCom(QObject *parent = 0);
      ~SerialCom();
      void openSerialPort();
      void closeSerialPort();
      void writeData(const QByteArray &amp;data);    
  public slots:
      void handleError(QSerialPort::SerialPortError error);
      void readData();
  private:
      QSerialPort *serial;
  };
  
  #endif // SERIALCOM_H
  </code></pre>
  
  <p><strong>serialCom.cpp</strong></p>
  
  <pre><code>#include "serialcom.h"
  #include &lt;QDebug&gt;
  #include &lt;QSerialPortInfo&gt;
  
  SerialCom::SerialCom(QObject *parent):
      QObject(parent)
  {
      serial = new QSerialPort(this);
  
      connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
              SLOT(handleError(QSerialPort::SerialPortError)));
      connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
  }
  
  SerialCom::~SerialCom()
  {
  
  }
  
  void SerialCom::openSerialPort()
  {
      QSerialPortInfo portToUse;
      foreach (const QSerialPortInfo &amp;port, QSerialPortInfo::availablePorts()) {
  
          if(port.isNull() || !port.isValid())
          {
              qDebug() &lt;&lt; "port is not valid:" &lt;&lt; port.portName();
              return;
          }
          if(!port.isBusy() &amp;&amp; port.manufacturer().contains("Arduino")){
              portToUse = port;
              qDebug() &lt;&lt; tr("Port: %1 || manufacurer: %2").arg(port.portName()).arg(port.manufacturer());
          } else {
              qDebug() &lt;&lt; "either port is busy or its not arduino";
          }
      }//forEach
  
      serial-&gt;setPortName(portToUse.portName());
      serial-&gt;setBaudRate(QSerialPort::Baud9600);
      serial-&gt;setDataBits(QSerialPort::Data8);
      serial-&gt;setParity(QSerialPort::NoParity);
      serial-&gt;setStopBits(QSerialPort::OneStop);
      serial-&gt;setFlowControl(QSerialPort::NoFlowControl);
      if (serial-&gt;open(QIODevice::ReadWrite)) {
          qDebug() &lt;&lt; "Connected to" &lt;&lt; portToUse.description() &lt;&lt; "on" &lt;&lt; portToUse.portName();
      } else {
          qCritical() &lt;&lt; "Serial Port error:" &lt;&lt; serial-&gt;errorString();
  
          qDebug() &lt;&lt; tr("Open error");
      }
  }
  
  void SerialCom::writeData(const QByteArray &amp;data)
  {
      if(serial-&gt;isOpen() &amp;&amp; serial-&gt;isWritable()){
          serial-&gt;write(data);
          serial-&gt;flush();
          qDebug() &lt;&lt; "Writing: " &lt;&lt; data;
      }else{
          qDebug() &lt;&lt; "port not ready to write:" &lt;&lt; serial-&gt;isWritable();
      }
  }
  
  void SerialCom::readData()
  {
      QByteArray data = serial-&gt;readAll();
      qDebug() &lt;&lt; "UART:" &lt;&lt; data;
  }
  
  void SerialCom::handleError(QSerialPort::SerialPortError error)
  {
      qDebug() &lt;&lt; error;
  }
  
  void SerialCom::closeSerialPort()
  {
      serial-&gt;close();
      qDebug() &lt;&lt; tr("Disconnected");
  }
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;QCoreApplication&gt;
  #include "serialcom.h"
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
      SerialCom serialCom;
  
      //open port
      serialCom.openSerialPort();
  
  
      //make some stupid data to write to the serial port for testing
      const QByteArray stupid_data = "Hello World";
  
  
      //write @stupid_data to serial port
      serialCom.writeData(stupid_data);
  
      //closing port
  //    serialCom.closeSerialPort();
  
      return a.exec();
  }
  </code></pre>
  
  <p><strong>simpleSerialCom.pro</strong></p>
  
  <pre><code>QT += core serialport
  QT -= gui
  
  TARGET = simpleSerialCom
  CONFIG += console
  CONFIG -= app_bundle
  
  TEMPLATE = app
  
  SOURCES += main.cpp \
      serialcom.cpp
  
  HEADERS += \
      serialcom.h
  </code></pre>