Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I use qMessage bar in a function for receive data for UART?



  • Hi,
    There are two case:
    Case 1: I do not get any response after sending the query.
    Case 2: I get two responses. 1st one similar to Case 1. I t means that I don't get any things but after some time I get the response of 8 bytes.

    Here, I am giving some condition to get the message if I have not received any response but the condition is that I am getting two condition similar in both conditions. How can I rectify that?

    void transmit(QSerialPort & port, const QByteArray & data) {
        port.write(data);
        port.flush();
        qDebug() << "\nWrote" << data.size() << ":" << data.toHex().constData();
        chkError(port);
    }
    
    void receive(QSerialPort & port) {
        QMessageBox msg;
        auto data = port.readAll();
        qDebug()<<data;
    
        qDebug() << "\nRead" << data.size() << ":" << data.toHex().constData();
        data = data.toHex().constData();
        int x,y,z,m,n;
         x = QString::compare("4a", data, Qt::CaseInsensitive);
         y = QString::compare("01", data, Qt::CaseInsensitive);
         z = QString::compare("5b", data, Qt::CaseInsensitive);
         m = QString::compare("03", data, Qt::CaseInsensitive);
         n = QString::compare("", data, Qt::CaseInsensitive);
    
        qDebug() << "x: "<<x;
        qDebug() << "y: "<<y;
        qDebug() << "z: "<<z;
        qDebug() << "m: "<<m;
        qDebug() << "n: "<<n;
        if(x==0)
        {
            msg.setText("\nConnection Error\n");
            msg.exec();
           // return;
        } else
        if(y==0)
        {
            msg.setText("Transmission Error");
            msg.exec();
            //return;
        }else
        if(z==0)
        {
            msg.setText("\nCRC ERROR\n");
            msg.exec();
           // return;
        } else
        if(m==0)
        {
            msg.setText("\nData Mismatch\n");
            msg.exec();
           // return;
        } else
            if(n!=0)
        {
            msg.setText("\nConnection failed\n");
            msg.exec();
        }else
        {
            ;
        }
    
        chkError(port);
    }
    

    The condition n is for not getting the data but how can I identify in both conditions? I am getting the response "condition failed" in both case.


  • Lifetime Qt Champion

    Hi,

    How are you using QSerialPort ? Asynchronously or synchronously ?

    In any case you have to take into account that you might not receive all your data at once. You have to cumulate what you get until you can ensure that you have a full frame of your data and then process it.



  • I am using the synchronously.
    I am not receiving the data at once.

       do {
              receive(port);
        } while (port.waitForReadyRead(3000));
    

  • Lifetime Qt Champion

    Out of curiosity, why use it synchronously ?


Log in to reply