problem using QSerialPort
-
Hi all -
I've got an app that uses a COM port for initial configuration of a device that then joins a Wifi network. I'm using the QSerialPort class for this configuration. I'm connecting a readyRead signal for input, but it never triggers. I'm also getting this error message:
serial port error QSerialPort::NoError encountered. QObject: Cannot create children for a parent that is in a different thread. (Parent is QSerialPort(0x16d13d8), parent's thread is QThread(0x161cb20), current thread is QThread(0x16e58d0)
I'm wondering if I'm doing something wrong with my threads. Here's a code snippet:
int main(int argc, char *argv[]) { QApplication a(argc, argv); worker = new Worker(widget); thread = new QThread; worker->moveToThread(thread); widget->show(); thread->start(); rc = a.exec(); ... Worker::Worker(Widget *widget, QObject *parent) : QObject (parent) { m_serialPort = new SerialPort(this); ... SerialPort::SerialPort(QObject *parent) : QObject (parent) { QSerialPort m_serialPort; connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
My slot SerialPort::readSerial() never gets called. I've verified that there's incoming data using a port monitor. Any ideas what I've done wrong here?
Thanks...
-
What happens if you defer creation of the Serial port object until AFTER you've moved Worker to the other thread?
-
@Kent-Dorfman how do I do that?
-
dont construct it in the constructor, but send a signal to the thread later to construct and start the serial object.
-
@mzimmers said in problem using QSerialPort:
SerialPort::SerialPort(QObject *parent) : QObject (parent) { QSerialPort m_serialPort; connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial);
This doesn't look right. Are you sure this is what you wrote?
-
@mzimmers
IIRC you're using a static build? so its probably not the latest Qt Version on what are you running?If it isn't the error @kshegunov pointed out
-
@jsulm I'm not using a thread exclusively for the serial port work; my worker thread essentially does all my non-UI stuff(encoding/decoding messages, etc.)
@kshegunov sorry; I was engaging in a bit of shorthand. m_serialPort is a member variable of SerialPort. The connect is correct, though.
@J-Hilk I'm currently using (non-static) 5.12.5.Last night it occurred to me that long ago, I'd come across a problem with transferring objects to new threads, in which some sub-objects don't get transferred properly.
I might try Dorfman's idea, though I'd prefer not to have to do that.
Thanks...
-
@mzimmers said in problem using QSerialPort:
@J-Hilk I'm currently using (non-static) 5.12.5.
And there is your problem
https://bugreports.qt.io/browse/QTBUG-78086please update
-
Hi,
IIRC, your QSerialPort object won't get moved if your don't parent it.
-
@mzimmers said in problem using QSerialPort:
Good eye. Thanks for catching that. I'll update immediately.
Any way, your initialization seems not correct to me:
SerialPort::SerialPort(QObject *parent) : QObject (parent) { QSerialPort m_serialPort; connect(&m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial); ... }
should be, I think:
SerialPort::SerialPort(QObject *parent) : QObject (parent) , m_serialPort(new QSerialPort(this)) { connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPort::readSerial); ... }
-
@KroMignon thanks...so a QByteArray object wouldn't be affected then.
It looks like the mechanics of updating have changed since I last used them. Is there a pre-built 5.14 for Windows available somewhere on github? EDIT: looks like the answer is "no;" I guess everyone builds their own now. Given my recent troubles trying to build a static Qt, I hope the directions in the 5.14 README are accurate.
I'm going to mark this solved, in anticipation that when I get a new Qt built, it'll work. Thanks for everyone's input on this.
-
You need now to use your Qt account to access the pre-built binaries (the same you use to post here)
-
The bug tracker is your friend :-)