Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSerial - Can't read Arduino serial data
QtWS25 Last Chance

QSerial - Can't read Arduino serial data

Scheduled Pinned Locked Moved Solved Mobile and Embedded
qserialarduino
5 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LuizSabbagh
    wrote on 12 Nov 2019, 13:15 last edited by
    #1

    Hello!

    This is my first project using Qt and Arduino together. I'm trying to read and write data using a GUI and Arduino Uno, but I don't know what is happening.

    I want to click on a pushButton and receive the data that is on Serial. My code is not accessing readSerial and I believe that the problem is on the SIGNAL(readyRead()).

    void MainWindow::on_pushButton_clicked()
    {
        arduino_is_available = false;
        arduino_port_name = "";
        arduino = new QSerialPort;
    
        foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
            if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
                if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                    if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                        arduino_port_name = serialPortInfo.portName();
                        arduino_is_available = true;
                    }
                }
            }
        }
    
        if(arduino_is_available){
            // open and configure the serialport
            arduino->setPortName(arduino_port_name);
            arduino->open(QSerialPort::ReadWrite);
            arduino->setBaudRate(QSerialPort::Baud9600);
            arduino->setDataBits(QSerialPort::Data8);
            arduino->setParity(QSerialPort::NoParity);
            arduino->setStopBits(QSerialPort::OneStop);
            arduino->setFlowControl(QSerialPort::NoFlowControl);
            QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
    
        }else{
            // give error message if not available
            QMessageBox::warning(this, "Port error", "Couldn't find the Arduino!");
        }
    }
    
    void MainWindow::readSerial(){
        qDebug() << "Teste 1";
        QByteArray daraSerial = arduino->readAll();
        serialBuffer += QString::fromStdString(daraSerial.toStdString());
        //std::cout << serialBuffer.toStdString();
        qDebug() << "Teste 2";
        qDebug() << serialBuffer;
        qDebug() << daraSerial;
    }
    

    My Arduino code is just a loop with Serial.print("a"); and a delay.

    I have no idea how to solve this. I read some posts on the forum and didn't find something that solves my problem.

    What am I doing wrong?

    QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial())); is returning true.

    Thank you!

    J 1 Reply Last reply 12 Nov 2019, 13:27
    0
    • L LuizSabbagh
      12 Nov 2019, 13:15

      Hello!

      This is my first project using Qt and Arduino together. I'm trying to read and write data using a GUI and Arduino Uno, but I don't know what is happening.

      I want to click on a pushButton and receive the data that is on Serial. My code is not accessing readSerial and I believe that the problem is on the SIGNAL(readyRead()).

      void MainWindow::on_pushButton_clicked()
      {
          arduino_is_available = false;
          arduino_port_name = "";
          arduino = new QSerialPort;
      
          foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
              if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
                  if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                      if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                          arduino_port_name = serialPortInfo.portName();
                          arduino_is_available = true;
                      }
                  }
              }
          }
      
          if(arduino_is_available){
              // open and configure the serialport
              arduino->setPortName(arduino_port_name);
              arduino->open(QSerialPort::ReadWrite);
              arduino->setBaudRate(QSerialPort::Baud9600);
              arduino->setDataBits(QSerialPort::Data8);
              arduino->setParity(QSerialPort::NoParity);
              arduino->setStopBits(QSerialPort::OneStop);
              arduino->setFlowControl(QSerialPort::NoFlowControl);
              QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
      
          }else{
              // give error message if not available
              QMessageBox::warning(this, "Port error", "Couldn't find the Arduino!");
          }
      }
      
      void MainWindow::readSerial(){
          qDebug() << "Teste 1";
          QByteArray daraSerial = arduino->readAll();
          serialBuffer += QString::fromStdString(daraSerial.toStdString());
          //std::cout << serialBuffer.toStdString();
          qDebug() << "Teste 2";
          qDebug() << serialBuffer;
          qDebug() << daraSerial;
      }
      

      My Arduino code is just a loop with Serial.print("a"); and a delay.

      I have no idea how to solve this. I read some posts on the forum and didn't find something that solves my problem.

      What am I doing wrong?

      QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial())); is returning true.

      Thank you!

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 12 Nov 2019, 13:27 last edited by
      #2

      @LuizSabbagh
      what Qt version are you using ? if it's below 12.6 or 13.2 you'll have to update, as the Serialport module had a bug in some versions.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      L J 2 Replies Last reply 13 Nov 2019, 00:18
      3
      • L Offline
        L Offline
        LuizSabbagh
        wrote on 12 Nov 2019, 13:59 last edited by
        #3

        I'm using 5.12.5. I'll try to update.

        1 Reply Last reply
        0
        • J J.Hilk
          12 Nov 2019, 13:27

          @LuizSabbagh
          what Qt version are you using ? if it's below 12.6 or 13.2 you'll have to update, as the Serialport module had a bug in some versions.

          L Offline
          L Offline
          LuizSabbagh
          wrote on 13 Nov 2019, 00:18 last edited by
          #4

          @J-Hilk Fixed! Thank you!

          1 Reply Last reply
          1
          • J J.Hilk
            12 Nov 2019, 13:27

            @LuizSabbagh
            what Qt version are you using ? if it's below 12.6 or 13.2 you'll have to update, as the Serialport module had a bug in some versions.

            J Offline
            J Offline
            Jamshid
            wrote on 28 Jan 2020, 09:45 last edited by
            #5

            @J-Hilk Thanks man, your answer saved my time also,
            I had Qt 5.13.1 and serial read was not working, I updated to 5.14.0 and now it works.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved