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 Updated to NodeBB v4.3 + New Features

QT reading Serialport of Arduino

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 3.9k 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 mimamrafi
    30 Nov 2021, 07:50

    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 Offline
    J Offline
    J.Hilk
    Moderators
    wrote on 30 Nov 2021, 07:58 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 30 Nov 2021, 08:28
    1
    • J jsulm
      30 Nov 2021, 07:54

      @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 30 Nov 2021, 08:06 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?

      J 1 Reply Last reply 30 Nov 2021, 08:10
      0
      • M mimamrafi
        30 Nov 2021, 08:06

        @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?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 30 Nov 2021, 08:10 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 J.Hilk
          30 Nov 2021, 07:58

          @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 30 Nov 2021, 08:28 last edited by
          #6

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

          arduino->open(QIODevice::ReadWrite);
          
          J 1 Reply Last reply 30 Nov 2021, 08:30
          0
          • M mimamrafi
            30 Nov 2021, 08:28

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

            arduino->open(QIODevice::ReadWrite);
            
            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 30 Nov 2021, 08:30 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 30 Nov 2021, 08:43
            0
            • J jsulm
              30 Nov 2021, 08:30

              @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 30 Nov 2021, 08:43 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

              J J 2 Replies Last reply 30 Nov 2021, 08:48
              0
              • M mimamrafi
                30 Nov 2021, 08:43

                @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 Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 30 Nov 2021, 08:48 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 30 Nov 2021, 08:54
                0
                • M mimamrafi
                  30 Nov 2021, 08:43

                  @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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 30 Nov 2021, 08:54 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 30 Nov 2021, 09:13
                  0
                  • J jsulm
                    30 Nov 2021, 08:48

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

                    M Offline
                    M Offline
                    mimamrafi
                    wrote on 30 Nov 2021, 08:54 last edited by
                    #11

                    @jsulm I only send

                    Serial.println ("test"); 
                    

                    in void loop

                    1 Reply Last reply
                    0
                    • J J.Hilk
                      30 Nov 2021, 08:54

                      @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 30 Nov 2021, 09:13 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 1 Reply Last reply 30 Nov 2021, 09:31
                      0
                      • M mimamrafi
                        30 Nov 2021, 09:13

                        @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 Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 30 Nov 2021, 09:31 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 30 Nov 2021, 10:06
                        0
                        • J jsulm
                          30 Nov 2021, 08:30

                          @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 30 Nov 2021, 09:38 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 1 Reply Last reply 30 Nov 2021, 09:48
                          0
                          • K kuzulis
                            30 Nov 2021, 09:38

                            @jsulm said in QT reading Serialport of Arduino:

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

                            No. It is irrelevant.

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 30 Nov 2021, 09:48 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 J.Hilk
                              30 Nov 2021, 09:31

                              @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 30 Nov 2021, 10:06 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 2 Dec 2021, 03:33 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 J.Hilk referenced this topic on 3 Jul 2023, 14:35

                                12/17

                                30 Nov 2021, 09:13

                                • Login

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