kpkale
10th July 2012, 13:30
I am writing a program for control of a machine using serial port. At one point, I am required to run a process on another thread. For implementing the same I am following the tutorial given here.
http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
My application is a GUI based application. The serial port and its functions are being handled by a separate class which has a port object (using qextserialport library). This port object is opened and closed at the start and ending of the application.
Here, when I press one button on the FORM, I require to start a thread that will monitor the serial port for a particular input and execute a slot when the input is recieved from the serial port.
Following is excerpts from the code written when the button is pressed.
QThread* thread = new QThread;
ports->moveToThread(thread);
connect(thread, SIGNAL(started()), ports, SLOT(start()));
connect(ports, SIGNAL(dataFound(QString)),this, SLOT(ondatafound(QString)));
connect(ports, SIGNAL(finished()), thread, SLOT(quits()));
connect(ports, SIGNAL(finished()), ports, SLOT(deleteLater()));
connect(ports, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
Here, the ports (Worker) class is the class which is controlling all the functions related to the serial port.
Here, the start slot executes a Qtimer that reads the serial port after a timeout till the required data is not received. And after receiving data, emits the datafound signal and also emits the finished signal.
This works for one time execution. However, when I press the Button again for re-executing which I am required to do again and again, after the ondatafound slot is executed, The QT gives an error and does not execute the movetothread.
What I see in the tutorial is that a new instance of the worker class is created and movetothread is called and I am not doing it that way... but dont know the right way of doing it either for the thing to work.
Secondly, I also need to have a provision of terminating this thread from the main thread in case I do not receive the data. So if i dont include the data from the serial port, then i need to push another button and continue the process further. I dont know how to terminate this thread from the main thread.
http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
My application is a GUI based application. The serial port and its functions are being handled by a separate class which has a port object (using qextserialport library). This port object is opened and closed at the start and ending of the application.
Here, when I press one button on the FORM, I require to start a thread that will monitor the serial port for a particular input and execute a slot when the input is recieved from the serial port.
Following is excerpts from the code written when the button is pressed.
QThread* thread = new QThread;
ports->moveToThread(thread);
connect(thread, SIGNAL(started()), ports, SLOT(start()));
connect(ports, SIGNAL(dataFound(QString)),this, SLOT(ondatafound(QString)));
connect(ports, SIGNAL(finished()), thread, SLOT(quits()));
connect(ports, SIGNAL(finished()), ports, SLOT(deleteLater()));
connect(ports, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
Here, the ports (Worker) class is the class which is controlling all the functions related to the serial port.
Here, the start slot executes a Qtimer that reads the serial port after a timeout till the required data is not received. And after receiving data, emits the datafound signal and also emits the finished signal.
This works for one time execution. However, when I press the Button again for re-executing which I am required to do again and again, after the ondatafound slot is executed, The QT gives an error and does not execute the movetothread.
What I see in the tutorial is that a new instance of the worker class is created and movetothread is called and I am not doing it that way... but dont know the right way of doing it either for the thing to work.
Secondly, I also need to have a provision of terminating this thread from the main thread in case I do not receive the data. So if i dont include the data from the serial port, then i need to push another button and continue the process further. I dont know how to terminate this thread from the main thread.