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. QSerialPort waitForReadyRead() only works in certain situations.
Forum Updated to NodeBB v4.3 + New Features

QSerialPort waitForReadyRead() only works in certain situations.

Scheduled Pinned Locked Moved Solved General and Desktop
qserialport
3 Posts 2 Posters 468 Views 1 Watching
  • 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.
  • CJhaC Offline
    CJhaC Offline
    CJha
    wrote on last edited by CJha
    #1

    Hi, I am trying to control Arduino Mega 2560 through C++ using Qt QSerialPort. The code in the setup() function in Arduino is:

    void setup() {
      Serial.begin(115200);
      Serial.print("handshake"); // handshake signal to estable authenticity
      Serial.flush();
    }
    

    I have a findDevices() function to open the port to Arduino:

    void MainWindow::findDevices()
    {
        if(port.isOpen()) // port is a QSerialPort object
            port.close();
        auto portList = QSerialPortInfo::availablePorts(); // looks for all available serial ports
        if(!portList.isEmpty()) {
            for(auto ii = 0; ii < portList.length(); ++ii) {
                if((portList[ii].productIdentifier() == 66) && (portList[ii].vendorIdentifier() == 9025)) { // Arduino Mega's PID and VID
                    port.setPort(portList[ii]);
                    bool portOpen = port.open(QIODevice::ReadWrite); // opens the port for read and write, unable to open if Arduino Serial Monitor is open
                    if(portOpen) {
                        bool baudSuccess = port.setBaudRate(QSerialPort::Baud115200, QSerialPort::AllDirections); // setting the baud rate
                        if(baudSuccess) {
                            bool readAvailable = port.waitForReadyRead(30000); // waiting (for 30 seconds) for reply from Arduino telling me that the Serial.print() is ready to send data
                            while(readAvailable) {
                                qDebug() << "Port Open, Read All:" << QString::fromUtf8(port.readAll()); // reading the Serial data from Arduino, this is where the "handshake" is received and then printed in the debug output
                                readAvailable = port.waitForReadyRead(30000); // waiting again for 30 seconds, sometimes only half of Serial output arrives and if anything is still coming in next 30 seconds this becomes true again
                            }
                        }
                        else {
                            port.close(); // if unable to set the baud rate then I close the port
                        }
                    }
                    break;
                }
            }
        }
    }
    

    I have an Arduino hardware that I have to detect through my C++ application. I can search through all the COM ports available in my system through my application. I look for Arduino Mega 2560 through it's VID (9025) and PID (66). I always find it if it is connected through the USB port. If the Arduino IDE Serial Monitor is open then of course I am unable to open a COM port to this Arduino Mega 2560 which makes sense since only one COM port can communicate at a time with the Arduino. So I close the Serial Monitor in my Arduino IDE and I am able to open my COM port in the C++ application.

    The problem: Each time I open (or reset) the COM port using my C++ application the setup() function in Arduino Mega 2560 runs again and the line Serial.print("handshake"); is executed and sent over the serial port to my C++ application every time but only if I have had open the Arduino Serial Monitor at least once before trying to connect with my C++ application. If I directly plug in my Arduino Mega 2560 in my PC and do not open the Arduino Serial Monitor then I never receive Serial.print("handshake"); over the serial port. Why?

    J.HilkJ 1 Reply Last reply
    0
    • CJhaC CJha

      Hi, I am trying to control Arduino Mega 2560 through C++ using Qt QSerialPort. The code in the setup() function in Arduino is:

      void setup() {
        Serial.begin(115200);
        Serial.print("handshake"); // handshake signal to estable authenticity
        Serial.flush();
      }
      

      I have a findDevices() function to open the port to Arduino:

      void MainWindow::findDevices()
      {
          if(port.isOpen()) // port is a QSerialPort object
              port.close();
          auto portList = QSerialPortInfo::availablePorts(); // looks for all available serial ports
          if(!portList.isEmpty()) {
              for(auto ii = 0; ii < portList.length(); ++ii) {
                  if((portList[ii].productIdentifier() == 66) && (portList[ii].vendorIdentifier() == 9025)) { // Arduino Mega's PID and VID
                      port.setPort(portList[ii]);
                      bool portOpen = port.open(QIODevice::ReadWrite); // opens the port for read and write, unable to open if Arduino Serial Monitor is open
                      if(portOpen) {
                          bool baudSuccess = port.setBaudRate(QSerialPort::Baud115200, QSerialPort::AllDirections); // setting the baud rate
                          if(baudSuccess) {
                              bool readAvailable = port.waitForReadyRead(30000); // waiting (for 30 seconds) for reply from Arduino telling me that the Serial.print() is ready to send data
                              while(readAvailable) {
                                  qDebug() << "Port Open, Read All:" << QString::fromUtf8(port.readAll()); // reading the Serial data from Arduino, this is where the "handshake" is received and then printed in the debug output
                                  readAvailable = port.waitForReadyRead(30000); // waiting again for 30 seconds, sometimes only half of Serial output arrives and if anything is still coming in next 30 seconds this becomes true again
                              }
                          }
                          else {
                              port.close(); // if unable to set the baud rate then I close the port
                          }
                      }
                      break;
                  }
              }
          }
      }
      

      I have an Arduino hardware that I have to detect through my C++ application. I can search through all the COM ports available in my system through my application. I look for Arduino Mega 2560 through it's VID (9025) and PID (66). I always find it if it is connected through the USB port. If the Arduino IDE Serial Monitor is open then of course I am unable to open a COM port to this Arduino Mega 2560 which makes sense since only one COM port can communicate at a time with the Arduino. So I close the Serial Monitor in my Arduino IDE and I am able to open my COM port in the C++ application.

      The problem: Each time I open (or reset) the COM port using my C++ application the setup() function in Arduino Mega 2560 runs again and the line Serial.print("handshake"); is executed and sent over the serial port to my C++ application every time but only if I have had open the Arduino Serial Monitor at least once before trying to connect with my C++ application. If I directly plug in my Arduino Mega 2560 in my PC and do not open the Arduino Serial Monitor then I never receive Serial.print("handshake"); over the serial port. Why?

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

      @CJha IIRC we had this perviously and the user was able to fix this issue by calling

      setDataTerminalReady(true) on the QSerialport instance


      ah, found it:

      https://forum.qt.io/topic/132401/qt-reading-serialport-of-arduino/17?_=1688394838410


      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.

      CJhaC 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @CJha IIRC we had this perviously and the user was able to fix this issue by calling

        setDataTerminalReady(true) on the QSerialport instance


        ah, found it:

        https://forum.qt.io/topic/132401/qt-reading-serialport-of-arduino/17?_=1688394838410

        CJhaC Offline
        CJhaC Offline
        CJha
        wrote on last edited by
        #3

        @J-Hilk Thank you so much, it fixed the problem :)

        1 Reply Last reply
        0
        • CJhaC CJha has marked this topic as solved on

        • Login

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