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

Secure receiving serial data (QSerialPort)



  • Hello, everyone!

    In my application, I need to receive 8 bytes everytime a button (hardware) is pressed. I have already checked the bytes with a terminal software and the transmission is ok.

    I want to receive these bytes with QT, but sometimes the package comes wrong. I need help to build a secure routine to read the data without errors.

    What is the problem with my program?

    Until now I did this:

    @
    void ipd::serialReceived()
    {
    leitura = serial->readAll();
    dado_serial.append(leitura); // Group the data until 8 bytes

        tamanho_serial += leitura.size();             // Size of data
        if (tamanho_serial >=8)                               // If size is bigger than 8,
        {
            qDebug()<< "--"<< cont++ <<"--";
            qDebug()<< dado_serial.toHex();
    
            dado.REG_END    = dado_serial.mid(0,1).toHex();             // Get each byte
            dado.REG_FUNC   = dado_serial.mid(1,1).toHex();
            dado.REGISTRO0  = dado_serial.mid(2,1).toHex();
            dado.REGISTRO1  = dado_serial.mid(3,1).toHex();
            dado.REGISTRO2  = dado_serial.mid(4,1).toHex();
            dado.REGISTRO3  = dado_serial.mid(5,1).toHex();
            dado.REG_CRCH   = dado_serial.mid(6,1).toHex();
            dado.REG_CRCL   = dado_serial.mid(7,1).toHex();
    
             // Clear variables
            dado_serial = 0;                                                                            
            tamanho_serial = 0;
            flag_conta_silencio = 1;
            serial->clear();
    
            
        }
    

    }
    @


  • Qt Champions 2020

    You can take the Terminal example (from the QtSerialPort) and check received data. Of course, in case your data transferred in "string" form.


Log in to reply