[SOLVED] QString.Contains Code works only with break point, does not work if no break point



  • I am a beginner on QSerialPort Terminal Example.
    Whenever my sensor initialization happens, SerialPort should receive data string that contains the menu selection string SenInitialMenuString as shown below.

    Here is my code:

    void MainWindow::readData()
    {

    QString SenDataString;
    QString SenInitialMenuString = "------SparkFun SPI Shortcut------\n\r\n\rMAIN MENU:\n\r(1) Actions\n\r(2) Settings\n\r\n\r->";
    
    QByteArray data = serial->readAll();
    console->putData(data);
    
    SenDataString = data;
    if(SenDataString.contains(SenInitialMenuString))
    {
        qDebug("\nThis is the %d time initialization", ++NumOfInitial);
        SenFakeKey = new QKeyEvent(QEvent::KeyPress,Qt::Key_1,Qt::NoModifier, "1",false,1);
        console->SenFakeKeyPressEvent(SenFakeKey);
    }
    

    }


    However, this code works very well only if I add a break point at the line of
    " if(SenDataString.contains(SenInitialMenuString))",
    whenever I run the code with no break point, it will pass it like it was never there.

    Could anyone help me find out what is going on ?

    Thanks!



  • I added a delay function before reading the serial port,

    console->SenDelay(70);
    QByteArray data = serial->readAll();
    

    now everything is working.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.