Connect Arduino with Qt

gr3g picture gr3g · Jun 25, 2014 · Viewed 13k times · Source

I'm sending "R" char from Qt to Arduino
and waiting on that R, to make the LED blink on Arduino. But it doesn't work...

I think it might be a problem with waitFotBytesWritten or ASCI...
N.B: Any signal is good; don't need to be a char.

My Qt code:

QSerialPort serial;
        serial.setPortName("PORT_NAME");
        serial.open(QIODevice::ReadWrite);
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);

        if (serial.isOpen() && serial.isWritable())
        {
            qDebug() << "Ready..." << endl;
            serial.write("R");

            if(serial.flush())
                {
                    qDebug() << "ok" << endl;
                }
                qDebug() <<"value sent!!! "<< endl;
                serial.close();
        }

My Arduino code

int led = 13; 

void setup()   
{   
pinMode(led, OUTPUT);    
}

void loop() 
{

if (Serial.read() == 'R') //blink on 5sec  
{  
  digitalWrite(led, HIGH);   
  delay(5000);              
  digitalWrite(led, LOW);    
} 

else //Blink on and off 1 sec,1sec  
{  
   digitalWrite(led, HIGH);   
  delay(1000);              
  digitalWrite(led, LOW);   
  delay(1000);  
}

}

Qt output says:

 Manufacturer:  "Arduino (www.arduino.cc)"  
Busy:  false   
Ready...   
ok   
value sent!!!   

Any help would be appreciate


Here is complete code:

Qt:

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QString>

QT_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSerialPortInfo info("usbmodem621");
    // Check info of the port
            qDebug() << "Name        : " << info.portName();
            qDebug() << "Manufacturer: " << info.manufacturer(); //if showing manufacturer, means Qstring &name is good
            qDebug() << "Busy: " << info.isBusy() << endl;

            // Initialize Serial
            QSerialPort serial;
            serial.setPortName("usbmodem621");
            serial.open(QIODevice::ReadWrite);
            serial.setBaudRate(QSerialPort::Baud9600);
            serial.setDataBits(QSerialPort::Data8);
            serial.setParity(QSerialPort::NoParity);
            serial.setStopBits(QSerialPort::OneStop);
            serial.setFlowControl(QSerialPort::NoFlowControl);

            if (serial.isOpen() && serial.isWritable())
            {
                qDebug() << "Is open : " << serial.isOpen() << endl;
                qDebug() << "Is writable : " << serial.isWritable() << endl;

                QString string("R\n");
                QByteArray ba("R\n");
                serial.write(ba);
                if (serial.bytesToWrite() > 0)
                {
                    serial.flush();
                    if(serial.waitForBytesWritten(1000))
                    {
                        qDebug() << "data has been send" << endl;
                    }

                }
                if(serial.flush())
                    {
                        qDebug() << "ok" << endl;
                    }
                    qDebug() <<"value sent!!! "<< endl;
                    serial.close();
            }

            else
            {
                qDebug() << "An error occured" << endl;
            }
            return 0;
}

Arduino:

int led = 13; 

void setup() 
{ 
Serial.begin(9600);
pinMode(led, OUTPUT); 
}

void loop() 
{ 

  if (Serial.available() > 0)
  {
  digitalWrite(led, HIGH);  
  delay(5000);             

  if(Serial.read() == 'R')
  {
  digitalWrite(led, LOW);   
  delay(1000);  
  }  
  }

else
{
   digitalWrite(led, HIGH);  
  delay(1000);             
  digitalWrite(led, LOW);  
  delay(1000);  
}

}

Answer

gr3g picture gr3g · Jun 27, 2014

Problem has been solved:

Qt:

if (serial.isOpen() && serial.isWritable())
            {

                QByteArray ba("R");
                serial.write(ba);
                serial.flush();
                qDebug() << "data has been send" << endl;
                serial.close();
            }

Arduino:

int led = 13, avlb = 0; 

void setup() 
{ 
Serial.begin(9600);
pinMode(led, OUTPUT); 
Serial.println("started");
}

void loop() 
{ 
  if (Serial.available() > 0)
  {
    Serial.println("available");
    Serial.println(Serial.available());  
    delay(2000);    
  if(Serial.read())
  {
    Serial.println("read");
    Serial.println(Serial.read());
    delay(2000);
  }  
  }

else
{
  Serial.println("not available");
  delay(1000);
}

}

N.B.: Don't forget to put baudrate in Arduino IDE correctly;
In monitor, 9600 baudrate