On Thu, Apr 4, 2019 at 5:46 PM Jérôme Godbout <godbo...@amotus.ca> wrote:
> It relief the main thread from read data all the time and keep a good > reactivity. The dedicated thread try to read and can wait until something > come along and once it found something that can be parsed, it emit his own > signal that the main thread only have to handle into normal slot (will be > queued, since it's not the same thread). > > But yeah, you can have the main thread do it and process the readyRead(), > you main thread will perform the read and parsing. if you have any CRC and > other things, this might be bad for application reactivity, depending on > the amount of data flowing. > > -----Original Message----- > From: Interest <interest-boun...@qt-project.org> On Behalf Of Paolo > Angelelli > Sent: April 4, 2019 10:19 AM > To: interest@qt-project.org > Subject: Re: [Interest] Parsing data from serialport > > What is the advantage of having such a continuous reading loop in a > separate thread? > > I mean it as opposed to reacting to the readyRead() signal, and having a > while(canReadLine()) { ... } in the reacting slot. > 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).
_______________________________________________ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest