[SOLVED] QtSerialPort send/recieve issue when window is resized, or mouse held down
-
wrote on 1 Oct 2013, 01:14 last edited by
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().
-
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.. :)
-
wrote on 5 Nov 2013, 09:44 last edited by
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 -
[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"?
-
wrote on 21 Nov 2013, 07:59 last edited by
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.