QSerialPort waitForReadyRead() only works in certain situations.
-
Hi, I am trying to control Arduino Mega 2560 through C++ using Qt
QSerialPort
. The code in thesetup()
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 lineSerial.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 receiveSerial.print("handshake");
over the serial port. Why? -
@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
-