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. Subclassing QIODevice
Forum Updated to NodeBB v4.3 + New Features

Subclassing QIODevice

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 1.5k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    MoonRaiser
    wrote on last edited by
    #1

    Hello, i'm trying to subclass QIODevice, create my own QSerialPort with another backend. I dont wanna use standard module due to it isnt compatitible with my hardware https://bugreports.qt.io/browse/QTBUG-87265
    I tried to follow docs and constructed this

    class SerialPort : public QIODevice {
    
        Q_OBJECT
        Q_DECLARE_PRIVATE(SerialPort)
    public:
        explicit SerialPort(QObject* parent = nullptr);
        explicit SerialPort(const QString& name, QObject* parent = nullptr);
        explicit SerialPort(const SerialPortInfo& info, QObject* parent = nullptr);
        ~SerialPort();
    
       ...
    public:
        bool open(OpenMode mode) override;
        void close() override;
        qint64 pos() const override;
        qint64 size() const override;
        bool atEnd() const override;
        qint64 bytesAvailable() const override;
        qint64 bytesToWrite() const override;
        bool waitForReadyRead(int msecs = 0) override;
        bool waitForBytesWritten(int msecs = 0) override;
        inline bool isSequential() const override;
    
        QString portName() const;
        bool setPortName(const QString& name);
    
        int baudRate() const;
        bool setBaudRate(const int& baudRate);
    
        DataBits dataBits() const;
        bool setDataBits(const DataBits& dataBits);
    
        Parity parity() const;
        bool setParity(const Parity& parity);
    
        StopBits stopBits() const;
        bool setStopBits(const StopBits& stopBits);
    
        FlowControl flowControl() const;
        bool setFlowControl(const FlowControl& flowControl);
    
    signals:
        void baudRateChanged(int baudRate);
        void dataBitsChanged(SerialPort::DataBits dataBits);
        void parityChanged(SerialPort::Parity parity);
        void stopBitsChanged(SerialPort::StopBits stopBits);
        void flowControlChanged(SerialPort::FlowControl flowControl);
    
    protected:
        qint64 readData(char* data, qint64 maxlen) override;
        qint64 writeData(const char* data, qint64 len) override;
    
    private:
        uint m_timeout = 1000;
        Q_DISABLE_COPY(SerialPort)
    
        // QIODevice interface
    public:
        bool canReadLine() const override;
    };
    

    alt text
    But i caught this error. I implemented my own isSequential func but it uses another.
    My own version looks like this

    bool SerialPort::isSequential() const
    {
        qDebug(__PRETTY_FUNCTION__);
        return true;
    }
    

    So what i do wrong? If i dont provide enough info everything is available here https://github.com/Jihadist/libserial-wrapper

    J.HilkJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MoonRaiser said in Subclassing QIODevice:

      But i caught this error.

      Which error? What does not work as expected? Do you call isSequential() somewhere and the wrong one is called?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        Hi,

        Since you are mentioning modbus on the bug report, then maybe the Qt SerialBus module will be a better choice.

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

        P 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Since you are mentioning modbus on the bug report, then maybe the Qt SerialBus module will be a better choice.

          P Offline
          P Offline
          PaulNewman
          wrote on last edited by
          #4

          @SGaist hi, Qt SerialBus is possible way but we have our own modbus implementation based on QSerialPort... And one some serious thing with QtSerialBus that it has problems with catching parity errors

          P.S. I have 2 acc(Moonraiser)

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @MoonRaiser said in Subclassing QIODevice:

            But i caught this error.

            Which error? What does not work as expected? Do you call isSequential() somewhere and the wrong one is called?

            P Offline
            P Offline
            PaulNewman
            wrote on last edited by
            #5

            @Christian-Ehrlicher https://github.com/Jihadist/libserial-wrapper/blob/e7774ba9f01bbd6487d2e4639c1e5240ee0fc930/main.cpp#L34 here is error alt text App crashes when i try to call read or write

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6
                  int error_code = port1.open(QIODevice::ReadWrite);
                  if (error_code)
                      qDebug("Port opened");
              

              This is wrong - if you can't open the port you should not work on it.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              P 1 Reply Last reply
              3
              • Christian EhrlicherC Christian Ehrlicher
                    int error_code = port1.open(QIODevice::ReadWrite);
                    if (error_code)
                        qDebug("Port opened");
                

                This is wrong - if you can't open the port you should not work on it.

                P Offline
                P Offline
                PaulNewman
                wrote on last edited by
                #7

                @Christian-Ehrlicher Look at implementation please, open returns true if opened or false if not

                Christian EhrlicherC 1 Reply Last reply
                0
                • P PaulNewman

                  @Christian-Ehrlicher Look at implementation please, open returns true if opened or false if not

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @PaulNewman But you still go on when it failed.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  P 1 Reply Last reply
                  0
                  • M MoonRaiser

                    Hello, i'm trying to subclass QIODevice, create my own QSerialPort with another backend. I dont wanna use standard module due to it isnt compatitible with my hardware https://bugreports.qt.io/browse/QTBUG-87265
                    I tried to follow docs and constructed this

                    class SerialPort : public QIODevice {
                    
                        Q_OBJECT
                        Q_DECLARE_PRIVATE(SerialPort)
                    public:
                        explicit SerialPort(QObject* parent = nullptr);
                        explicit SerialPort(const QString& name, QObject* parent = nullptr);
                        explicit SerialPort(const SerialPortInfo& info, QObject* parent = nullptr);
                        ~SerialPort();
                    
                       ...
                    public:
                        bool open(OpenMode mode) override;
                        void close() override;
                        qint64 pos() const override;
                        qint64 size() const override;
                        bool atEnd() const override;
                        qint64 bytesAvailable() const override;
                        qint64 bytesToWrite() const override;
                        bool waitForReadyRead(int msecs = 0) override;
                        bool waitForBytesWritten(int msecs = 0) override;
                        inline bool isSequential() const override;
                    
                        QString portName() const;
                        bool setPortName(const QString& name);
                    
                        int baudRate() const;
                        bool setBaudRate(const int& baudRate);
                    
                        DataBits dataBits() const;
                        bool setDataBits(const DataBits& dataBits);
                    
                        Parity parity() const;
                        bool setParity(const Parity& parity);
                    
                        StopBits stopBits() const;
                        bool setStopBits(const StopBits& stopBits);
                    
                        FlowControl flowControl() const;
                        bool setFlowControl(const FlowControl& flowControl);
                    
                    signals:
                        void baudRateChanged(int baudRate);
                        void dataBitsChanged(SerialPort::DataBits dataBits);
                        void parityChanged(SerialPort::Parity parity);
                        void stopBitsChanged(SerialPort::StopBits stopBits);
                        void flowControlChanged(SerialPort::FlowControl flowControl);
                    
                    protected:
                        qint64 readData(char* data, qint64 maxlen) override;
                        qint64 writeData(const char* data, qint64 len) override;
                    
                    private:
                        uint m_timeout = 1000;
                        Q_DISABLE_COPY(SerialPort)
                    
                        // QIODevice interface
                    public:
                        bool canReadLine() const override;
                    };
                    

                    alt text
                    But i caught this error. I implemented my own isSequential func but it uses another.
                    My own version looks like this

                    bool SerialPort::isSequential() const
                    {
                        qDebug(__PRETTY_FUNCTION__);
                        return true;
                    }
                    

                    So what i do wrong? If i dont provide enough info everything is available here https://github.com/Jihadist/libserial-wrapper

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @MoonRaiser
                    you open the serial port before you try to change the settings, you should actually get console warnings about that.
                    Set settings first than open the port


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    P 1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @MoonRaiser
                      you open the serial port before you try to change the settings, you should actually get console warnings about that.
                      Set settings first than open the port

                      P Offline
                      P Offline
                      PaulNewman
                      wrote on last edited by
                      #10

                      @J-Hilk Do you see i use another backend which works another than QSerialPort and requires to set settings after openning port?

                      J.HilkJ 1 Reply Last reply
                      0
                      • P PaulNewman

                        @J-Hilk Do you see i use another backend which works another than QSerialPort and requires to set settings after openning port?

                        J.HilkJ Online
                        J.HilkJ Online
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @PaulNewman I did in fact not see that 🤷‍♂️


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @PaulNewman But you still go on when it failed.

                          P Offline
                          P Offline
                          PaulNewman
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher i dont, look at check() func

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @PaulNewman said in Subclassing QIODevice:

                            i dont, look at check() func

                            You do...

                                int error_code = port1.open(QIODevice::ReadWrite);
                                if (error_code)
                                    qDebug("Port opened");
                                if (port1.setBaudRate(9600)) <<-- here you try access even if open failed
                            

                            and btw: open() returns a boolean, not an integer

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            P 1 Reply Last reply
                            2
                            • Christian EhrlicherC Christian Ehrlicher

                              @PaulNewman said in Subclassing QIODevice:

                              i dont, look at check() func

                              You do...

                                  int error_code = port1.open(QIODevice::ReadWrite);
                                  if (error_code)
                                      qDebug("Port opened");
                                  if (port1.setBaudRate(9600)) <<-- here you try access even if open failed
                              

                              and btw: open() returns a boolean, not an integer

                              P Offline
                              P Offline
                              PaulNewman
                              wrote on last edited by
                              #14

                              @Christian-Ehrlicher you are right here, but check() calls abort() if not true, so it never returns if port cannot be opened. It's only test case but it doesnt work when port was opened

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                PaulNewman
                                wrote on last edited by PaulNewman
                                #15

                                @Christian-Ehrlicher said in Subclassing QIODevice:

                                if (port1.setBaudRate(9600)) <<-- here you try access even if open failed

                                I fixed this, but problem stays...
                                Source code from topic header was updated to.
                                alt text

                                Why did it call QIODevice::isSequential but not my own implementation?

                                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