Subclassing QIODevice
-
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 thisclass 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; };
But i caught this error. I implemented my own isSequential func but it uses another.
My own version looks like thisbool 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
-
@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?
-
Hi,
Since you are mentioning modbus on the bug report, then maybe the Qt SerialBus module will be a better choice.
-
Hi,
Since you are mentioning modbus on the bug report, then maybe the Qt SerialBus module will be a better choice.
@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)
-
@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?
@Christian-Ehrlicher https://github.com/Jihadist/libserial-wrapper/blob/e7774ba9f01bbd6487d2e4639c1e5240ee0fc930/main.cpp#L34 here is error
App crashes when i try to call read or write
-
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.
-
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.
@Christian-Ehrlicher Look at implementation please, open returns true if opened or false if not
-
@Christian-Ehrlicher Look at implementation please, open returns true if opened or false if not
@PaulNewman But you still go on when it failed.
-
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 thisclass 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; };
But i caught this error. I implemented my own isSequential func but it uses another.
My own version looks like thisbool 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
@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 -
@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@J-Hilk Do you see i use another backend which works another than QSerialPort and requires to set settings after openning port?
-
@J-Hilk Do you see i use another backend which works another than QSerialPort and requires to set settings after openning port?
@PaulNewman I did in fact not see that 🤷♂️
-
@PaulNewman But you still go on when it failed.
@Christian-Ehrlicher i dont, look at check() func
-
@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
-
@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
@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
-
@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.
Why did it call QIODevice::isSequential but not my own implementation?