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.
    ...so 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

    if(SerialArduino->canReadLine()){
        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 = AtElemts.at(1);                    // Extract the registernumber from the list
        StrRegisterValue = AtElemts.at(2);                  // 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(AtElemts.at(0) == "="){                          // If we got a command to set a register (=)...
            recRegisters[RegRegister] = RegisterValue;      // then set it in recRegisters (glabal)
        }
    }
    

    }@

    Regards
    Jens


  • 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



  • 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
 

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