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. [SOLVED] QtSerialPort send/recieve issue when window is resized, or mouse held down

[SOLVED] QtSerialPort send/recieve issue when window is resized, or mouse held down

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.4k 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.
  • S Offline
    S Offline
    scoot80
    wrote on last edited by
    #1

    Hi everyone,

    I am using QtSerialPort class to talk to sensors over a USB connection (virtual com port). Everything works fine until a window is resized or moved, by either clicking and holding one of the borders, or clicking and holding the application title bar. While mouse is down, no data is outputted from the serial port, even though timer keeps firing, and write() keeps returning the correct number of bytes written.

    I've made a simple application to test this behavior by creating a window, a timer and serial port. Every time the timer fires, data is sent on the serial port. If you click and hold any of the window borders (as to resize), or the title bar, the timer continues firing, but no data is coming out of the serial port.

    Example:

    in window.h:
    @
    QSerialPort Port;
    QTimer testTimer;
    @

    in window.cpp
    @
    Port.setPortName("COM13");
    Port.open(QSerialPort::ReadWrite);
    Port.setBaudRate(57600);
    connect(&testTimer, SIGNAL(timeout()), this, SLOT(timerExpired()));
    testTimer.start(100);
    @

    timer expired:
    @
    static long i=0;
    i++;

    QString stringToSend = QString::number(i);
    QByteArray byteArray;
    byteArray.append(stringToSend);

    int bytesWritten = Port.write(byteArray);
    qDebug() << "Data sent: " << stringToSend << "bytes:" <<QString::number(bytesWritten);
    @

    Output:
    Data sent: "1" bytes: "1"
    Data sent: "2" bytes: "1"
    Data sent: "3" bytes: "1"
    Data sent: "4" bytes: "1"
    Data sent: "5" bytes: "1"
    Data sent: "6" bytes: "1"
    Data sent: "7" bytes: "1"
    Data sent: "8" bytes: "1"
    Data sent: "9" bytes: "1"
    Data sent: "10" bytes: "2"
    Data sent: "11" bytes: "2"
    Data sent: "12" bytes: "2"
    Data sent: "13" bytes: "2"
    ....

    Any ideas?

    Edit:

    Issue was solved by creating a thread, moving the serial port to that thread, and running thread->start().

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

      Yes, this problem is known, it belongs to QWinEventNotifier private Qt class (which is used in QSerialPort for tracking the serial port events). So, one solution - move to an separate thread.

      On future, maybe, we will change to QWinOverlappedNotifier private Qt class... And problem will be solved.. maybe.. :)

      1 Reply Last reply
      0
      • _ Offline
        _ Offline
        _Mark_
        wrote on last edited by
        #3

        I'm running into the same issue, Perhaps I don't use the thread correctly.

        @
        mySerial *m_serial;
        QThread *m_thread;

        m_serial = new mySerial(); // mySerial is a wrapper around QSerialPort,
        m_thread = new QThread(this);
        m_serial->moveToThread(m_thread);
        connect(m_serial, SIGNAL(lineReceived(QByteArray)), this, SLOT(lineReceived(QByteArray)));
        m_thread->start(QThread::HighPriority);
        @

        I have a QTimer which sends a string over the serial port. It always fires but when I move the QMainWindow the serial port doesn't send anything.

        Do you see any error in the code above?
        Thanks

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          [quote author="Mark" date="1383644663"]
          @
          m_serial = new mySerial(); // mySerial is a wrapper around QSerialPort,
          m_serial->moveToThread(m_thread);
          @
          [/quote]
          You moved the mySerial object, but is the QSerialPort moved too? Did you set the parent-child relationships properly? See http://doc-snapshot.qt-project.org/qt5-stable/qobject.html#thread-affinity

          [quote]but when I move the QMainWindow the serial port doesn't send anything.[/quote]What do you mean "move the QMainWindow"?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • _ Offline
            _ Offline
            _Mark_
            wrote on last edited by
            #5

            bq. You moved the mySerial object, but is the QSerialPort moved too? Did you set the parent-child relationships properly? See http://doc-snapshot.qt-project.org/qt5-stable/qobject.html#thread-affinity

            Yes, I did:

            @
            mySerial::mySerial(QObject *parent) : QObject(parent) {
            m_serial = new QSerialPort(this);
            // other stuff
            }
            @

            bq. What do you mean “move the QMainWindow”?

            Left-click on the title bar and move the mouse.

            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