PDA

View Full Version : one signal to two slots for reading and logging in a file



QJak
15th April 2018, 13:58
hi
I want to read from serial port (rs232) then processing and in the same time saving in a file. one way is using QThread but could i use "readyRead" signal with two slots like this instead of QTread?

signal(readyRead) , slot(doing some things and processing)

signal(readyRead) , slot(saving_file())

high_flyer
15th April 2018, 23:37
I don't know what threads have to do with the question.
Any way, have a look at the signal/slot documentation.
If you do, you will see you can connect any signal to any number of slots.
So yes, you can connect your readyRead() signal to two different slots.

Lesiok
16th April 2018, 06:55
But if in the first slot you do readAll() on the serial port then in the second you will not read anything from the port.

QJak
16th April 2018, 09:02
thanks. i need to read in two slots. but in first slot after reading, i do some processing and in second slot after reading i need to put it into a file.
if i understand your answer, you mean is that second slot can not read?

Lesiok
16th April 2018, 09:12
Yes. Why not to use one slot and simple call logging method from slot ?

QJak
16th April 2018, 10:22
realy thanks. please help me to do this. how can i use a slot and call saving into a file?

Lesiok
16th April 2018, 11:37
Slot is normal class method. So in body call some another method with QByteArray argument to save data to file.
These are the basics of C++.

QJak
16th April 2018, 11:47
thanks again. you mean i call saving function from body of slot? (i need to do two works (1-reading and processing 2-reading and writing in a file) together like tread.
please explain more

Lesiok
16th April 2018, 12:40
Yes. In slot You need :

1. Read data from port.
2. Process data.
3. Write data in a file.

Steps 2 and 3 can be swapped.

QJak
16th April 2018, 12:51
thanks but processing and writing after that is too slow. i need to do 2 jobs in the same time
is this work correct to emit a signal after reading like this:
i define a signal in name of : signal_saving(QByteArray) and connect this signal to the second slot below
then define 2 slots: slot1_readingAndProcessing() , slot2_saving(QByteArray)
after connecting signal_saving(QByteArray) to slot2_saving(QByteArray) i emit this signal after reading data in body of first slot. imean:
slot1_readingAndProcessing()
{
reading data and put in QByteArray
emit signal_saving(QBytArray)
then processing
}


is this correct?

Lesiok
16th April 2018, 13:37
Yes, this is correct under one condition : connection to slot2_saving is in Qt::QueuedConnection. In Qt::DirectConnection emiting a signal is equivalent to calling the method. Read documentation.

QJak
2nd May 2018, 15:41
this way has not my desier result (working paralel). please help me

d_stranz
2nd May 2018, 20:57
...thanks but processing and writing after that is too slow. i need to do 2 jobs in the same time...

...working paralel...

please help me

Please explain why you insist this must be a parallel operation (you say so here and in your other posts). What is your "processing" that makes it so slow? And writing a small amount of data to a log file should be very fast, unless you are opening and closing the file for every write.

Based on what you are asking and how you are asking it, I am not sure you understand enough to write a multi-threaded application that will work correctly.