Quoting interest-requ...@qt-project.org:

I think the point is that there's little reason to poll the serial port if
you can react to the event. Exactly what you'd do if you had a network
socket. Qt already does the heavy lifting for you, so you only need to
react to the signal and read as much as you want/need. Basically:

    QSerialPort * port;
    QObject::connect(port, &QIODevice::readyRead, port, [port] () -> void  {
        while (port->canReadLine())  {
            QByteArray data = port->readLine();
            // emit with data as argument and do the parsing
        }
    });

Whether you have the port in another thread or not is irrelevant in this
case, either can work fine (unlike your while-sleep loop).

Never ever ever do I/O in the main GUI thread of a Qt application. It's a recipe for disaster despite the countless examples you will find posted on-line and even in the Qt examples themselves. Those database examples are really nice. Try testing them with a million+ row table.

Real world serial comm is fraught with peril. I've been doing it since the days of DOS 3.1.

Before anyone goes running off and using the above lambda, they need to consider a few things.

Unless something has dramatically changed in QSerialPort, readyRead is only emitted for the first character in the buffer because there is no concept of block or packet. A UART handles one byte at a time.

When you get a readyRead and fail to completely empty the buffer, that's it. You never get another one.

When QSerialPort was taken from Playground and had all its method names changed, there was an odd timing bug too. In a production system running multiple ports of embedded target the code loading the next byte into the class buffer (perhaps not completely Qt code as it could have been in the device driver). The adding of the next byte could begin after readLine() determined how many bytes were in the buffer but prior to actually pulling them. The new byte got added. One less than a full buffer got read and readyRead never fired again because the next byte wasn't being written to a pristine empty buffer.


--
Roland Hughes, President
Logikal Solutions
(630) 205-1593

http://www.theminimumyouneedtoknow.com
http://www.infiniteexposure.net
http://www.johnsmith-book.com
http://www.logikalblog.com
http://www.interestingauthors.com/blog
http://lesedi.us

_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to