Important: Please read the Qt Code of Conduct -

QSerialPort: "canReadLine" with \r instead of \n

  • Hello everybody,

    I try using canReadLine() of QSerialPort class to extract AT commands, sent over a serial port usually used to control Modems.
    AT Commands do look like this:
    "ATS209=3\r" ... We have to set register 209 = 3.
    "ATS44?\r" ... We have to tell what value register 44 has got. every command is ending with an '\r'. but canReadLine uses '\n' to detect a new Line.
    Is there a way to tell QSerialPort to tell what kind of character i want to use to separate lines?
    If not, has any body a Idea for a solution?
    I thought about read the inbuffer byte after byte until \r was found.
    That’s not that easy, because readData() only is called, when new Data arrive on serial Port. So If there is more than one Command in the in buffer they will pile up.

    @void MainWindow::readData(){
    QStringList AtElemts; // Contains all elemts of one Command
    char serialIndata[25]; // Contains raw input as char[]
    QString serialInString; // Contains raw input as String
    QString StrRegRegister; // Contains the Registernumber (ATS<StrRegRegister>=***)
    QString StrRegisterValue; // Contains the Registervalue (ATS***=<StrRegisterValue>)
    bool convOk; // Will be true, if String > Int convertion was ok
    uint RegRegister; // Registernumber convertet to Int
    uint RegisterValue; // Registevalue convertet to Int

        SerialArduino->readLine(serialIndata, 25);  // Take all we can get of input buffer
        serialInString = QString(serialIndata);     // convert bytes to a String
        AtElemts = SplitAtCommand(serialInString);  // This gives back a stringlist with:
                                                    // (0) [=] | [?] | [E] ...Tells us, what kind of command an if there was an Error
                                                    // (1) <Registernumber>
                                                    // (2) <Registevalue>
        StrRegRegister =;                    // Extract the registernumber from the list
        StrRegisterValue =;                  // Extract the registevalue from the list
        RegRegister = StrRegRegister.toUInt(&convOk, 0);    // Convert registernumber to Int
        RegisterValue = StrRegisterValue.toUInt(&convOk, 0);// Convert registevalue to Int
        if( == "="){                          // If we got a command to set a register (=)...
            recRegisters[RegRegister] = RegisterValue;      // then set it in recRegisters (glabal)



  • Lifetime Qt Champion

    Hi and welcome to devnet,

    From a quick look to the code, the only option I can see currently is to modify QSerialPort sources to do that

  • Qt Champions 2020

    Yes, because the internal read buffer is private and you can not derive from QSerialPort to has access to internal buffer. Besides, the method canReadLine - it is pure QIODevice::canReadLine(). So, you should himself implement reading by "\r" from readAll() and so on.

Log in to reply