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

Changing end-of-line character for QSerialPort?



  • Is there any way to change the end-of-line character for QSerialPort from /n to /r so that canReadLine will return true if there is only /r between lines?



  • I don't believe there is any way to change the '\n' end of line character to '\r' since it is the standard for for QSerialPort which inherits "QIODevice":http://qt-project.org/doc/qt-4.8/qiodevice.html#getChar

    It may not be the most efficient way of doing it, but you could call getChar() and read until you get your '\r' character. This probably isn't the most GUI friendly way unless you toss it in a thread.

    Depending on how your using the QSerialPort class, a common way to read the port if your expecting data after a '\r' (say its streaming data constantly on the port) is to use a circular buffer. You can call readAll() and just post the remaining bytes for the next readyRead() you encounter.



  • Well, I had to write my own canReadLine and readLine equivalents that use the peek function to inspect the data stream for the existence and location of \r characters. It works but it's not elegant. This is all because the device I'm talking to does a non-standard end-of-line. Other devices I've used all use \n.

    IMHO, full support of termios that would allow me to set ICRNL and other configuration flags would be better. Of course that assumes that these are available on all platforms e.g. Windows.

    So, feature request.


  • Moderators

    Hi,

    If you want to submit a feature request, you can do so https://bugreports.qt-project.org/ (Create a new issue and set the Issue Type to "Suggestion").


  • Qt Champions 2020

    RogueWarrior,

    you can derive you own class from the QSerialPort and implement any your feature as you want. Just override an desired virtual methods:

    [code]
    MyCoolSerialPort : public QSerialPort
    {
    public:
    ...
    bool canReadLine() const Q_DECL_OVERRIDE;
    ...

    protected:
    ...
    qint64 readLineData(char *data, qint64 maxSize) Q_DECL_OVERRIDE;
    ...

    }
    [/code]

    and so on.

    Besides, you can use QSerialPort::handle() to setup your specific options to termios.



  • For the record, here is the report: https://bugreports.qt-project.org/browse/QTBUG-38812



  • Are the details of handle documented anywhere?



  • [quote author="RogueWarrior" date="1399471127"]Are the details of handle documented anywhere?[/quote]

    The "handle" is almost certainly going to be platform-specific, so you'll have to refer to the Win32 API documentation or the POSIX standard, respectively.



  • @#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
    typedef void* Handle;
    #else
    typedef int Handle;
    #endif
    @

    https://qt.gitorious.org/qt/qtserialport/source/c0fc9aecd67323630011eaa9a1c213eed5bc5387:src/serialport/qserialport.h#L78



  • I think the really interesting part is in "qserialport_win_p.h" and "qserialport_unix_p.h", respectively:
    @class QSerialPortPrivate : public QSerialPortPrivateData
    {
    public:
    HANDLE handle; /WINDOWS version/
    int descriptor; /UNIX version/
    }@

    So, not much surprising, it's a "native" file "HANDLE":http://goo.gl/Hrjk1W on the Windows platform and it's a standard file descriptor on POSIX systems.


Log in to reply