Qt UI to Arduino Serial connection. Setup called multiple times
<p>I have noticed that <code>setup()</code> is executed again and again. I receive the same messages from <code>setup()</code>
Here's the code:</p>

<pre><code>enum flags{
  RFID=       1,
  LASER_ON=   2,
  LASER_OFF=  4,
  .
  .
  .
};

int inByte;
bool fcount = false;
unsigned int count=0;
long average_input=0;
bool cont = false;
int prev=-1;
/**
 * Initialize.
 */
void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
//    while (!Serial);// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
//    Serial1.begin(19200);
//    while (!Serial1);
//    SPI.begin();        // Init SPI bus
//    mfrc522.PCD_Init(); // Init MFRC522 card
    pinMode(laser, OUTPUT);
    pinMode(13, OUTPUT);
    // Prepare the key (used both as key A and as key B)
    // using FFFFFFFFFFFFh which is the default at chip delivery from the factory
//    for (byte i = 0; i &lt; 6; i++) {
//        key.keyByte[i] = 0xFF;
//    }

    Serial.println(F("Scan a MIFARE Classic PICC to demonstrate read and write."));
    Serial.print(F("Using key (for A and B):"));
//    dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
    Serial.println();

    Serial.println(F("BEWARE: Data will be written to the PICC, in sector #1"));

    digitalWrite(laser, HIGH);
    for(byte i =0; i&lt;10 ; i++){
        average_input += analogRead(photocell);
        delay(10);
    }
    Serial.print("total sum=\t");Serial.println(average_input);
    average_input /=10;
    Serial.print("average=\t");Serial.println(average_input);
    digitalWrite(laser, LOW);
}

/**
 * Main loop.
 */
void loop() {
    if(Serial.available()){
        delay(2000);
        Serial.println("Serial available");
        int input = 0;
        while (Serial.available()){
            input = input*10 + (Serial.read()-48);
            inByte = input;
        }
        Serial.println(inByte);
    }
//    if(Serial1.available()){
//      delay(100);
//      Serial.println("Serial1 available");
//      int input = 0;
//      while (Serial1.available()){
//            input = input*10 + (Serial1.read()-48);
//            inByte = input;
//      }
//      Serial.println(inByte);
//  }
    switch(inByte){
      case RFID:
          Serial.println("RFID CHOSEN");
//        get_rfid();     
          break;
      case LASER_ON:
          digitalWrite(laser, HIGH);
          fcount=true;
          break;
      case LASER_OFF:
          digitalWrite(laser, LOW);
          fcount=false;
          break;
      .
      .
      .
    }

    if (fcount){
      .
      .
      .
}
</code></pre>

<p>As you can see I commented out a lot, just trying to make <code>setup()</code> run only once. As the general goal: I'm trying to connect my Qt UI through <code>QSerialPort</code>. I've found a good solution on similar topic and adopted it. Here's the Qt code:</p>

<pre><code>.
.
.
QSerialPort serial;
    QStringList ports;
    for (QSerialPortInfo port : QSerialPortInfo::availablePorts()){
        qDebug() &lt;&lt; port.portName() &lt;&lt; port.vendorIdentifier() &lt;&lt; port.productIdentifier()
                         &lt;&lt; port.hasProductIdentifier() &lt;&lt; port.hasVendorIdentifier() &lt;&lt; port.isBusy();
                ports += port.portName();
    }

    serial.setPortName("cu.usbmodem1471");
    serial.open(QIODevice::ReadWrite);
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);
    qDebug()&lt;&lt;serial.isOpen()&lt;&lt;endl;
    qDebug()&lt;&lt;serial.isWritable()&lt;&lt;endl;
            if (serial.isOpen() &amp;&amp; serial.isWritable())
                        {

                            QByteArray ba("2");
                            serial.write(ba);
                            //serial.flush();
                            qDebug() &lt;&lt; "data has been send" &lt;&lt; endl;
                            serial.close();
                        }


.
.
.
</code></pre>

<p>First of all, it shows <code>false</code> <code>false</code> on both <code>isOpen()</code> and <code>isWritable()</code> when the serial monitor is opened. My guess is that Qt is trying to connect/send data to the same Serial as PC getting data on. When the Serial monitor is closed the flags are set to <code>true</code> <code>true</code> and message is sent. However it goes through the same setup process (aka calibrating laser) and not receiving 2 as a flag to turn on laser. Please advice me on Serial communication between PC and Arduino.</p>