Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT reading Serialport of Arduino
Forum Update on Monday, May 27th 2025

QT reading Serialport of Arduino

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 3.7k 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.
  • M Offline
    M Offline
    mimamrafi
    wrote on last edited by mimamrafi
    #1

    Hi. all. i'm stuck in reading some string from arduino. The Serial Port didn't open automatically when I debug first time in QT. But when I open the serial monitor from Arduino IDE, then I close it, then I debug again in QT, then the arduino begin to send its string. But when I upload the code to arduino again, then I debug the QT, it didn't sent anything. It seems my arduino didn't open automatically of its serial port. How can I debug the QT without open the serial monitor from arduino ide in the first?

    This is my simple code

    //================ Arduino Code ==============
    void setup()
    {
        Serial.begin(9600);
        pinMode (led, OUTPUT);
    }  
    
    void loop()
    {
        Serial.println ("test");
    }
    

    and this is QT Code

    SerialPortManager::SerialPortManager(QObject *parent) :
        QObject(parent)
    {
       arduino = new QSerialPort(this);
       connect(arduino, SIGNAL(readyRead()), this, SLOT(readData()));
    }
    
    void SerialPortManager::openSerialPort()
    {
       arduino->setPortName("COM8");
       arduino->open(QIODevice::ReadWrite);
       arduino->setBaudRate(QSerialPort::Baud9600);
       arduino->setDataBits(QSerialPort::Data8);
       arduino->setParity(QSerialPort::NoParity);
       arduino->setStopBits(QSerialPort::OneStop);
       arduino->setFlowControl(QSerialPort::NoFlowControl);
    }
    
    void SerialPortManager::readData()
    {
        QByteArray data = arduino->readAll();
    //    console->putData(data);
        qDebug() << "UART:" << data;
    }
    
    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • M Offline
      M Offline
      mimamrafi
      wrote on last edited by
      #17

      Hai everyone. I found some clues about this topic. The serial communication have to enable the DTR (Data Terminal Ready). So this is my current code that I used

      void SerialPortManager::openSerialPort()
      {
         arduino->setPortName("COM8");
         arduino->open(QIODevice::ReadWrite);
         arduino->setBaudRate(QSerialPort::Baud9600);
         arduino->setDataBits(QSerialPort::Data8);
         arduino->setParity(QSerialPort::NoParity);
         arduino->setStopBits(QSerialPort::OneStop);
         arduino->setFlowControl(QSerialPort::NoFlowControl);
         arduino->setDataTerminalReady(true);
      }
      

      Actually, I don't know why its working, but some of tutorial it's not a problem without writing setDataTerminalReady

      1 Reply Last reply
      3
      • M mimamrafi

        Hi. all. i'm stuck in reading some string from arduino. The Serial Port didn't open automatically when I debug first time in QT. But when I open the serial monitor from Arduino IDE, then I close it, then I debug again in QT, then the arduino begin to send its string. But when I upload the code to arduino again, then I debug the QT, it didn't sent anything. It seems my arduino didn't open automatically of its serial port. How can I debug the QT without open the serial monitor from arduino ide in the first?

        This is my simple code

        //================ Arduino Code ==============
        void setup()
        {
            Serial.begin(9600);
            pinMode (led, OUTPUT);
        }  
        
        void loop()
        {
            Serial.println ("test");
        }
        

        and this is QT Code

        SerialPortManager::SerialPortManager(QObject *parent) :
            QObject(parent)
        {
           arduino = new QSerialPort(this);
           connect(arduino, SIGNAL(readyRead()), this, SLOT(readData()));
        }
        
        void SerialPortManager::openSerialPort()
        {
           arduino->setPortName("COM8");
           arduino->open(QIODevice::ReadWrite);
           arduino->setBaudRate(QSerialPort::Baud9600);
           arduino->setDataBits(QSerialPort::Data8);
           arduino->setParity(QSerialPort::NoParity);
           arduino->setStopBits(QSerialPort::OneStop);
           arduino->setFlowControl(QSerialPort::NoFlowControl);
        }
        
        void SerialPortManager::readData()
        {
            QByteArray data = arduino->readAll();
        //    console->putData(data);
            qDebug() << "UART:" << data;
        }
        
        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @mimamrafi said in QT reading Serialport of Arduino:

        The Serial Port didn't open automatically

        What do you mean? Do you mean arduino->open(QIODevice::ReadWrite); returns false (you should check the return value!)?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • M mimamrafi

          Hi. all. i'm stuck in reading some string from arduino. The Serial Port didn't open automatically when I debug first time in QT. But when I open the serial monitor from Arduino IDE, then I close it, then I debug again in QT, then the arduino begin to send its string. But when I upload the code to arduino again, then I debug the QT, it didn't sent anything. It seems my arduino didn't open automatically of its serial port. How can I debug the QT without open the serial monitor from arduino ide in the first?

          This is my simple code

          //================ Arduino Code ==============
          void setup()
          {
              Serial.begin(9600);
              pinMode (led, OUTPUT);
          }  
          
          void loop()
          {
              Serial.println ("test");
          }
          

          and this is QT Code

          SerialPortManager::SerialPortManager(QObject *parent) :
              QObject(parent)
          {
             arduino = new QSerialPort(this);
             connect(arduino, SIGNAL(readyRead()), this, SLOT(readData()));
          }
          
          void SerialPortManager::openSerialPort()
          {
             arduino->setPortName("COM8");
             arduino->open(QIODevice::ReadWrite);
             arduino->setBaudRate(QSerialPort::Baud9600);
             arduino->setDataBits(QSerialPort::Data8);
             arduino->setParity(QSerialPort::NoParity);
             arduino->setStopBits(QSerialPort::OneStop);
             arduino->setFlowControl(QSerialPort::NoFlowControl);
          }
          
          void SerialPortManager::readData()
          {
              QByteArray data = arduino->readAll();
          //    console->putData(data);
              qDebug() << "UART:" << data;
          }
          
          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          @mimamrafi said in QT reading Serialport of Arduino:

          void SerialPortManager::openSerialPort()
          {
          arduino->setPortName("COM8");
          arduino->open(QIODevice::ReadWrite);
          arduino->setBaudRate(QSerialPort::Baud9600);
          arduino->setDataBits(QSerialPort::Data8);
          arduino->setParity(QSerialPort::NoParity);
          arduino->setStopBits(QSerialPort::OneStop);
          arduino->setFlowControl(QSerialPort::NoFlowControl);
          }

          you have to open the serial port after setting the parameters.


          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.

          M 1 Reply Last reply
          1
          • jsulmJ jsulm

            @mimamrafi said in QT reading Serialport of Arduino:

            The Serial Port didn't open automatically

            What do you mean? Do you mean arduino->open(QIODevice::ReadWrite); returns false (you should check the return value!)?

            M Offline
            M Offline
            mimamrafi
            wrote on last edited by
            #4

            @jsulm yes, it return false. but after I open the serial monitor in Arduino IDE, then I close the serial monitor in arduino ide, then I debug the qt. now it return true. How can I debug the QT without open the serial monitor in arduino ide in the first time?

            jsulmJ 1 Reply Last reply
            0
            • M mimamrafi

              @jsulm yes, it return false. but after I open the serial monitor in Arduino IDE, then I close the serial monitor in arduino ide, then I debug the qt. now it return true. How can I debug the QT without open the serial monitor in arduino ide in the first time?

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @mimamrafi Please read what @J-Hilk wrote

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @mimamrafi said in QT reading Serialport of Arduino:

                void SerialPortManager::openSerialPort()
                {
                arduino->setPortName("COM8");
                arduino->open(QIODevice::ReadWrite);
                arduino->setBaudRate(QSerialPort::Baud9600);
                arduino->setDataBits(QSerialPort::Data8);
                arduino->setParity(QSerialPort::NoParity);
                arduino->setStopBits(QSerialPort::OneStop);
                arduino->setFlowControl(QSerialPort::NoFlowControl);
                }

                you have to open the serial port after setting the parameters.

                M Offline
                M Offline
                mimamrafi
                wrote on last edited by
                #6

                @J-Hilk it is the command to open the serial port right?

                arduino->open(QIODevice::ReadWrite);
                
                jsulmJ 1 Reply Last reply
                0
                • M mimamrafi

                  @J-Hilk it is the command to open the serial port right?

                  arduino->open(QIODevice::ReadWrite);
                  
                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @mimamrafi said in QT reading Serialport of Arduino:

                  it is the command to open the serial port right?

                  Yes, it should be AFTER you set the parameters (like setFlowControl).

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M K 2 Replies Last reply
                  0
                  • jsulmJ jsulm

                    @mimamrafi said in QT reading Serialport of Arduino:

                    it is the command to open the serial port right?

                    Yes, it should be AFTER you set the parameters (like setFlowControl).

                    M Offline
                    M Offline
                    mimamrafi
                    wrote on last edited by mimamrafi
                    #8

                    @jsulm yes, I have been open it and the output is true, but my issue is the arduino send nothing to serial when I debug the QT first. If I open the serial monitor and close it again then debug the QT, it works properly

                    jsulmJ J.HilkJ 2 Replies Last reply
                    0
                    • M mimamrafi

                      @jsulm yes, I have been open it and the output is true, but my issue is the arduino send nothing to serial when I debug the QT first. If I open the serial monitor and close it again then debug the QT, it works properly

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @mimamrafi I don't know how your Arduino side is working, so can't comment

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      M 1 Reply Last reply
                      0
                      • M mimamrafi

                        @jsulm yes, I have been open it and the output is true, but my issue is the arduino send nothing to serial when I debug the QT first. If I open the serial monitor and close it again then debug the QT, it works properly

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #10

                        @mimamrafi said in QT reading Serialport of Arduino:

                        yes, I have been open it and the output is true

                        yes, thats obvious, but did you open it after setting the settings ? e.g:

                        void SerialPortManager::openSerialPort()
                        {
                        arduino->setPortName("COM8");
                        arduino->setBaudRate(QSerialPort::Baud9600);
                        arduino->setDataBits(QSerialPort::Data8);
                        arduino->setParity(QSerialPort::NoParity);
                        arduino->setStopBits(QSerialPort::OneStop);
                        arduino->setFlowControl(QSerialPort::NoFlowControl);
                        
                        arduino->open(QIODevice::ReadWrite);
                        }
                        

                        assuming of course, those are the same settings as the other program uses.


                        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.

                        M 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @mimamrafi I don't know how your Arduino side is working, so can't comment

                          M Offline
                          M Offline
                          mimamrafi
                          wrote on last edited by
                          #11

                          @jsulm I only send

                          Serial.println ("test"); 
                          

                          in void loop

                          1 Reply Last reply
                          0
                          • J.HilkJ J.Hilk

                            @mimamrafi said in QT reading Serialport of Arduino:

                            yes, I have been open it and the output is true

                            yes, thats obvious, but did you open it after setting the settings ? e.g:

                            void SerialPortManager::openSerialPort()
                            {
                            arduino->setPortName("COM8");
                            arduino->setBaudRate(QSerialPort::Baud9600);
                            arduino->setDataBits(QSerialPort::Data8);
                            arduino->setParity(QSerialPort::NoParity);
                            arduino->setStopBits(QSerialPort::OneStop);
                            arduino->setFlowControl(QSerialPort::NoFlowControl);
                            
                            arduino->open(QIODevice::ReadWrite);
                            }
                            

                            assuming of course, those are the same settings as the other program uses.

                            M Offline
                            M Offline
                            mimamrafi
                            wrote on last edited by
                            #12

                            @J-Hilk Hey, I've got a clue. I'm using Arduino leonardo, I don't know why it didn't send the serial to QT without open the serial monitor. When I try Arduino UNO, it works properly without open the serial monitor first

                            J.HilkJ 1 Reply Last reply
                            0
                            • M mimamrafi

                              @J-Hilk Hey, I've got a clue. I'm using Arduino leonardo, I don't know why it didn't send the serial to QT without open the serial monitor. When I try Arduino UNO, it works properly without open the serial monitor first

                              J.HilkJ Online
                              J.HilkJ Online
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #13

                              @mimamrafi try adding a sleep/delay at the end of your Arduino loop, see if that improves things.


                              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.

                              M 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @mimamrafi said in QT reading Serialport of Arduino:

                                it is the command to open the serial port right?

                                Yes, it should be AFTER you set the parameters (like setFlowControl).

                                K Offline
                                K Offline
                                kuzulis
                                Qt Champions 2020
                                wrote on last edited by
                                #14

                                @jsulm said in QT reading Serialport of Arduino:

                                Yes, it should be AFTER you set the parameters (like setFlowControl).

                                No. It is irrelevant.

                                J.HilkJ 1 Reply Last reply
                                0
                                • K kuzulis

                                  @jsulm said in QT reading Serialport of Arduino:

                                  Yes, it should be AFTER you set the parameters (like setFlowControl).

                                  No. It is irrelevant.

                                  J.HilkJ Online
                                  J.HilkJ Online
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #15

                                  @kuzulis I was about to vehemently deny this, but then I noticed the setters have boolean return type.

                                  I was only going on the notes from https://doc.qt.io/qt-5/qserialport.html#open
                                  that says it sets the settings during open().

                                  did this change in one of the newer versions? apparently not. At least not as far back as 5.12


                                  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.

                                  1 Reply Last reply
                                  0
                                  • J.HilkJ J.Hilk

                                    @mimamrafi try adding a sleep/delay at the end of your Arduino loop, see if that improves things.

                                    M Offline
                                    M Offline
                                    mimamrafi
                                    wrote on last edited by
                                    #16

                                    @J-Hilk No, it still print nothing. Maybe, I will switch to arduino uno. It's hardware issue, not the QT. Thanks for help

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mimamrafi
                                      wrote on last edited by
                                      #17

                                      Hai everyone. I found some clues about this topic. The serial communication have to enable the DTR (Data Terminal Ready). So this is my current code that I used

                                      void SerialPortManager::openSerialPort()
                                      {
                                         arduino->setPortName("COM8");
                                         arduino->open(QIODevice::ReadWrite);
                                         arduino->setBaudRate(QSerialPort::Baud9600);
                                         arduino->setDataBits(QSerialPort::Data8);
                                         arduino->setParity(QSerialPort::NoParity);
                                         arduino->setStopBits(QSerialPort::OneStop);
                                         arduino->setFlowControl(QSerialPort::NoFlowControl);
                                         arduino->setDataTerminalReady(true);
                                      }
                                      

                                      Actually, I don't know why its working, but some of tutorial it's not a problem without writing setDataTerminalReady

                                      1 Reply Last reply
                                      3
                                      • J.HilkJ J.Hilk referenced this topic on

                                      • Login

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