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 dont emitted readyRead if window is resizing or moving
Qt 6.11 is out! See what's new in the release blog

QSerialPort dont emitted readyRead if window is resizing or moving

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.6k Views 1 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.
  • V Offline
    V Offline
    Varvara
    wrote on last edited by
    #1

    Hello, help me, please.

    I have class for work with some device by COM port. But it dont emitted readyRead if i'm resizing or moving program's window. How solve this problem?

    @class Device : public QObject
    {
    Q_OBJECT

    public:
    explicit Device(QObject *parent = 0);
    ~Device();
    bool openPort(const QString &portName);
    void closePort();
    bool isPortOpen() const;
    bool sendCommand(unsigned char cmd, unsigned char arg = 0);
    bool connectDevice();

    private slots:
    void onReadData();
    void error(QSerialPort::SerialPortError error);

    private:
    QSerialPort *port;
    QByteArray buffer;

    signals:
    void newDataAvailbled(CurrentData* data);
    };@

    @Device::Device(QObject *parent) : QObject(parent)
    {
    ...
    this->port = new QSerialPort(this);
    connect(this->port, SIGNAL(readyRead()), this, SLOT(onReadData()));
    }

    Device::~Device()
    {
    this->closePort();
    delete this->port;
    }

    bool Device::openPort(const QString &portName)
    {
    this->closePort();
    this->port->setPortName(portName);
    const bool isOpen = this->port->open(QIODevice::ReadWrite);
    if (isOpen == true) {
    this->port->setBaudRate(QSerialPort::Baud115200);
    this->port->setDataBits(QSerialPort::Data8);
    this->port->setFlowControl(QSerialPort::NoFlowControl);
    this->port->setParity(QSerialPort::NoParity);
    this->port->setStopBits(QSerialPort::OneStop);
    }
    return isOpen;
    }

    void Device::closePort()
    {
    if (this->port->isOpen())
    this->port->close();
    }

    bool Device::isPortOpen() const
    {
    return this->port->isOpen();
    }

    bool Device::sendCommand(unsigned char cmd, unsigned char arg)
    {
    if (this->isPortOpen() == false)
    return false;
    ...
    QByteArray buf;
    if (this->port->write(buf) != buf.size())
    return false;
    return true;
    }

    bool Device::connectDevice()
    {
    return this->sendCommand(DeviceCommands::TurnOffSMode);
    }

    void Device::onReadData()
    {
    this->buffer.append(this->port->readAll());
    ...
    emit this->newDataAvailbled(newData);
    }@

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

      Hi and welcome to devnet,

      I would say that the event loop is "blocked" processing the resizing hence the readyRead are not processed while your moving your window. You could move Device in another thread.

      @
      QThread *serialThread = new QThread(this);
      Device *device = new Device;
      device->moveToThread(serialThread);
      serialThread->start();
      // do connections etc...
      @

      Something like that

      Hope it helps

      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, SGaist is right.

        At present the single decision is use of a separate thread. But in the future (for Qt >= 5.x only) is planned to use I/O completion port feature for resolve this problem (but it just ideas on the future).

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Varvara
          wrote on last edited by
          #4

          I used moveToThread(), but problem was left((
          I'm use windows 7 and Qt5.1

          also, if connected readyRead to slot in Device by Qt:AutoConnection, then I have Heap Corruption on reading data. if connect by DirectConnection or BlockingQueuedConnection, then it's OK, but problem with moving of window was left

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Varvara
            wrote on last edited by
            #5

            So...
            I moved method from signal from readyRead() to timerEvent...
            @void Device::timerEvent(QTimerEvent *)
            {
            int c = port->bytesAvailable();
            ...
            emit testSignal(QTime::currentTime().toString() + " " + QString::number(c));
            ...
            }@

            and if I'm moving window, then c is zero, but signal was emitted and I getting current time and zero

            1 Reply Last reply
            0
            • V Offline
              V Offline
              Varvara
              wrote on last edited by
              #6

              sorry, I forgot waitForReadyBytes

              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