Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSerialPort: "canReadLine" with \r instead of \n

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nonesenseN Offline
    nonesenseN Offline
    nonesense
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved